歡迎大家在這裡學習android接收系統自帶的廣播!下面是我們給大家整理出來的精彩內容。希望大家在這裡學習!
1、建立一個項目Lesson21_BroadcastReceiver,拷貝一首音樂進res/raw目錄
2、建立HelloBroadcastReceiver.java 內容如下:
package android.basic.lesson21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
public class HelloBroadReciever extends BroadcastReceiver {
//如果接收的事件發生
@Override
public void onReceive(Context context, Intent intent) {
//則輸出日志
Log.e("HelloBroadReciever", "BOOT_COMPLETED!!!!!!!!!!!!!!!!!!!!!!!!!");
Log.e("HelloBroadReciever", ""+intent.getAction());
//則播放一首音樂
MediaPlayer.create(context, R.raw.babayetu).start();
}
}
- package android.basic.lesson21;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.util.Log;
3、在AndroidManifest.xml中注冊此Receiver
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionname="1.0" android:versioncode="1" package="android.basic.lesson21">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:label="@string/app_name" android:name=".MainBroadcastReceiver">
- <intent -filter="">
- <action android:name="android.intent.action.MAIN">
- <category android:name="android.intent.category.LAUNCHER">
- </category></action></intent>
- </activity>
- <!-- 定義Broadcast Receiver 指定監聽的Action -->
- <receiver android:name="HelloBroadReciever">
- <intent -filter="">
- <action android:name="android.intent.action.BOOT_COMPLETED">
- </action></intent>
- </receiver>
- </application></manifest>
好了,android接收系統自帶的廣播精彩內容就給大家介紹到這裡了。希望大家繼續關注我們的網站!
相關推薦:
android使用Content Provider的方法