线程不会在暂停时停止



我试图在我的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();
        }

first :您希望finally语句中的代码始终执行吗?因为,如前所述,如果不更新通知,您将无法退出线程。

如果可以,只需调用timer.interrupt(),这将触发您的InterruptedException处理程序&然后调用你的finally块-如果你不愿意打断线程,并且真的觉得与你的线程相连,你可以使用一个锁存器来释放它。

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.
    }

(仅供参考:一个AsyncTask可能是一个更好的主意或张贴Runnable到一个视图在您的布局使用View#postDelayed(Runnable, long))

只是出于好奇,你怎么知道onPause()实际上被调用了?

使用Toast或Log来发现,你的应用程序进入onPause()状态

最新更新