3

私が取り組んでいるアプリケーションでは、機能の 1 つは、ユーザーが以前に設定した場所に到達したときにユーザーに通知することです。

以下のコードは、アクティビティの addProximityAlert にあります。

final Intent intent = new Intent(PROX_ALERT_INTENT);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(
        InfoActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
LocationManager locationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
locationManager.addProximityAlert(18.7726271, 98.9738381, 5000, -1,
        pendingIntent);
this.locationReminderReceiver = new LocationReminderReceiver();
final IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
this.registerReceiver(this.locationReminderReceiver, filter);

@Override
protected void onPause() {
    super.onPause();
    if (this.locationReminderReceiver != null) {
        Log.i("unregisterReceiver", "unregisterReceiver");
        this.unregisterReceiver(this.locationReminderReceiver);
    }
}

受信機は次のとおりです。

public class LocationReminderReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    final String key = LocationManager.KEY_PROXIMITY_ENTERING;
    final Boolean entering = intent.getBooleanExtra(key, false);

    if (entering) {
        Toast.makeText(context, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show();
        Log.i("LocationReminderReceiver", "entering");
    } else {
        Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show();
        Log.i("LocationReminderReceiver", "exiting");
    }
}
}

unregisterReceiver正常に動作しますが、アクティビティを破棄するたびに呼び出す必要があります。つまり、アプリケーションはユーザーに通知しなくなります。しかし、ユーザーがキャンセルするまで、またはアプリを閉じても既に通知されるまで、その場所の近くにいるときにユーザーに通知したい.

私は何が欠けていますか?

4

2 に答える 2

3

ユーザーがアクティビティを閉じた場合は、実際にロケーションリスナーの登録を解除する必要があります。

ユーザーがアプリを閉じた後も実行を継続できるように、アプリケーションの一部(位置を監視してユーザーに警告するビット)をバックグラウンドサービスに移動する必要があるようです。

于 2012-09-28T11:01:13.297 に答える
2

マニフェストでレシーバーを定義するだけです

その後、アクティビティに登録/登録解除する必要はありません

  <receiver android:name="th.clbs.android.broadcastreceiver.LocationReminderReceiver" >
        <intent-filter>
            <action android:name="th.co.clbs.action.LOCATION_REMINDER" />
        </intent-filter>
    </receiver>
于 2012-10-25T05:54:16.260 に答える