can i use isFinishing() from a service?
I want to know if my app closing legitimately or from a crash. When my app starts it sets an alarm which when triggered starts a service. I cant work out how to code the isFinishing() from the service . I tried :-
if (!((Activity) getBaseContext()).isFinishing())
this 开发者_如何学Pythoncompiled ok but when run gave the following error:-
java.lang.ClassCastException: android.app.ContextImpl
Is there a way to call from a service? Ron
Your problem comes from a wrong casting : getBaseContext()
doesn't return an Activity at all but a ContextImpl
.
But still, I am not sure this will solve your problem. See my comment above.
Regards, Stéphane
isFinishing() is works for Activity not Service. Most probably it is written inside your onPause() method of your activity to find out activity is finishing or (pausing or stopping).
Now if you pass activity context to service it will work. But if your context is not activity context hence the exception.
精彩评论