<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.permissionbroadcastreceiver">
<permissionandroid:name="com.example.broadcast.permission"
android:protectionLevel="normal" />
<applicationandroid:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PermissionRecevicer"
android:permission="com.example.broadcast.permission">
<intent-filter>
<action android:name="com.example.permissionbroadcastreceiver.message" />
</intent-filter>
</receiver>
</application></manifest>123456789101112131415161718192021222324252627282930
在清单文件中声明一个权限,然后在receiver中要求发送者具有此权限,这样广播接受者进程就算是准备完成了!!
广播发送者的清单文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.permissionbroadcast">
<uses-permission android:name="com.example.broadcast.permission" />
<applicationandroid:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application></manifest>12345678910111213141516171819202122
在清单文件请求刚才在接收者清单文件声明的权限即可,这里的运行结果就不展示了,只是log而已!!!
Android接收开机广播,需要用到播广播接收者BroadcastReceiver组件。
具体代码:
在配置文件AndroidManifest.xml中向系统注册receiver
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
需要添加相应权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
在Receiver中就可以添加开机需要进行的 *** 作
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)