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();
}