1

メイン アクティビティ内からブロードキャスト レシーバー (私の場合は近接アラート レシーバー) を作成し、アプリ プロセスがなんらかの理由で強制終了されるとどうなるでしょうか?

アプリの状態に関係なく、登録したブロードキャストレシーバーで近接アラートを受信したいのですが、それは起こりますか、それとも確実にするために何か特別なことをする必要がありますか?

明確にするために編集:

マニフェスト経由ではなく、アプリ内からレシーバーを登録する必要があります。複数の近接アラートが必要なため、(さまざまな) 場所ごとに受信者を動的に作成する必要があります。残念ながら、一意の ID を使用して、場所ごとに受信者を登録する必要があるためです。

インテント/ペンディングインテント/ブロードキャストレシーバーを作成するコード:

    double latitude = location.getLat();
    double longitude = location.getLon();
    Intent intent = new Intent(PROX_ALERT_INTENT_ID);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), 0, intent, 0);
    lm.addProximityAlert(
        latitude, // the latitude of the central point of the alert region
        longitude, // the longitude of the central point of the alert region
        POINT_RADIUS, // the radius of the central point of the alert region, in meters
        PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no                           expiration
        proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
    );

    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT_ID);

    activity.registerReceiver(new ProximityIntentReceiver(location), filter);
4

2 に答える 2

3

アプリの状態に関係なくトリガーしたい場合は、アプリケーションファイルBroadcastReceiversを介して登録する必要があります。AndroidManifest.xml

やり方はこちら。

  1. BroadcastReceiverメソッドを拡張して実装するクラスを定義しonReceive()ます。あなたはすでにそれを行っているようです-それProximityIntentReceiverはそのクラスです。

  2. AndroidManifest.xml ファイルに以下を追加します。

    <application>
    ...
        <receiver
            android:name=".MyReceiver"
            android:exported="false" >
            <intent-filter>
                   <action android:name="my.app.ACTION" />
            </intent-filter>
        </receiver> 
    </application>
    

MyReceiverあなたのレシーバークラスの名前はどこですか(ProximityIntentReceiverあなたの場合)、 my.app.ACTION はレシーバーがリッスンするアクションです(あなたの場合、それは の値だと思いますPROX_ALERT_INTENT_ID)。

注:レシーバーの名前は.MyReceiver、アプリのルート パッケージにあると想定しています。そうでない場合は、ルートから始まるそのクラスへのパスを提供する必要があります。

于 2012-12-29T11:16:26.780 に答える