android获取定位三种⽅法,Android使⽤⽹络获取定位的⽅法本⽂实例为⼤家分享了Android使⽤⽹络获取定位的具体代码,供⼤家参考,具体内容如下
⽬标效果:
程序运⾏弹出权限选择,选择运⾏⽹络定位后会查询位置,然后在TextView上显⽰当前国家和城市。
1.l页⾯定义TextView显⽰城市名。
l页⾯:
xmlns:tools="schemas.android/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" >
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在定位。。。" />
2.新建Common.java页⾯,设置公共常量。
Common.java页⾯:
package com.sc.demomon;
/**
* 公共常量
* @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.demomon.Common;
import android.app.Service;
t.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 (Provider(LocationManager.NETWORK_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
else if (Provider(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, String());
sendBroadcast(intent);
// 如果只是需要定位⼀次,这⾥就移除监听,停掉服务。如果要进⾏实时定位,可以在退出应⽤或者其他时刻停掉定位服务。veUpdates(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.demomon.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;
t.BroadcastReceiver;
t.Context;
t.Intent;
t.IntentFilter;
public class MainActivity extends Activity {
private TextView text;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
text = (TextView) findViewById();
// 注册⼴播
IntentFilter filter = new IntentFilter();
filter.addAction(Common.LOCATION_ACTION);
Intent intent = new Intent();
startService(intent);
// 等待提⽰
dialog = new ProgressDialog(this);安卓intent用法
dialog.setMessage("正在定位...");
dialog.setCancelable(true);
dialog.show();
}
private class LocationBroadcastReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
if (!Action().equals(Common.LOCATION_ACTION))
return;
String locationInfo = StringExtra(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
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 = (i);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论