Android-Boot Completed is not working in Broadcastreceiver

强颜欢笑 提交于 2020-01-02 09:55:54

问题


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);
}
}

回答1:


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



来源:https://stackoverflow.com/questions/23491777/android-boot-completed-is-not-working-in-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!