Android的引导完成未在广播接收器工作 [英] Android-Boot Completed is not working in Broadcastreceiver
问题描述
我使用的Android(4.1.1版本)迈乐盒(的SmartTV)用于开发一个应用程序,我要开始我的应用程序时,设备开机时间完成,但我的设备没有赶上BOOT_COMPLETED行动。如果我使用,在手机或模拟器Boot_Completion动作相同的应用程序被Broadcast_receiver抓住了。
如果有谁知道关于这个问题,帮助我在此先感谢....
这是我的code ...
清单:
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>
块引用><接收机器人:名字=。BootCompletedReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
<作用机器人:名字=android.intent.action.QUICKBOOT_POWERON/>
&所述; /意图滤光器>
< /接收器><服务机器人:名字=NotifyingDailyService>
< /服务>BootCompletedReceiver类:
公共类BootCompletedReceiver扩展广播接收器{
@覆盖
公共无效的onReceive(上下文的背景下,意图ARG1){
// TODO自动生成方法存根
Log.w(boot_broadcast_poc,启动服务...);
context.startService(新意图(上下文,NotifyingDailyService.class));
}
}服务类:
公共类NotifyingDailyService延伸服务{@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}@覆盖
公众诠释onStartCommand(意向pIntent,诠释标志诠释startId){
// TODO自动生成方法存根
Toast.makeText(这一点,NotifyingDailyService,Toast.LENGTH_LONG).show();
Log.i(com.example.bootbroadcastpoc,NotifyingDailyService);返回super.onStartCommand(意向,旗帜,startId);
}
}
解决方案有一件事我注意到的是你没有的类别集你在你的清单接收器。下面对我的作品在我的应用程序。
<接收机器人:名字=us.nineworlds.serenity.StartupBroadcastReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
<类机器人:名字=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /接收器>然后在我的StartupBroadcastReceiver,我有以下
公共无效的onReceive(上下文的背景下,意图意图){ 如果(intent.getAction()== NULL){
返回;
} 如果(intent.getAction()。等于(android.intent.action.BOOT_COMPLETED)){
共享preferences preFS = preferenceManager.getDefaultShared preferences(背景);
布尔startupAfterBoot = prefs.getBoolean(serenity_boot_startup,FALSE);
如果(startupAfterBoot){
意图I =新意图(背景下,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ⅰ);
}
}
}一旦启动完成这将启动MainActivity类别。
链接到该项目code是在这里:<一href=\"https://github.com/NineWorlds/serenity-android/blob/master/serenity-app/src/main/java/us/nineworlds/serenity/StartupBroadcastReceiver.java\" rel=\"nofollow\">https://github.com/NineWorlds/serenity-android/blob/master/serenity-app/src/main/java/us/nineworlds/serenity/StartupBroadcastReceiver.java
I'm using android(version 4.1.1) MeLE box(SmartTv) for developing one application, i need to start up my application when device Boot Time is completed but my device not catch up the BOOT_COMPLETED Action. If i'm use that same application in mobiles or emulator the Boot_Completion action were caught by Broadcast_receiver.
if anyone known about this issue help me thanks in advance....
here is my code...
manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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(intent, flags, startId); } }
解决方案One thing I noticed is you don't have the category set for your receiver in your Manifest. The following works for me in my App.
<receiver android:name="us.nineworlds.serenity.StartupBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
Then in my StartupBroadcastReceiver, i have the following
public void onReceive(Context context, Intent intent) { if (intent.getAction() == null) { return; } if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean startupAfterBoot = prefs.getBoolean("serenity_boot_startup", false); if (startupAfterBoot) { Intent i = new Intent(context, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } }
This will start the MainActivity class once bootup has been completed.
Link to the project code is here: https://github.com/NineWorlds/serenity-android/blob/master/serenity-app/src/main/java/us/nineworlds/serenity/StartupBroadcastReceiver.java
这篇关于Android的引导完成未在广播接收器工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!