Auto Start Service after booting device even app not opened atonce. Android

不想你离开。 提交于 2020-01-15 09:55:31

问题


I am making a system app. In that I have a requirement is to run a service after boot load WITHOUT A SINGLE TIME LUNCHING THE APP. this question is bit similar to this System App auto starting But it does not have any appropriate solution. Also read that BOOT_COMPLETE_RECEIVER works only when app launched at once.


回答1:


Use Broadcast Receiver for getting action after that start service from that broad cast receiver and use START_STICKY service so that if it is killed because of some priority than it's recreate and if you want to continuously run this service in background than WAKE_Lock that service and using Alarm Manager check it is runnig or not.




回答2:


Set this in manifest

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

<receiver
 android:name="AutoStart"
 android:enabled="true"
 android:exported="false">
    <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
 </receiver>

AutoStart class

public class AutoStart extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
        // Start your service here..
    }
  }
}



回答3:


Thanks all for your effort, I finally got answer. Solution: As I stated my app is system app, System work even they not opened at once. Because they are not in stopped state i.e enforce after android 3.1.

Secondly If a user app wants this then Its manifest don't have any "android.intent.category.LAUNCHER" category in activity.

Also by adb you can enable your app by using this command adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages (This is not tested)

Some good link to this: http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/ Static BroadcastReceiver not Working after Installation from ADB



来源:https://stackoverflow.com/questions/40149797/auto-start-service-after-booting-device-even-app-not-opened-atonce-android

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