1

Androidでウォーターリマインダーアプリを構築していて、特定の時間(ユーザーが設定した)の後に停止し、次の日にユーザーがアプリを開く必要があります。それについてどうすればいいですか?AlarmManagerとBroadcastReceiverによる複数のアラーム

繰り返しアラームを設定する方法がわかりません。繰り返しアラームを1つのポイントで停止して翌日に再開し、アラームを繰り返すと、そのアラームは繰り返されません。どのように達成するのですか?

答えて

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> 
関連する問題