2016-03-21 1 views
1

私のアプリケーションでは、毎日特定のタスクを実行したいと思います。そのタスクが完了した後、複数の通知を別々のタイミングでトリガーしたいと思います。 グーグルでは、AlarmManagerを使用することが最善の選択だと分かりました。複数の通知を起動するようにAlarm Managerを設定する

このクラスは、システムアラームサービスへのアクセスを提供します。これにより、将来のある時点でアプリケーションを実行するようにスケジュールすることができます。アラームが解除されると、登録されたインテントがシステムによってブロードキャストされ、ターゲットアプリケーションがまだ実行されていない場合は自動的に開始されます。登録されたアラームは、デバイスがスリープしている間は保持されます(また、その時間中にデバイスがオフになった場合にデバイスを起動することもできます)が、オフになって再起動されるとクリアされます。

私の問題は次のとおりです。 1.通知は初めての場合のみ表示され、その後は表示されません。 2.そのアプリケーションを再起動するたびにAlarmManagerが起動され、過去の通知が表示されます。

PS:私はアンドロイドで初心者ですし、任意のヘルプは高く評価される。ここ

は、私が試したものです:

private void handleNotification() { 

    Intent alarmIntent = new Intent(this, AlarmReciever.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
} 
:私はこのアラームを設定していますから、私の活動の

目的球

public class AlarmReciever extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    ArrayList<String> times = new ArrayList(); 
    GPSTracker gpsTracker = new GPSTracker(context); 

    //calculate the times arraylist 

    DateFormat format; 
    Date date = null; 
    for (String s : times) { 
     if (hour12) { 
      format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH); 
      try { 
       date = format.parse(s); 
      } catch (ParseException e) { 
      } 
     } else { 
      format = new SimpleDateFormat("hh:mm", Locale.ENGLISH); 
      try { 
       date = format.parse(s); 
      } catch (ParseException e) { 
      } 
     } 
     Intent alarmIntent = new Intent(context, NotificationReciever.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(date); 

     Log.d(Constants.LOG_TAG, calendar.getTime().toString()); 

     if (Build.VERSION.SDK_INT < 19) { 
      alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
     } else { 
      alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
     } 
    } 
} 

}

これは私のAlarmRecieverクラスです

そして、これが私のNotificationRecieverクラスです:

私のAndroidManifest.xmlで
public class NotificationReciever extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    if (Constants.D) Log.d(Constants.LOG_TAG, "NOTIFICATION RECIEVER"); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.sc_logo_final) 
        .setContentTitle(context.getResources().getString(R.string.app_name)) 
        .setContentText(context.getResources().getString(R.string.app_name)); 
    Intent resultIntent = new Intent(context, NavigationDrawerActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(NavigationDrawerActivity.class); 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(1, mBuilder.build()); 

} 

}

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

     <receiver android:name=".AlarmReciever"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    <receiver android:name=".NotificationReciever"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

答えて

2

まず第一に、このコードは、最も可能性の高い発生するアラームを設定しています現在の夜中には、ほぼ確実に、現在はとなっています。これは、すぐにアラームを受け取ることを意味します。

setRepeating()のjavadocによると
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 0); 
calendar.set(Calendar.MINUTE, 0); 

:述べたトリガー時間が過去にある場合

、アラームが過去にどの程度に応じて、アラームカウントと、すぐに をトリガされます トリガー時間はリピート間隔に比例します。

あなただけsetInexactRepeating()で動作フラグINTERVAL_DAYを、使用しているので、あなたのアラームが繰り返されません。

次の日の深夜に何かを実行する場合は、カレンダーに1日追加する必要があります。秒フィールドも0に設定することも考えられます。

アプリが起動するたびにアラームを登録している場合は、毎回早めにアラームが表示されます。また、javadocに従って:

すでに同じIntentSenderに対してアラームがスケジュールされている場合は、 が最初にキャンセルされます。

setInexactRepeating()にのみ有効なインターバルを過ぎているため、アラームは繰り返されません。

+0

残りの時間に通知が表示されない理由は何ですか? –

+0

はい、私の答えを編集しました。 –

+0

もう1つクリアしてください...私は位置の許可を取得した後にアラームを登録しています(オーバーライドされたメソッドは、アプリケーションが起動するたびに呼び出されます)...理想的にはいつアラームを登録すべきですか?私はこの方法をしようとした:http://stackoverflow.com/a/10836555/4451655それは助けていない.... –

関連する問題