2017-01-06 6 views
0

設定用のアクティビティを作成し、その中に午前7時と午後5時に特定の時刻に通知を送信するスイッチを作成します。私がスイッチをチェックすると、2つの通知が現れました。アラームマネージャーが指定された時刻に機能しない

スイッチのコードが

public void OnRepeationNotification(int id,int hours,int minutes,int seconds) { 
    Calendar calendar= Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY,hours); 
    calendar.set(Calendar.MINUTE,minutes); 
    calendar.set(Calendar.SECOND,seconds); 
    Intent intent=new Intent(getApplicationContext(),NotificationReciever.class); 
    PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),id,intent,PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent); 
} 

public void onClickNotify(View view) { 
    Switch v=(Switch) view; 
    if(v.isChecked()){ 
     putSett(true,"sett",this,0); 
     OnRepeationNotification(1,7,0,0); 
     OnRepeationNotification(2,17,0,0); 

    }else { 
     alert_sound.setChecked(false); 
     alert_vibrate.setChecked(false); 
     putSett(false,"sett",this,1); 
     putSett(false,"sett",this,2); 
     putSett(false,"sett",this,0); 
    } 
} 

放送受信機アプリが閉じているか、デバイスが再起動された場合でも、通知を送信する方法

public class NotificationReciever extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     sendNotification(context,1); 
     sendNotification(context,2); 
    } 

    public void sendNotification(Context context,int id){ 
     NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent repeating_intent=new Intent(context,RepeatingActivity.class); 

     repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent=PendingIntent.getActivity(context,id,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT); 


     NotificationCompat.Builder builder=new NotificationCompat.Builder(context) 
       .setContentIntent(pendingIntent) 
       .setSmallIcon(R.drawable.fav_black) 
       .setContentTitle("Notifiction Title") 
       .setContentText("Notification Text") 
       .setAutoCancel(true); 
     notificationManager.notify(id,builder.build()); 
    } 
} 

のコードですか?

答えて

0

設定されている時間が既に当日に経過している場合、通知は直ちに発生します。

calendar.set(Calendar.DATE, 1); 

ユーザーならばアラームがアクティブのままになります。それは次の日のためのアラームを設定するには、カレンダーオブジェクトにこれを追加した場合は、これを修正するには、時間が既に経過しているかどうかを確認する必要があり、 '最近のアプリ'画面でアプリをスワイプして閉じます。携帯電話の再起動後に生きているアラームを保つために、あなたはそのマニフェスト宣言でBOOT_COMPLETEDインテントフィルタとBroadcastReceiverを行う必要があります、次のように:そのBroadcastReceiverの内部

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

、あなたはIntentServiceどれを実行する必要がありますすべてのアラームを再起動します。

+0

別の時刻を設定しても、2つの通知が同時に表示される –

+0

スイッチを反転させると、両方の表示が正しく表示されますか? 'calendar.set(Calendar.DATE、1);'部分を追加しましたか? –

+0

私はこのコードを設定します。calendar RightNow = Calendar.getInstance(); if(calendar.before(rightNow)); calendar.set(Calendar.DATE、1);その通りです –

関連する問題