本文来自郭霖大神的《第一行代码》。
鄙人不才,抄作笔记,以供回顾,删改之处,还望见谅。
动态注册的广播接收器可以自由地控制注册和注销,灵活性好,但有一个缺点,即必须要在程序启动后才能接收到广播,因为注册的逻辑是写在onCreate()方法中的.
这里我们采用静态注册的方式让程序接收到一条开机广播,收到这条广播的时候就可以在onReceive()方法里执行相应的逻辑,从而实现开机启动的工功能.可以使用Android Studio 提供的快捷方式创建一个广播接收器.
,右击com.example.broadcasttest包 ->News ->Other ->Broadcast Receiver
这里我们将广播接收器命名为BootCompleteReceiver , Exported属性表示是否允许这个广播接收器接收本程序以外的广播,Enabled属性表示是否启用这个广播接收器,都勾选,Finish.
然后修改BootCompleteReceiver的代码
package com.example.luokexi.broadcasttestdemo2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.widget.Toast;
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show();
}
}
这里这是在onReceive()方法中使用Toast弹出一段提示信息.
另外,静态的广播接收器一定要在AndroidManifest.xml文件中注册才能使用,不过我们使用的是Android Studio 的快捷方式创建的广播接收器,因此注册这一步已经自动完成.打开AndroidManifest.xml查看下.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luokexi.broadcasttestdemo2">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android: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=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
receiver>
application>
manifest>
可以看到 application标签内出现了一个新的标签 receiver ,所有的静态的广播器都是在这里进行注册的. 用法和 activity 标签非常类似. 也是通过android:name来指定具体注册哪一个广播接收器,而enabled和exported属性则是根据我们刚才勾选的状态自动生成的.
不过目前的 BootCompleteReceiver还是不能接受到开机广播的,我们还需要对AndroidManifest.xml文件进行修改:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luokexi.broadcasttestdemo2">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android: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=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
intent-filter>
receiver>
application>
manifest>
由于Android系统启动完成后会发出一条值为android.intent.action.BOOT_COMPLETED的广播,因此我们在 intent-filter 标签里添加了响应的 action .另外,监听系统开机广播也是需要声明权限的,可以看到我们使用 uses - permission 标签 又加入了一条 android.permission.RECEIVE_BOOT_COMPLETED权限.
现在我们试试看. 运行程序后,将模拟器关闭,重新启动,在启动完成后就会收到开机广播.
我们在广播接收器的onReceive()方法中都只是简单地使用Toast提示了一段文本信息,当你真正在项目中使用它的时候,就可以在里面编写自己的逻辑.需要注意的是不要在onReceive()方法中添加过多的逻辑或者进行任何耗时的操作,因为在广播接收器中是不允许开启线程的,当onReceive()方法运行较长时间而没有结束时,程序就会报错.因此广播接收器更多的是扮演一种打开程序其他组件的角色,比如创建一条状态栏通知,或者启动一个服务等.