首先,我知道已经有数百个问这样的问题了,但是我已经检查了一段时间,但仍然找不到任何解决方案。
我见过this answer说BOOT_COMPLETED没有发送到应用程序,除非用户首先启动你的应用程序,在Android版本3.1之后但我仍然看到一些应用程序正在这样做,必须有办法。我真的需要处理它,否则我也反对在没有用户交互的情况下做某事。
所以这是我的AndroidManifest:
<manifest ... >
<!-- to be activated service on boot is completed -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application ... >
<!-- to receive data when boot completed -->
<receiver
android:name="myPackage.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
提前致谢。
编辑:在我的广播接收器中没有太多东西要看,但是这里需要的是:
package myPackage
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Utils.LogI("BootReceiver", "BootReceiver received!");
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// Do my stuff
}
}
}
以下这件事对我有用
AndroidManifest.xml中
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application>
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="NotifyingDailyService" >
</service>
BootCompletedReceiver.class
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.w("boot_broadcast_poc", "starting service...");
context.startService(new Intent(context, NotifyingDailyService.class));
}
}
Service.class
public class NotifyingDailyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");
return super.onStartCommand(pIntent, flags, startId);
}
}
这是一个古老而基本的问题,但很多Android开发人员现在仍然对这个问题感到困惑,因为他们没有时间仔细阅读文档
我看到有人分享了一些链接并说: “这将不再适用” ,这是完全错误和误解。
关于这个问题:“我已经看到这个答案说BOOT_COMPLETED没有发送到应用程序,除非用户首先在Android 3.1版之后启动你的应用程序”,请阅读这些行(来自官方文档:https://developer.android.com/about/versions/android-3.1.html#launchcontrols)以正确理解:
FURTHERMORE,直到现在(Android Oreo 8.0),当Android限制在Manifest(https://developer.android.com/about/versions/oreo/background.html#broadcasts)注册隐式广播时,几个广播目前仍然免于这些限制。 BOOT_COMPLETED是他们提到的第一个! (https://developer.android.com/guide/components/broadcast-exceptions.html)
顺便说一下,这是我在这个问题上找到的最佳解决方案:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<!--For HTC devices-->
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
最后,请仔细阅读文档并仔细思考一次代码:3!
一些新的平板电脑和Android设备默认具有安全应用程序。有时这些应用会锁定您的自动启动模式。这个安全应用程序的一个例子是MyAsus经理。所以你可以为你的应用添加“允许自动启动”
而对于Htc设备添加com.htc.intent.action.QUICKBOOT_POWERON
<receiver android:enabled="true" android:name=".receivers.BootUpReceiver">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
问题出在设备上。某些设备仅允许内部应用程序接收此操作(例如:Android 5.1)。
您可以将其添加到您的意图过滤器中作为解决方法
行动android:name="android.intent.action.USER_PRESENT"
用户解锁设备后会触发此操作。
我遇到的问题是,当我从Android 6.0.1面板关闭电源时,BOOT_COMPLETED
和QUICKBOOT_POWERON
并不总是触发我的意图。我一直在互联网上搜索相当长的时间,并通过将QUICKBOOT_POWEROFF
添加到清单文件中找到了解决方案。
也可以看看:
HTC's "fast boot" is not broadcasting BOOT_COMPLETED intent nor wiping intents from alarm manager
要解决此问题,您需要创建NotificationListener服务
如果设备是> = Build.VERSION_CODES.JELLY_BEAN_MR2并且具有华为设备中的电池优化选项,那么您必须要求NotificationListener权限并创建NotificationListener服务,如下面的代码,然后您将在接收器中获得BOOT_COMPLETED
<receiver
android:name=".TestRes"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1">
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class DevAppNotificationListener extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// super.onNotificationPosted(sbn);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// super.onNotificationRemoved(sbn);
}
}
static boolean CheckNotificationLisPermission(Context context)
{
return NotificationManagerCompat.getEnabledListenerPackages (context).contains(context.getApplicationContext().getPackageName());
}
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
context.startActivityForResult(intent, callBackResultIntent);