0

を使用するアプリがありますgps。アプリが(ユーザーまたはAndroid OSによって)強制的に閉じられ、再度開かれない限り、正常に機能しています。gpsその後、更新を閉じることができないようです。これは私のコードです:

private void registerLocationUpdates() {
    Intent intent = new Intent(ParkOGuardActivity.class.getName()
            + ".LOCATION_READY");
    pendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(), 0, intent, 0);
    // minimum every 1 minutes, 5 kilometers 
    this.locationManager.requestLocationUpdates(this.provider, 5000,
            300000, pendingIntent);
}

private void cancelLocationUpdates() {
    if(pendingIntent != null){
        Log.d(TAG,pendingIntent!=null ? "pending is not null" : "pending is null");
        this.locationManager.removeUpdates(pendingIntent);
    }
}

メソッドを呼び出している場合は問題ありませんcancelLocationUpdates()が、アプリを再度開いた後 (強制的に閉じた後)pendingIntentは null であり、removeUpdates を実行できません...それを行う方法はありますか?

4

2 に答える 2

2

私は解決策を見つけました。それは醜いものですが、動作します:

private void cancelLocationUpdates() {
    if(pendingIntent == null) {
        registerLocationUpdates();
    }
    this.locationManager.removeUpdates(pendingIntent);
}

それが役に立てば幸い。

于 2012-05-30T10:05:27.447 に答える
0

以下のように、アクティビティを開始する前に GPS がオンかオフかを確認してください。

LocationManager lm;
boolean gpsOn = false;
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER )) {
    launchGPSOptions();
    if (!gpsOn) launchGPS();
}

以下のように、コードで LaunchGPS および LaunchGPS オプションを使用します。

private void launchGPSOptions() {
    String provider = Settings.Secure.getString(getContentResolver(),
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!provider.contains("gps")) {
        final Intent poke = new Intent();
        gpsOn = true;
        poke.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3"));
        sendBroadcast(poke);
    }
}

private void launchGPS() {
    // final ComponentName toLaunch = new
    // ComponentName("com.android.settings","com.android.settings.SecuritySettings");
    final Intent intent = new Intent(
            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    // intent.setComponent(toLaunch);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(intent, 0);
}
于 2012-05-30T10:11:14.667 に答える