package com.example.myapplication;总结
import androID.Manifest;
import androID.annotation.Suppresslint;
import androID.app.Activity;
import androID.content.Context;
import androID.content.pm.PackageManager;
import androID.location.Location;
import androID.location.LocationListener;
import androID.location.LocationManager;
import androID.os.Bundle;
import androID.util.Log;
import androID.Widget.TextVIEw;
import androID.Widget.Toast;
import androIDx.core.app.ActivityCompat;
import java.util.List;
import static com.example.myapplication.MainActivity.tag;
public class FourActivity extends Activity {
public static final int SHOW_LOCATION = 0;
private TextVIEw positionTextVIEw;
private LocationManager locationManager;
private String provIDer;
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.activity_four);
positionTextVIEw = (TextVIEw) findVIEwByID(R.ID.btfour);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> provIDerList = locationManager.getProvIDers(true);
if (provIDerList.contains(LocationManager.GPS_PROVIDER)) {
provIDer = LocationManager.GPS_PROVIDER;
Log.d(tag, "测试GPS");
} else if (provIDerList.contains(LocationManager.NETWORK_PROVIDER)) {
provIDer = LocationManager.NETWORK_PROVIDER;
Log.d(tag, "测试NETWORK");
} else {
Log.d(tag, "没有可用的位置提供器");
return;
}
@Suppresslint("MissingPermission")Location location = locationManager.getLastKNownLocation(provIDer);
if (location != null) {
showLocation(location);
Log.d(tag, "显示当前设备的位置信息");
} else {
Log.d(tag, "位置信息是空");
}
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.d(tag, "检测权限");
return;
}
locationManager.requestLocationUpdates(provIDer, 2000, 1,
locationListener);
}
protected voID onDestroy() {
super.onDestroy();
if (locationManager != null) {
// 关闭程序时将监听器移除
locationManager.removeUpdates(locationListener);
};
}
LocationListener locationListener = new LocationListener() {
@OverrIDe
public voID onStatusChanged(String provIDer, int status, Bundle extras) {
}
@OverrIDe
public voID onProvIDerEnabled(String provIDer) {
}
@OverrIDe
public voID onProvIDerDisabled(String provIDer) {
}
@OverrIDe
public voID onLocationChanged(Location location) {
// 更新当前设备的位置信息
showLocation(location);
}
};
public voID showLocation(Location location) {
String currentposition="纬度"+location.getLatitude()+"\n"+"经度"+location.getLongitude();
positionTextVIEw.setText(currentposition);
}
}
以上是内存溢出为你收集整理的获取gps全部内容,希望文章能够帮你解决获取gps所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)