在Android Manifest文件中註冊。
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="marakana.intent.action.ReceiverDemo" />
</intent-filter>
</receiver>
註冊程序。
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Create the receiver
receiver = new TimelineReceiver();
filter = new IntentFilter(UpdaterService.NEW_STATUS_INTENT);
}
protected void onResume() {
super.onResume();
super.registerReceiver(receiver, filter,
"com.marakana.yamba.SEND_TIMELINE_NOTIFICATIONS", null);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
...
UPDATE:多個值 如果可以指定一個以上的值,該元件將被重複,而不是單一的元素裏列出的多個值。例如,一個意圖過濾器可以列出幾個動作:
<intent-filter . . . >
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.INSERT" />
<action android:name="android.intent.action.DELETE" />
. . .
</intent-filter>
UPDATE2:這就是AndroidManifest.xml中
<manifest
package="com.marakana.android.lifecycle"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application
android:name=".ApplicationDemo"
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".ActivityDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity"></activity>
<activity android:name=".SystemServicesDemo"></activity>
<service android:name=".ServiceDemo"></service>
<service android:name=".IntentServiceDemo">
<intent-filter>
<action android:name="marakana.intent.action.IntentServiceDemo" />
</intent-filter>
</service>
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="marakana.intent.action.ReceiverDemo" />
</intent-filter>
</receiver>
<provider
android:name=".ProviderDemo"
android:authorities="com.marakana.android.lifecycle.providerdemo" />
</application>
</manifest>
我還是不太明白,marakana.intent.action.ReceiverDemo代表什麼? – Jolin 2013-05-07 05:30:44
上面更新了我的答案。 – NMALKO 2013-05-07 05:35:56
是的..但在我的情況下,我應該如何接收這個意圖'Intent in = new Intent(PlAY_FINISHED);'?這是我想知道的... – Jolin 2013-05-07 05:38:10