我试图在我的 onPause() 中停止我的线程,但线程仍在运行并获取线程到期时发送的通知。看来我的一切设置正确......我的通知工作正常,并在 8000 次睡眠后出现......但如果用户在此之前离开/onPause(),那么通知仍然会弹出。
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();
}