1

我正在Android上構建一個水提醒應用程序,並希望每天安排一定數量的警報,在特定時間後停止(由用戶設置),並在第二天重新啓動而沒有用戶不得不打開該應用程序。我該如何解決它?AlarmManager和BroadcastReceiver的多個報警

我看不出我如何設置重複報警,因爲我希望他們在一個點停止並重新啓動第二天,重複報警不會這樣做。我如何實現這一目標?

回答

0

您可以設置兩個報警,第一個是用於提醒的重複報警。 另一個鬧鐘用於在特定時間停止第一個鬧鐘,然後安排第二天的鬧鐘。

+0

但你如何設置它爲多天,而無需打開應用程序的用戶? –

+0

當您收到當天的最後一個鬧鐘時,您將設置第二天的鬧鐘。接收器在接收到警報並且未選擇應用程序時工作。 – androidLover

+0

你能舉個例子嗎? –

0

這是我的示例代碼。這可能對你有幫助,但我不確定。

MainActivity:

public static int NOW_NOTIFICATION = 101; 

Intent intent = new Intent(MainActivity.this, NotificationReceiver.class); 
       intent.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert"); 
       PendingIntent pendingIntent = PendingIntent.getBroadcast(
         MainActivity.this, NOW_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT); 

       cancelNotification(NOW_NOTIFICATION); 
       AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
       alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
         + (2 * 1000), pendingIntent); 

取消通知:

public void cancelNotification(int requestCode) { 
     try { 
      Intent notificationIntent = new Intent(getApplicationContext(), NotificationReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
      alarmManager.cancel(pendingIntent); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

NotificationReceiver:

public class NotificationReceiver extends BroadcastReceiver { 
    public static String NOTIFICATION_MESSAGE = "notificationMessage"; 


    @Override 
    public void onReceive(Context context, Intent intent) { 
     String message = intent.getStringExtra(NOTIFICATION_MESSAGE); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     //Define sound URI 
     Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     Intent notificationIntent = new Intent(context, EmptyActivity.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     final DateTime dateTime = DateTime.now(); 
     int color = 0xffffaa00; 
//  int color1 = context.getColor(R.color.notificatinBackgroundColor); 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setSmallIcon(R.drawable.festi_push_message_small); 
     builder.setContentIntent(pendingIntent); 
     builder.setContentTitle("Notification Sample"); 
     builder.setAutoCancel(true); 
     builder.setContentText(message + ", Current Time : " + dateTime.getHourOfDay() + ":" + dateTime.getMinuteOfHour() + ":" + dateTime.getSecondOfMinute()); 
     builder.setSound(soundUri); 
     builder.setLights(Color.RED, 1000, 1000); 
     builder.setColor(color); 


     Notification notification = builder.build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     notificationManager.notify(102938, notification); 
    } 

} 

DateChangedReceiver:

public class DateChangedReceiver extends BroadcastReceiver { 
    public static String NOTIFICATION_MESSAGE = "notificationMessage"; 

    public static int NOW_NOTIFICATION = 101; 

    Context context; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     this.context = context; 
     Intent intentNitification = new Intent(context, NotificationReceiver.class); 
     intentNitification.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       context, NOW_NOTIFICATION, intentNitification, PendingIntent.FLAG_ONE_SHOT); 

     cancelNotification(NOW_NOTIFICATION); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
       + (2 * 1000), pendingIntent); 
    } 

    public void cancelNotification(int requestCode) { 
     try { 
      Intent notificationIntent = new Intent(context, NotificationReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      alarmManager.cancel(pendingIntent); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

AndroidManifiest:

<receiver android:name=".NotificationReceiver" /> 

     <receiver android:name=".DateChangedReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.DATE_CHANGED" /> 
      </intent-filter> 
     </receiver>