Show a notification only if Activity is not showing

99封情书 提交于 2019-12-03 12:32:41

问题


I have a background task I would like to handle. The thing is that when the task completes, I would like to call a new Activity to show the result to the user, only if my main Activity is showing, otherwise I would like to send just a notification so the user can see that the action completed, and be able to open it whenever he likes.

I was thinking of using a service to handle the start and termination of the background task and broadcast a message when it finishes, but in this case I have no option to know whether the Activity was shown or the broadcast was not processed and I should send a notification.

So this is my problem, and because my knowledge and experience in background tasks and services is limited I decided to ask for some help.

Thanks in advance for reading my case, hope for some help!


回答1:


Here's a good article which describes how to implement what you want: Activity or Notification via Ordered Broadcast.

The main idea is to use ordered broadcasts. You should create a BroadcastReceiver which will live without any activities. In order to do that you should declare it in the AndroidManifest.xml file. This receiver will show a Notification. Also you should register another BroadcastReceiver with higher priority in your main activity which will display something on the screen. Then you just need to send an ordered broadcast.




回答2:


Try like this.

private static boolean isInForeground;

onResume(){
    isInForground = true;
}
onPause(){
   isInForground = false;
}

if isInForground is true then Activity is in Forground(Showing) otherwise not showing.

if you want to know from anywhere then add the following in MainActivity.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    onResume(){
         pref = PreferenceManager.getDefaultSharedPreferences(this);
         prefEditor = prefs.edit();
         prefEditor.putBoolean("isInForeground",true);
         prefEditor.commit();
    }

    onPause(){
             pref = PreferenceManager.getDefaultSharedPreferences(this);
             prefEditor = prefs.edit();
             prefEditor.putBoolean("isInForeground", false);
             prefEditor.commit();
    }

Then from your service.

pref = PreferenceManager.getDefaultSharedPreferences(this);
if(pref.getBoolean("isInForeground", false)){
       //MainActivity is in forground

}
else{
      //not in forground
}



回答3:


It sounds like you want to implement the task in a thread from a Service. You can have a persistent static boolean in the Activity that says whether the activity is shown. An activity is visible to the user in onResume() and not visible whenever onPause() is called. Set the boolean to true in onResume() and false in onPause().




回答4:


you can use the SharedPreferences; update a preferences on an activity when it is onStart() or onStop() and just verify it in you service.

Hope i will help.



来源:https://stackoverflow.com/questions/7191413/show-a-notification-only-if-activity-is-not-showing

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