0

I'm trying to stop my thread in my onPause() but the thread still runs and gets the notification that is sent when the thread expires. It seems I have everything set right... My notifcation works fine and comes up after the 8000 sleep.. but if the user leaves/onPause() before then the notification will still pop.

private static final int MY_NOTIFICATION_ID=1;
     private NotificationManager notificationManager;
     private Notification myNotification;
 Thread timer;

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        setContentView(R.layout.expire);

        timer = new Thread(){
                public void run(){
                    try{
                        sleep(8000);                    
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }finally{
                        NotificationManager notificationManager =
                                (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                               myNotification = new Notification(R.drawable.missedcall,
                                 "Exired",
                                 System.currentTimeMillis());
                               Context context = getApplicationContext();
                               String notificationTitle = "Time Expired";
                               String notificationText = "Try next time";
                               Intent myIntent = new Intent();
                               PendingIntent pendingIntent
                                 = PendingIntent.getActivity(CallScreen.this,
                                   0, myIntent,
                                   Intent.FLAG_ACTIVITY_NEW_TASK);
                               myNotification.defaults |= Notification.DEFAULT_SOUND;
                               myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
                               myNotification.setLatestEventInfo(context,
                                  notificationTitle,
                                  notificationText,
                                  pendingIntent);
                               notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
                               finish();


                    }
                }
            };
            timer.start();

}


@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        timer.stop();
        finish();
        }
user961389
  • 423
  • 2
  • 8
  • 19

2 Answers2

0

Firstly: Do you want the code in the finally statement to always execute? Because, as written, you're not going to be able to exit the thread without updating your notification.

If thats ok, just call timer.interrupt(), which will trigger your InterruptedException handler & then call your finally block - if you're averse to interrupting threads and really feel attached to your Thread you can always use a latch to release it.

public class SomeDamnedActivity extends Activity {
    // ... misc junk ...
    private CountDownLatch mLatch;
    private Thread mThread;

    protected void onCreate(Bundle savedInstanceState) {
        mLatch = new CountDownLatch(1);
        mThread = new Thread() {
            public void run() {
                try {
                    mLatch.await(8, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    Log.w(TAG, "Interrupted waiting for latch to release", e);
                } finally {
                    // Stuff goes here that you want to execute.
                }
            }
        };      
    }

    protected void onPause() {
        // ...
        mLatch.countDown(); // This will release the latch "prematurely" calling your finally-statement.
    }

(FYI: An AsyncTask is probably a better idea or posting a Runnable to a View in your layout using View#postDelayed(Runnable, long) )

Jens
  • 16,853
  • 4
  • 55
  • 52
  • What I think I'm going to do and I tested that it work is move the notifcation up into the try under `sleep(8000);` then call `timer.interrupt()` in the `onPause()`... so that way if it runs 8 secs the notifcation will come up and the activity will `finish()` but if the user leaves or starts a new activity within the 8 secs... the thread will call `timer.interrupt()` in the `onPause()` and they won't see the expired notifcation. That's essentially what I want. DO you see any problems doing it that way? – user961389 Dec 20 '11 at 16:07
-1

Just out of curiosity, how do you know that onPause() is actually called?

Use Toast or Log to find out, that your app enters the onPause() state

DoDu
  • 44
  • 2