[英]Activity intent-filter tag in manifest not working as expected
我有一個目標為 API >= 22 的應用程序,在清單中以這種方式配置了一個獨特的單頂活動:
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/Theme.MyApplication.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意上面的android.intent.action.MAIN
字符串。 我有一個這樣配置的雲 (Firebase) 消息服務:
<service
android:name=".MyService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
我的典型使用場景是當我的應用程序在接收消息時已經在前台運行。 收到傳入消息時,將運行我的服務的onMessageReceived
方法,並執行以下操作:
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
[...]
Intent intent = new Intent("com.example.myapplication.myservice");
intent.putExtra("foo", "bar");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode++, intent, PendingIntent.FLAG_ONE_SHOT);
showNotification(
getTitle(),
getBody(),
pendingIntent);
}
請注意上面的"com.example.myapplication.myservice"
操作字符串。 我有一個片段以這種方式監視廣播消息:
IntentFilter filter = new IntentFilter("com.example.myapplication.myservice");
requireActivity().registerReceiver(new MyBroadcastReceiver(), filter);
接收器確實被成功調用,接收到調用onMessageReceived
時創建的消息。
這是正常的,知道我的清單中活動的<intent-filter>
標簽應該只讓android.intent.action.MAIN
意圖操作進入嗎?
是的,這是正常的。 你混淆了兩件事。
清單中的<intent-filter>
聲明控制用於啟動Activity
的Intent
操作。
這與廣播Intent
無關。 Broadcast Intent
用於觸發BroadcastReceiver
,這些與活動無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.