LocationManager 不会在重新打开应用程序后启动
LocationManager won't start after re-opening app
我正在开展一个项目,该项目定期确定 phone 的坐标并将它们上传到数据库中。我使用的代码是:
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){
}
}
}
}
从一个干净的开始(没有 cache/data 关于之前保存的应用程序)它运行良好并且按预期工作:
- 启动第一个 activity,这是一个简单的登录(如果 usertext=user
$$ passtext=文本开始
Activity2
)
Activity2
请求权限(如果 GPS 被禁用,则请求权限)
- 等待 GPS 发现其第一个位置
- 每 5 秒上传一次位置
但是如果我关闭应用程序(不是按后退按钮而是从 RAM 中清除它)然后我尝试再次打开它,在 Activity2
启动后它不再要求我启用 GPS 并且它不再显示位置,即使它已经启用。让它恢复工作的唯一方法是清除内部存储内存中的数据。
我该如何解决这个问题?谢谢。
问题是当 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);
}
我正在开展一个项目,该项目定期确定 phone 的坐标并将它们上传到数据库中。我使用的代码是:
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){
}
}
}
}
从一个干净的开始(没有 cache/data 关于之前保存的应用程序)它运行良好并且按预期工作:
- 启动第一个 activity,这是一个简单的登录(如果 usertext=user
$$ passtext=文本开始
Activity2
) Activity2
请求权限(如果 GPS 被禁用,则请求权限)- 等待 GPS 发现其第一个位置
- 每 5 秒上传一次位置
但是如果我关闭应用程序(不是按后退按钮而是从 RAM 中清除它)然后我尝试再次打开它,在 Activity2
启动后它不再要求我启用 GPS 并且它不再显示位置,即使它已经启用。让它恢复工作的唯一方法是清除内部存储内存中的数据。
我该如何解决这个问题?谢谢。
问题是当 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);
}