0

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

2 回答 2

0

首先:您是否希望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 可能是一个更好的主意,或者使用 将 a 发布Runnable到布局中的视图View#postDelayed(Runnable, long)

于 2011-12-20T15:47:35.360 回答
-1

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

使用 Toast 或 Log 找出您的应用程序进入 onPause() 状态

于 2011-12-20T15:46:24.617 回答