BOOT_COMPLETED 无法运行 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20441308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BOOT_COMPLETED not working Android
提问by yahya
First of all, i know there has been hundreds of this kind of question asked, but i've been checking them all for a while and still couldn't find any solution.
首先,我知道已经有数百个这样的问题被问到,但我已经检查了一段时间,仍然找不到任何解决方案。
I've seen this answersaid BOOT_COMPLETED not send to application unless user launch your application first, after Android version 3.1 But i still see some applications are doing that, there must be a way. I really need to handle it, otherwise i'm also against to do something without user's interaction.
我已经看到这个答案说 BOOT_COMPLETED 不会发送到应用程序,除非用户首先启动您的应用程序,在 Android 3.1 版之后但我仍然看到一些应用程序正在这样做,必须有办法。我真的需要处理它,否则我也反对在没有用户交互的情况下做一些事情。
So here's my AndroidManifest:
所以这是我的 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>
Thanks in advance.
提前致谢。
Edit:There is no much thing to see in my broadcastreceiver but to whom required here it is:
编辑:在我的广播接收器中没有什么可看的,但这里需要的是:
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
}
}
}
回答by Piyush
This below thing worked for me
下面这件事对我有用
AndroidManifest.xml
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
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);
}
}
回答by Think Twice Code Once
This is an old and basic question but a lot of Android developers now still confused about this trouble, because THEY DON'T TAKE TIME TO READ THE DOCS CAREFULLY
这是一个古老而基本的问题,但现在很多 Android 开发人员仍然对这个问题感到困惑,因为他们没有花时间仔细阅读文档
I saw someone shared some links and said that: "This won't work anymore", it's totally wrongand misunderstood.
我看到有人分享了一些链接并说: “这已经行不通了”,这是完全错误和误解。
About this concern: "I've seen this answer said BOOT_COMPLETED is not sent to the application unless the user launches your application first, after Android version 3.1", please read these lines (from official docs: https://developer.android.com/about/versions/android-3.1.html#launchcontrols) to understand correctly:
关于这个问题:“我已经看到这个答案说 BOOT_COMPLETED 不会发送到应用程序,除非用户在 Android 3.1 版之后首先启动您的应用程序”,请阅读这些行(来自官方文档:https: //developer.android. com/about/versions/android-3.1.html#launchcontrols) 以正确理解:
Note that an application's stopped stateis not the same as an Activity's stopped state. The system manages those two stopped states separately.
Applications are in a stopped state when they are first installed but are not yet launchedand when they are manually stopped by the user (in Manage Applications).(They mean force stopan app)
请注意,应用程序的停止状态与活动的停止状态不同。系统分别管理这两种停止状态。
应用程序在首次安装但尚未启动时以及用户手动停止时(在Manage Applications 中)处于停止状态。(它们的意思是强制停止应用程序)
That means a user should launch app at least once after installationto activate the application, then the app can receive implicit broadcasts from OS as normal. (Just only one time launching ever !)
"Does any app that gets installed and never open even only one time ever?", yep, it 's spam and scam apps, this technique helps user to prevent that!
这意味着用户应该在安装后至少启动一次应用程序以激活应用程序,然后应用程序可以正常接收来自操作系统的隐式广播。(只有一次启动!)
“是否有任何应用程序安装后从未打开过,甚至只打开过一次?” ,是的,这是垃圾邮件和诈骗应用程序,这种技术可以帮助用户防止!
FURTHERMORE, UNTIL NOW (Android Oreo 8.0), when Android limits registering implicit broadcasts at Manifest (https://developer.android.com/about/versions/oreo/background.html#broadcasts), several broadcasts are still currently exempted from these limitations. And BOOT_COMPLETEDis the first onethey mention !(https://developer.android.com/guide/components/broadcast-exceptions.html)
此外,直到现在(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)
By the way, this is the best solution I found for this question:
顺便说一句,这是我为这个问题找到的最佳解决方案:
<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>
Finally, please read the document carefully and Think twice code once :3!
最后,请仔细阅读文档并三思而后行:3!
回答by Jawad Zeb
And for Htc devicesadd com.htc.intent.action.QUICKBOOT_POWERON
对于 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>
回答by Gà Và Sói
The problem is with the device. some devices only allow internal apps to receive this action(example: Android 5.1).
问题出在设备上。某些设备仅允许内部应用接收此操作(例如:Android 5.1)。
you can add this to your intent filter as work around
您可以将其添加到您的意图过滤器中作为解决方法
action android:name="android.intent.action.USER_PRESENT"
行动 android:name="android.intent.action.USER_PRESENT"
This is triggered after the user unlocks the device.
这是在用户解锁设备后触发的。
回答by iman kazemayni
some new tablets and android devices have security application by default. some times these apps lock your auto-start mode. an example of this secure app is MyAsus manager. so you can add "allow auto start" to your apps
一些新的平板电脑和安卓设备默认有安全应用程序。有时这些应用程序会锁定您的自动启动模式。这个安全应用程序的一个例子是 MyAsus manager。所以你可以在你的应用程序中添加“允许自动启动”
回答by Willem Luijk
The problem I experienced was that BOOT_COMPLETED
and QUICKBOOT_POWERON
together does not always triggered my intent when I switched the power off from my Android 6.0.1 panel. I have been searching the Internet for quite a long time and found the solution by adding QUICKBOOT_POWEROFF
to the manifest file.
我遇到的问题是,BOOT_COMPLETED
和QUICKBOOT_POWERON
在一起并不总是触发了我的意图,当我打开电源从我的Android 6.0.1面板。我在网上搜索了很长时间,通过添加QUICKBOOT_POWEROFF
到清单文件找到了解决方案。
See also:
也可以看看:
HTC's "fast boot" is not broadcasting BOOT_COMPLETED intent nor wiping intents from alarm manager
回答by dannyskim
For others that are still having an issue with this like I did, if you're debugging on a device that has a boot lock (pin, pattern, or otherwise), OS versions >= 7.0 need to subscribe to the android.intent.action.LOCKED_BOOT_COMPLETED
as illustrated below:
对于像我一样仍然有此问题的其他人,如果您在具有引导锁定(引脚、模式或其他)的设备上进行调试,操作系统版本 >= 7.0 需要订阅android.intent.action.LOCKED_BOOT_COMPLETED
如下所示:
<receiver
android:directBootAware="true"
android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
You can find the documentation at the following link: https://developer.android.com/training/articles/direct-boot
您可以在以下链接中找到文档:https: //developer.android.com/training/articles/direct-boot
回答by ahmadzuhair
For workaround you need to create a NotificationListener service
对于解决方法,您需要创建一个 NotificationListener 服务
if the Device is >= Build.VERSION_CODES.JELLY_BEAN_MR2 and having a battery optimization option as in Huawei devices, then you have to ask for NotificationListener permission and create a NotificationListener service as in below code, then you will get the BOOT_COMPLETED in your receiver
如果设备 >= Build.VERSION_CODES.JELLY_BEAN_MR2 并且像华为设备一样有电池优化选项,那么你必须请求 NotificationListener 权限并创建一个 NotificationListener 服务,如下面的代码,那么你将在你的接收器中得到 BOOT_COMPLETED
Manifest Receiver :
清单接收器:
<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>
1 create a NotificationListener service :
1 创建一个 NotificationListener 服务:
@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);
}
}
2 check if the NotificationListener is granted or not :
2 检查 NotificationListener 是否被授予:
static boolean CheckNotificationLisPermission(Context context)
{
return NotificationManagerCompat.getEnabledListenerPackages (context).contains(context.getApplicationContext().getPackageName());
}
3 if not then ask for NotificationListener permission :
3 如果没有,则请求 NotificationListener 权限:
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
context.startActivityForResult(intent, callBackResultIntent);