Is there any way to run service continuously?

前端 未结 3 1370
小蘑菇
小蘑菇 2020-11-30 08:27

There are few questions similar to this on Stack Overflow but none of the solutions are working for me

The problem is with only few devices like OnePlus and MI, The

相关标签:
3条回答
  • 2020-11-30 08:53

    The right way to listen to location on the background of to use a fused location API from play services.

    Look at the samples here. https://github.com/googlesamples/android-play-location?files=1

    These API's are power efficient, and I would recommend it too.

    0 讨论(0)
  • 2020-11-30 09:00

    If you go through THE LINK, you will find:

    Unfortunately, some devices implement killing the app from the recents menu as a force stop. Stock Android does not do this. When an app is force stopped, it cannot execute jobs, receive alarms or broadcasts, etc. So unfortunately, it's infeasible for us to address it - the problem lies in the OS and there is no workaround.

    It is a known issue. To save battery, many manufacturers force close the app, thus cancelling all the period tasks, alarms, and broadcast receivers etc. Major manufacturers being OnePlus (you have option to toogle), Redmi, Vivo, Oppo, Huwaei.

    Each of these devices have AutoStartManagers/AutoLaunch/StartManager type of optimization managers. Which prevent the background activities to start again. You will have to manually ask the user to whitelist your application, so that app can auto start its background processess. Follow THIS and THIS link, for more info.

    The methods to add to whitelist for different manufactures are give in this Stack Overflow answer. Even after adding to whitelist, your app might not work because of DOZE Mode, for that you will have to ignore battery otimizations

    Also in case you might be wondering, apps like Gmail/Hangout/WhatsApp/Slack/LinkedIn etc. are already whitelisted by these AutoStart Managers. Hence, there is no effect on their background processes. You always receive timely updates & notifications.

    0 讨论(0)
  • 2020-11-30 09:00

    Here are few things which helped little .

    In AndroidManifest.xml add line android:enabled="true"

    <service
          android:name=".service.TrackingService"
          android:exported="false"
          android:enabled="true"
        />
    

    Inside service Add alarm to wake up again after 2 seconds .

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        initAlarm();
        super.onTaskRemoved(rootIntent);
    }
    
       private void initAlarm() {
        AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, StartServiceReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    
        alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() +
                        2000, alarmIntent);
    
    }
    

    Create a receiver StartServiceReceiver and in it just start service again .

    For Mi devices we need to allow a permission inside setting to allows service to start in background

    0 讨论(0)
提交回复
热议问题