BroadcastReceiver未收到BOOT_COMPLETED
我在这里寻找类似的问题,但由于某种原因,我的 BroadcastReceiver 永远不会最终收到 android.intent.action.BOOT_COMPLETED Intent。
这是我的(相对)Android.Manifest 文件:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
这是实际的接收器。
public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";
@Override public void onReceive(Context context,Intent intent){
try{
context.startService(new Intent(context,ConnectivityListener.class));
Log.i(TAG,"Starting Service ConnectivityListener");
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}
谢谢!非常感谢任何帮助
I've looked around here for similiar problems, but for some reason my BroadcastReceiver never ends up receiving the android.intent.action.BOOT_COMPLETED Intent.
Here is my (relative) Android.Manifest File:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
And Here is the actual Receiver.
public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";
@Override public void onReceive(Context context,Intent intent){
try{
context.startService(new Intent(context,ConnectivityListener.class));
Log.i(TAG,"Starting Service ConnectivityListener");
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}
Thanks! Any help is greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以通过 adb 连接到设备并打开设备 shell 来模拟所有广播操作。
我们开始吧:
adb shell
或在 linux/mac 上./adb shell
am Broadcast -a android.intent.action.BOOT_COMPLETED
或您想要触发的任何操作adb 或 adb shell 附带了很多不错的命令。只是尝试一下
问候
弗洛
编辑:哦该死,我想要这个答案作为“每次都必须打开/关闭手机”的答案。对不起各位
You can emulate all broadcast actions by connecting via adb to the device and open a device shell.
Here we go:
adb shell
or on linux/mac./adb shell
am broadcast -a android.intent.action.BOOT_COMPLETED
or whatever action you want to fireThere are a bunch of nice commands coming with adb or the adb shell. Just try it
Regards
Flo
edit: oh damn, i wanted this answer as an answer on the "had to turn phone on/off every time". sorry folks
我发布此内容是希望对尝试了所有方法但在安装后仍无法在启动时运行或以前可以工作但现在不再工作的人有所帮助。
因此,假设您已添加权限:
并注册了您的接收器:
并对您的
BroadcastReceiver
进行了编码:从 Android 3.1 开始,所有应用程序在安装时都会放置在“已停止”状态。(这与用户从“设置”应用程序强制停止应用程序后应用程序最终所处的状态相同。)
当处于“停止”状态时,应用程序不会因任何原因运行,除非手动启动 Activity。 (意味着不会调用
BroadcastRecevier
(ACTION_PACKAGE_INSTALLED
、BOOT_COMPLETED
等,无论它们注册了哪个事件,直到用户手动运行应用程序。)这是 Google 为防止恶意软件应用程序而做出的设计决定。Google 主张用户应先从启动器启动一个 Activity,然后再阻止该应用程序执行
BOOT_COMPLETED<。 /code> 从交付到活动启动是该参数的逻辑结果。
一旦用户在您的应用程序中运行任何活动一次,您将在以后的所有启动后收到 BOOT_COMPLETED 广播:
http://developer.android.com/about/versions/android-3.1 .html#launchcontrols
http://commonsware.com/blog/2011/07/05 /boot-completed-regression.html
http://devmaze.wordpress.com/2011/12/05/activating-应用程序/
I'm posting this in the hope that it will be helpful to someone who has tried everything but still cannot get it to run on boot after installation or it used to work before and doesn't work anymore.
So assuming you have added the permission:
And registered your receiver:
And coded your
BroadcastReceiver
:Starting with Android 3.1 all applications, upon installation, are placed in a "stopped" state.(This is the same state that the application ends up in after the user force-stops the app from the Settings application.)
While in "stopped" state, the application will not run for any reason, except by a manual launch of an activity. (Meaning no
BroadcastRecevier
(ACTION_PACKAGE_INSTALLED
,BOOT_COMPLETED
etc. will be invoked, regardless of the event for which they have registered, until the user runs the app manually.)This is a design decision by Google to prevent malware apps. Google has advocated that users should launch an activity from the launcher first, before that application can do much. Preventing
BOOT_COMPLETED
from being delivered until the activity is launched is a logical consequence of that argument.Once a user runs any activity in your app once, you will receive the BOOT_COMPLETED broadcast after all future boots.
More details about this:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
http://devmaze.wordpress.com/2011/12/05/activating-applications/
如果您的应用安装在外部存储(SD 卡)上,您将永远不会收到启动完成操作。因此,您必须在
manifest 标记
中指定android:installLocation="internalOnly"
。If your app installed on external storage(SD card), you will never receive Boot Complete action. So you have to specify
android:installLocation="internalOnly"
in themanifest tag
.您的
元素需要是
元素的直接子元素,而上面列出的代码表明它不是。这是一个示例项目,演示了
BOOT_COMPLETED
的使用。Your
<uses-permission>
element needs to be an immediate child of the<manifest>
element, and your code listing above suggests that it is not.Here is a sample project demonstrating the use of
BOOT_COMPLETED
.结果发现接收者不在清单的标签中。哎呀!谢谢你们的帮助!测试这一点最糟糕的部分是必须不断关闭和打开电话。 :P
Turns out the receiver wasn't in the tag of the manifest. Whoops! Thanks for your help guys! The worst part about testing this is having to keep turning off and on the phone. :P
这似乎是这个问题的最前沿线程,所以我想为我的 C# 同事添加一个解决方案。在尝试了这里的一切之后,我绞尽脑汁试图找出我做错了什么,但无济于事。我终于弄清楚出了什么问题,它与这里针对 C# Mono 开发的建议有点不同。基本上,它可以归结为我刚刚通过艰难的方式学到的东西。使用 C# 不要手动修改 AndroidManifest.xml!
请参阅本指南以供参考:
Xamarin:使用 AndroidManifest.xml
更直接地解决此问题,请参见此处这就是你完成这件事的方法。
首先,在项目属性中的“清单”选项卡下,有一个复选框列表,用于选择要提供的权限,其中之一是 RECEIVE_BOOT_COMPLETED。检查以提供这些权限。
其次,您需要在 BroacastReceiver 类上放置正确的标签。
[IntentFilter()] 处理优先级的最后部分不是必需的,它只是让其他更高优先级的事情在启动时首先完成,如果您的应用程序不是高优先级的事情,那么这是一个很好的做法。
正如您将在链接的文章中看到的,在代码中使用这些标签将导致在构建时创建 AndroidManifest.xml 文件,一切都应如此。我发现,当手动修改清单以包含接收者标记时,系统导致它查找太深的一级类,从而引发 ClassNotFound 异常。它试图实例化 [Namespace].[Namespace].[BroadcastReceiver] 这是错误的。这样做是因为手动清单编辑。
无论如何,希望这会有所帮助。
另外,还有一个关于 adb 工具的快速提示。如果您想获得更易于阅读的日志版本,请尝试以下操作:
C:\Android\platform-tools\adb logcat>> C:\log.txt
这会将 logcat 转储到一个文本文件,您可以比在命令提示符窗口中更轻松地打开和阅读该文件。也使剪切和粘贴内容变得更加容易。
This seems to be the forefront thread for this problem, so I wanted to add a solution for my C# colleagues. I racked my brain trying to figure out what I was doing wrong after trying everything here, to no avail. I finally figure out what was wrong, and it differs a bit from the advice here for C# Mono development. Basically, it boils down to something I've just learned the hard way. With C# DO NOT MODIFY AndroidManifest.xml manually!
See this guide for reference:
Xamarin: Working with AndroidManifest.xml
More directly for this problem, here is how you get this done.
First, in your project properties, under the Manifest Tab, there is a checkbox list for choosing the permissions you want to provide, one of which is RECEIVE_BOOT_COMPLETED. Check that to provide these permissions.
Secondly, you need to put the proper tags on your BroacastReceiver class.
The final part of [IntentFilter()] dealing with priority isn't required, it just lets other higher priority stuff get done first on boot, and is good practice if your App isn't a high priority thing.
As you'll see in the linked article, using these tags in your code will cause the AndroidManifest.xml file to be created at build time, with everything the way it should be. What I found was that when modifying the manifest manually to include the receiver tag, the system was causing it to look for the class one level too deep, thus throwing a ClassNotFound exception. It was trying to instantiate [Namespace].[Namespace].[BroadcastReceiver] which was wrong. And it was doing that because of the manual manifest edits.
Anyway, hope this helps.
Also, another quick tip with the adb tool. If you want to get an easier to read version of the log, try this:
C:\Android\platform-tools\adb logcat >> C:\log.txt
This will dump the logcat to a text file you can open and read a bit easier than in the command prompt window. Makes cut and paste of things a bit easier too.
与某些运行 Android Kitkat 4.4.4_r2/r1 的设备有关。
Android 中似乎存在一个错误,导致 android.intent.action.BOOT_COMPLETED 无法广播。
请参阅:
启动失败,导致 Package Manager 服务准备就绪
在大多数情况下,这不是你的问题的答案(更可能是因为权限等),但如果你正在运行 Kitkat 那么你可能会看一下,看看这是否适合你。
我遇到了这个问题,并且 android.intent.action.BOOT_COMPLETED 在它启动的某些时候根本不会被广播!
Pertaining to some devices running Android Kitkat 4.4.4_r2/r1.
There seems to be a bug in Android that make android.intent.action.BOOT_COMPLETED no being broadcasted.
See:
BOOT FAILURE making Package Manager Service ready
In most cases this is not the answer to your problems (more likely because permissions etc), but if you are running Kitkat then you might have a look and see if this seems to be the case for you.
I had this problem and android.intent.action.BOOT_COMPLETED would simply not be broadcasted some of the times it had started up!
这里的其他答案已经涵盖了如何完美地实现广播接收器以便它能够工作,但是我仍然在接收 BOOT_COMPLETED 意图时遇到问题,直到我意识到通过按应用程序图标从手机/模拟器启动时该应用程序实际上正在工作。
每当我使用 Android Studio 中的调试/运行命令启动应用程序时,BOOT_COMPLETED Intent 都不会被传递,除非应用程序已打开并运行。
我希望这可以帮助那些像我一样在这个问题上挣扎了几个小时的人。
此外,如果有人对这种行为有解释,我很乐意了解更多信息。
Other answers here already covered how to perfectly implement the Broadcast Receiver so that it'll work, however I still had problems receiving the BOOT_COMPLETED Intent until I realized the app was actually working when started from the phone/emulator by pressing on the app icon.
Whenever I start my app with the debug/run commands from Android Studio the BOOT_COMPLETED Intent won't be delivered, unless the app is opened and running.
I hope this can help someone who, like me, was struggling for hours with this problem.
Moreover, if anyone has an explanation for this behavior, I'd be really happy to know more about it.
将
添加到我的清单文件中解决了我的问题并有效。on adding
<category android:name="android.intent.category.HOME" />
this to my manifest file solve my problem and works.