BootReceiver类无法正常工作,并在手动重启时关闭

时间:2015-12-19 11:45:51

标签: android

我想构建一个启动接收器。 的manifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver">
            <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

这是我的Receiver Class:

public class BootReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.e("-->", "BOOT COMPLETED");
        }
}

如果我重启设备,我会收到消息(在我的设备上):

  

app xxx已关闭

1 个答案:

答案 0 :(得分:0)

尝试在.manifest中更改此内容

 <receiver android:name=".BootReceiver"
   android:enabled="true"
   android:exported="false"
   android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
 </receiver>

也可以在Reciever中试试这个

public class BootReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        Toast.makeText(context, "Receiver Called", Toast.LENGTH_LONG).show();
    }
}

另外将android:persistent =“true”添加到清单标记