Android使用网络获取定位的方法
程序员文章站
2023-11-05 14:46:04
本文实例为大家分享了android使用网络获取定位的具体代码,供大家参考,具体内容如下
目标效果:
程序运行弹出权限选择,选择运行网络定位后会查询位置,...
本文实例为大家分享了android使用网络获取定位的具体代码,供大家参考,具体内容如下
目标效果:
程序运行弹出权限选择,选择运行网络定位后会查询位置,然后在textview上显示当前国家和城市。
1.activity_main.xml页面定义textview显示城市名。
activity_main.xml页面:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <textview android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在定位。。。" /> </relativelayout>
2.新建common.java页面,设置公共常量。
common.java页面:
package com.sc.demo.common; /** * 公共常量 * @author wxy * */ public class common { public static final string location = "location"; public static final string location_action = "locationaction"; }
3.新建locationsvc.java页面作为服务进行定位。
locationsvc.java页面:
package com.sc.demo.locate; import com.sc.demo.common.common; import android.app.service; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.ibinder; import android.util.log; import android.widget.toast; /** * 定位服务 * @author wxy * */ public class locationsvc extends service implements locationlistener { private locationmanager locationmanager; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { locationmanager = (locationmanager) getsystemservice(location_service); } @override public void onstart(intent intent, int startid) { if (locationmanager.getprovider(locationmanager.network_provider) != null) locationmanager .requestlocationupdates(locationmanager.network_provider, 0, 0, this); else if (locationmanager.getprovider(locationmanager.gps_provider) != null) locationmanager .requestlocationupdates(locationmanager.gps_provider, 0, 0, this); else toast.maketext(this, "无法定位", toast.length_short).show(); } @override public boolean stopservice(intent name) { return super.stopservice(name); } @override public void onlocationchanged(location location) { //通知activity intent intent = new intent(); intent.setaction(common.location_action); intent.putextra(common.location, location.tostring()); sendbroadcast(intent); // 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。 locationmanager.removeupdates(this); stopself(); } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } }
4.mainactivity.java页面获取经纬度,然后根据经纬度获取城市名。
mainactivity.java页面:
package com.sc.demo; import java.io.ioexception; import java.util.list; import com.sc.demo.common.common; import com.sc.demo.locate.locationsvc; import android.location.address; import android.location.geocoder; import android.os.bundle; import android.util.log; import android.widget.textview; import android.app.activity; import android.app.progressdialog; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; public class mainactivity extends activity { private textview text; private progressdialog dialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); text = (textview) findviewbyid(r.id.text); // 注册广播 intentfilter filter = new intentfilter(); filter.addaction(common.location_action); this.registerreceiver(new locationbroadcastreceiver(), filter); // 启动服务 intent intent = new intent(); intent.setclass(this, locationsvc.class); startservice(intent); // 等待提示 dialog = new progressdialog(this); dialog.setmessage("正在定位..."); dialog.setcancelable(true); dialog.show(); } private class locationbroadcastreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { if (!intent.getaction().equals(common.location_action)) return; string locationinfo = intent.getstringextra(common.location); double latitude = double //截取经纬度转换为double型 .parsedouble(locationinfo.substring(17, 26)); double longitude = double.parsedouble(locationinfo .substring(27, 37)); text.settext(getaddress(latitude, longitude)); dialog.dismiss(); mainactivity.this.unregisterreceiver(this);// 不需要时注销 } public string getaddress(double latitude, double longitude) { string cityname = ""; list<address> addlist = null; geocoder ge = new geocoder(mainactivity.this); try { addlist = ge.getfromlocation(latitude, longitude, 1); } catch (ioexception e) { e.printstacktrace(); } if (addlist != null && addlist.size() > 0) { for (int i = 0; i < addlist.size(); i++) { address ad = addlist.get(i); cityname += ad.getcountryname() + ";" + ad.getlocality(); } } log.i("wxy", "city:" + cityname); return cityname; } } }
5.androidmanifest.xml页面添加权限并声明服务。
androidmanifest.xml页面:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sc.demo" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="18" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.sc.demo.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <service android:name=".locate.locationsvc" /> </application> </manifest>
6.运行就能显示目标效果了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。