私は定期的に電話の座標を決定し、データベースにアップロードするプロジェクトに取り組んでいます。私が使用しているコードです:きれいなスタートからアプリケーションを再起動してもLocationManagerが起動しない
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){
}
}
}
}
(キャッシュなし/前に保存されたアプリに関するデータ)、それは良い実行され、期待通りに動作します:
- がある最初の活動を開始します(それが無効になっていた場合やGPS)簡単なログインが(usertext =ユーザー $$ passtext =テキストが始まる場合
Activity2
) Activity2
は、その第一の位置を検出するためにGPSのため- 待機パーミッションを要求 の
- アップロードごとに5秒
を位置付けしかし、私は(ボタンを背面押しが、RAMから、それを掃除しない)アプリを閉じてから、私は再びそれを開こうとする場合、Activity2
開始後、それは私に聞かないんもうGPSを有効にするためのもので、すでに有効になっていても位置情報は表示されません。これを元に戻す唯一の方法は、内部ストレージ・メモリーからデータをクリーニングすることです。
どうすればこの問題を解決できますか?ありがとうございました。
ああ親愛なります!私はそれを完全に逃した。どのような速い答え、今それは完璧に動作します、ありがとう! – Dadex