How to launch service on boot complete android?

邮差的信 提交于 2019-11-28 04:35:42

问题


I've read some tutorial on launch service on boot. What I've done is:

In manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>

<receiver android:name="my.package.ServiceStartup" >
   <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
   </intent-filter>
</receiver>

CODE:

public class ServiceStartup extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
           @Override
           public void run() {
               Intent dialogIntent = new Intent(getBaseContext(), MyActivity.class);
               dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               getApplication().startActivity(dialogIntent);
           }
        }, 10000);
    }
}

In this way, if i reboot my device and go to setting in active applications, my service is not launched. What can I do? Where I make error? thanks!!


回答1:


You want to start activity or service. In case of service, you will have to call startService(). Like:

getApplication().startService(new Intent(this, MyService.class));




回答2:


Did you run your app? Refer to this tutorial

If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.


Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.



来源:https://stackoverflow.com/questions/12615373/how-to-launch-service-on-boot-complete-android

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