我正在开发一个项目,定期确定手机的坐标并将其上传到数据库中。我正在使用的代码是:
public class Activity2 extends AppCompatActivity {
private LocationManager locm;
private LocationListener locl;
private Geocoder geocoder;
public TextView text, position;
public double lat, lon;
public String city;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
text = (TextView) findViewById(R.id.textView4);
position = (TextView) findViewById(R.id.textView5);
text.setText("Seriale: " + Build.SERIAL + "\nNome: " + Build.MANUFACTURER + " " + Build.PRODUCT);
geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
locm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locl = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
try{
lat=location.getLatitude();
lon=location.getLongitude();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(Calendar.getInstance().getTime());
List<Address> la = geocoder.getFromLocation(lat, lon, 1);
if (la != null & la.size() > 0) {
Address address = la.get(0);
city = address.getLocality();
}
else
{
city="UNKNOWN";
}
MainActivity.st.executeUpdate("INSERT INTO position VALUES ('" + Build.SERIAL + "', '" + date + "', '" + lat + "', '" + lon + "', '" +city + "')");
position.setText(city);
} catch (Exception a){
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
}, 10);
return;
}
}else{
locm.requestLocationUpdates("gps", 5000, 0, locl);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED) {
try{
locm.requestLocationUpdates("gps", 5000, 0, locl);
}catch (SecurityException e){
}
}
}
}
从一个干净的开始(没有关于以前保存的应用程序的缓存/数据)它运行良好并按预期工作:
Activity2
)Activity2
要求提供权限(如果已禁用,请提供GPS)但是,如果我关闭应用程序(不是按下后退按钮,而是从RAM中清除它),然后我尝试再次打开它,在Activity2
启动后它不再要求我启用GPS而且它不会即使它已经启用,也不再显示位置。让它恢复工作的唯一方法是清理内部存储器中的数据。
我该如何解决这个问题?谢谢。
答案 0 :(得分:1)
问题是当Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
为true
并且您已拥有所需权限时,您不会致电requestLocationUpdates()
。
尝试以下方法:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET }, 10);
return;
} else {
// you are missing this else block
locm.requestLocationUpdates("gps", 5000, 0, locl);
}
} else {
locm.requestLocationUpdates("gps", 5000, 0, locl);
}