2

How to close an Activity from a service. Here's my code in service

public void run() {             
    Date dt = new Date();
    int minutes = dt.getMinutes();
    if (minutes % 2 == 0) {
        stopThread();
        Intent i = new Intent();
        i.setClass(this, show_act.class); 
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);    
    }
    else{ 
       //what must i do,. How to close show_act activities from service     
    }
}

Please help me solve this . .

virho
  • 149
  • 2
  • 2
  • 9

1 Answers1

2

In your activity's onResume function, register a broadcast receiver that will call the activity's finish() method.

In your service, when you want to close the activity, simply send the broadcast. If the activity is currently shown, the broadcast receiver will call its finish() method.

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • check if this is helpful..http://stackoverflow.com/questions/5446565/android-how-do-i-check-if-activity-is-running – kedark Aug 11 '14 at 16:00
  • Your broadcast receiver being registered in onResume, so you can be sure that if the broadcast is received then your activity is visible. – Vincent Mimoun-Prat Aug 11 '14 at 16:32
  • i already try all , but still not working. Can you write there's code? – virho Aug 12 '14 at 04:08
  • No, sorry, you'll have to try and do that yourself, my solution should be working. Debug your code to know why. If you want, you can update your question with your current code. – Vincent Mimoun-Prat Aug 12 '14 at 14:46