这次的风格犀利点,直接上源码:
1.这里有一个开启媒体播放功能的BroadcastReceiver
- package com.william.BroadcastReceiverTest;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.media.MediaPlayer;
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- MediaPlayer.create(arg0, R.raw.wake_up_call).start();
- }
- }
2.在注册文件对上面的BroadcastReceiver设定了接受系统的包含android.intent.action.BOOT_COMPLETED 类型Action的Intent的属性
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.william.BroadcastReceiverTest"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <receiver android:name="MyReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- </manifest>
这个要注意以下两句的特殊性,想弄懂的话可以去Google上百度下:
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
只需这些操作就可以在开机播放一首歌了。同时,我们更可以利用这个原理来实现我们应用程序的开机启动,具体请自己探索。