2016-05-18 4 views
1

私はいくつかの通知をバックグラウンドで表示するためにハンドラを実行しています。テスト中、遅延制限を5秒に設定すると完全に動作します。しかし、私が60秒以上に設定しても、それはうまくいきません。何故ですか?ハンドラは60秒間動作しません?

int delay = 1000 * 60; 
    HandlerThread hThread = new HandlerThread("HandlerThread"); 
    hThread.start(); 
    Handler handler = new Handler(hThread.getLooper()); 

    Runnable task = new Runnable() { 
     @Override 
     public void run() { 
      NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
      Intent launchIntent = new Intent(getApplicationContext(), AndroidLauncher.class); 
      PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, launchIntent, 0); 

      NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidLauncher.this); 
      //Set notification information 
      builder.setSmallIcon(R.drawable.ic_launcher) 
        .setTicker("Hi!") 
        .setWhen(System.currentTimeMillis()) 
        .setAutoCancel(true) 
        .setDefaults(Notification.DEFAULT_SOUND) 
        .setContentTitle(mytitle) 
        .setContentText(mytext) 
        .setContentIntent(contentIntent); 

      NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(builder); 
      style.setSummaryText("4 Task"); 
      style.addLine("Two"); 
      style.addLine("Three"); 
      style.addLine("Four"); 
      style.addLine("Five"); 

      Notification note = style.build(); 
      manager.notify(50, note); 
     } 
    }; 

    handler.postDelayed(task, delay); 
+0

ハンドラーコードやいくつかのログを60秒間以上書き込んでも関係ありません。 – Amir

+0

私はちょうど..... –

+0

あなたは本当に何のために 'android-studio'とタグ付けしましたか? –

答えて

1

インポートimport android.os.Handler;

と次のコードを使用:HandlerHandlerThreadLooperhereについての良いチュートリアルはあり

new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        //do your work here after 60 second 
       } 
      },60000); 

を。

関連する問題