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

(FYI: AsyncTask は、おそらくより良いアイデアであるか、Runnableを使用してレイアウトのビューに投稿することをお勧めしますView#postDelayed(Runnable, long))

于 2011-12-20T15:47:35.360 に答える
-1

好奇心から、onPause() が実際に呼び出されていることをどうやって知るのでしょうか?

Toast または Log を使用して、アプリが onPause() 状態になったことを確認します

于 2011-12-20T15:46:24.617 に答える