Android使用网络获取定位的方法

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android使用网络获取定位的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了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页面:

 <&#63;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.运行就能显示目标效果了。

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android使用网络获取定位的方法全部内容,希望文章能够帮你解决Android使用网络获取定位的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。