Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

android - onTaskRemoved() not getting called when Home press & kill the app

This question not duplicate of onTaskRemoved() not getting called in HUAWEI and XIOMI devices

Problem:

When I press home button & kill the app onTaskRemoved() (Service class override method) - not called.

If I press back button & kill the app --> onTaskRemoved() called perfectly

This issue happen in Android lollipop versions & oreo versions

MyService.class -> Manifest declaration

    <service android:name=".MyService"
        android:label="MyService"
        android:stopWithTask="false"
        android:enabled="true"
        android:exported="true"
        />

I already used the return START_STICKY; in onStartCommand()

Tested devices

Lenovo, Samsung - lollipop version

Samsung - oreo version

Any suggestions or comments are welcome. Your small tips will help to fix this huge issue.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on your use case, you should be able to meet the criteria for the White listing on Android N and above. You can follow this link to Whitelist your app. By requesting this permission, you can relax some restrictions (like accessing network or holding partial lock) implied by Doze mode and Android O. These restrictions are imposed by OS on the app those are not white-listed.

For Lollipop: Certain manufacturers using cyanogenmod or other custom implementation, could have impact on the intended behavior of START_STICKY. Workaround in this case would be to rely on onDestroy() method of service to:

  1. Restart the service.
  2. Trigger an AlarmManager which will trigger after few seconds and start the service.

If you use approach 2:

On normal devices where the START_STICKY behaves as intended, you can use the AlarmManager to check if service is running by:

  1. Maintain a static variable in service to check if service has been started
  2. Cancel the AlarmManager onStartCommand() of the service.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...