6

ここでは、Eclipse の 2 つの異なるプロジェクトに 2 つのアプリケーションがあります。1 つのアプリケーション (A) は、最初に開始されるアクティビティ (A1) を定義します。次に、このアクティビティから、2 番目のプロジェクト (B) の 2 番目のアクティビティ (B1) を開始します。これはうまくいきます。

次の方法で開始します。

Intent intent = new Intent("pacman.intent.action.Launch");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

ここで、ブロードキャスト レシーバーを使用して、2 つのアクティビティ間でインテントを送信したいと考えています。アクティビティ A1 では、次の方法でインテントを送信します。

Intent intent = new Intent("pacman.intent.action.BROADCAST");
intent.putExtra("message","Wake up.");
sendBroadcast(intent);

このブロードキャストを担当するアクティビティ A1 のマニフェスト ファイルの部分は次のとおりです。

<activity android:name="ch.ifi.csg.games4blue.games.pacman.controller.PacmanGame" android:label="@string/app_name">
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
       <action android:name="android.intent.action.BROADCAST" />
    </intent-filter>
</activity>

受信アクティビティでは、マニフェスト ファイルで次のように受信者を定義します。

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PacmanGame"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="pacman.intent.action.Launch" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <receiver android:name="ch.ifi.csg.games4blue.games.pacman.controller.MsgListener" />
        </activity>

    </application>

クラス メッセージ リスナは次のように実装されます。

public class MsgListener extends BroadcastReceiver {

    /* (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("Message at Pacman received!");
    }

}

残念ながら、メッセージは受信されません。アクティビティ A1 のメソッドが呼び出されますが、B1 でインテントを受け取ることはありません。

これを解決するためのヒントはありますか?どうもありがとう!

4

4 に答える 4

14
  1. 要素は、子要素ではなく、要素<receiver>のピアである必要があります。<activity>
  2. Google で働いている場合を除き、アクション文字列を名前空間に含めないでください。代わりにまたはそのようなものを使用してください。android.intent.actionch.ifi.csg.games4blue.games.pacman.controller.BROADCAST
  3. あなたのカスタムアクションは、送信または受信ではなく、<intent-filter>に配置する必要があります<receiver><activity>

マニフェストに登録されたブロードキャスト レシーバーの実装例については、こちらを参照してください(システム ブロードキャスト インテント用)。

于 2010-05-01T12:38:02.083 に答える
2

Intent intent = new Intent("pacman.intent.action.BROADCAST");

対。

<android:name="android.intent.action.BROADCAST"/>

実際のコードで同じ文字列を使用してもよろしいですか?

于 2011-06-29T20:51:13.123 に答える
1

Androidで渡すアクションが何であれ、IntentオブジェクトまたはIntentのsetAction()メソッドを作成するときに同じアクションを使用する必要があります。ContextのsendBroadcasteReceiver()メソッドを使用してこのIntentオブジェクトを送信すると、このアクションがすべてのレシーバー(許可なし)に送信されます。Manifest.xmlで設定したレシーバーはすべて(インテントで同じアクションを持ちます) -filter tag)このアクションを取得します。

于 2011-07-29T11:39:45.233 に答える
0

まだうまくいきませんか?

答えは役に立ちますが、まだ問題がありました。ここで解決策を得ました。

ブロードキャストを送信するときは、ff フラグを追加します。

FLAG_INCLUDE_STOPPED_PACKAGES フラグは、送信前にインテントに追加され、インテントが停止したアプリケーションのコンポーネントを開始できることを示します。

intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
于 2015-11-06T14:27:01.520 に答える