App实现可定位可以通过网络network进行定位access.coarse.location(粗定位),同时还可以实现更加精确的GPS定位。当然首先需要declare android manifest.
两种权限:
1,网络粗定位
根据其名字网络定位,就知道需要开启网络了
2,精确定位
精确定位默认已经准备好粗定位的权限。
获取LocationManager的引用
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
demo
监听位置变化,一旦位置1秒后距离变化一米,即通过Handler更新textview值
代码:
package trend.applocation;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";
private static final int REQUEST_LOCATION = 1;
private TextView text;
private Handler myhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case REQUEST_LOCATION:
double[] locations = (double[]) msg.obj;
text.setText("latitude:" + locations[0] + " longitude:" + locations[1]);
break;
default:
break;
}
}
};
LocationManager locationManager;
MyLocationListener myLocationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
List providerlist = locationManager.getAllProviders();
for(String str : providerlist){
Log.i(TAG, "onCreate: Provider:"+str);
}
myLocationListener = new MyLocationListener();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//public void onRequestPermissionsResult(int requestCode, String[] permissions,
//int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.i(TAG, "onCreate: ------------>cannot permission");
return;
}else{
Log.i(TAG, "onCreate: ------------>has permission");
}
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, myLocationListener);
Location location_ = new Location(LocationManager.GPS_PROVIDER);
Log.i(TAG, "onCreate: "+location_.getLatitude());
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(TAG, "onCreate: ---->"+location);
}private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
double[] locations = new double[2];
locations[0] = latitude;
locations[1] = longitude;
Log.i(TAG, "onLocationChanged: --->"+locations[0]);
Message.obtain(myhandler, REQUEST_LOCATION, locations).sendToTarget();
}@Override
public void onStatusChanged(String s, int i, Bundle bundle) {}@Override
public void onProviderEnabled(String s) {}@Override
public void onProviderDisabled(String s) {}
}@Override
protected void onStart() {
super.onStart();
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gpsEnabled) {
enableLocationSettings();
}}private void enableLocationSettings() {
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}@Override
protected void onStop() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//public void onRequestPermissionsResult(int requestCode, String[] permissions,
//int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(myLocationListener);
super.onStop();
}
}
【Android应用分析|App实现可定位】结果如下:
文章图片
推荐阅读
- 图片系列(6)不同版本上 Bitmap 内存分配与回收原理对比
- webrtc|webrtc 编译环境搭建
- android|android webrtc编译成功之后,webrtc封装sdk(五)编译webrtc android遇到的问题
- android|android mvc mvp 简书,浅析 MVP,MVC,MVVM模式(Android)
- android|Android非mtk平台T9的实现
- java|Hessian 序列化、反序列化
- 大数据|抖音 Android 性能优化系列(Java 锁优化)
- Gradle|Gradle基础——Gradle构建的生命周期
- 程序员|2021年Android岗位BAT大厂面试题知识点小结,小白也能看明白