new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("to continue, turn on device location, which uses Google's location service")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, this);
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return l;
}
})
.setNegativeButton("NO THANKS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
})
.create()
.show();
对于API级别>23,必须先请求运行时权限,然后才能获取用户的最后一个已知位置