2017-01-23 7 views
0

Androidで次のコードを使用して繰り返しアラームを設定できました。しかし、問題は即座に通知を出し、私はそれを望んでいないということです。私はちょうど彼らがアラームを消したい時に入れて、その時にアラームを消して、選択された所定の間隔で繰り返すようにしておきたい。 RTC_WAKEUPの代わりにELAPSED_REALTIME_WAKEUPを切り替えてみましたが、そのコードは私のためには機能しませんでした。Android AlarmManagerで将来の繰り返しアラームをスケジュールするにはどうすればよいですか?

public static long getTimeForNotification(int hour, int minutes, int am_pm){ 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR, hour); 
    calendar.set(Calendar.MINUTE, minutes); 
    calendar.set(Calendar.AM_PM, am_pm); 
    return calendar.getTimeInMillis(); 
} 

public static PendingIntent createNotificationPendingIntent(
     String name, 
     String number, 
     String messageArrayListString, 
     String contactID, 
     String photo_uri, 
     String actionType, 
     Context mContext 
     ){ 

    Intent alertIntent = new Intent(mContext, AlertReceiver.class); 
    alertIntent.putExtra("name", name); 
    alertIntent.putExtra("number", number); 
    alertIntent.putExtra("messageList", messageArrayListString); 
    alertIntent.putExtra("contactID", contactID.toString()); 
    alertIntent.putExtra("photo_uri", photo_uri); 
    alertIntent.setAction(actionType); 


    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, Integer.parseInt(contactID.toString()), alertIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    return pendingIntent; 
} 

public static void createNotifications(PendingIntent notificationPendingIntent, Context mContext, Long alarmTime, int frequencyMultiplier){ 

    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY * Long.valueOf(frequencyMultiplier), notificationPendingIntent); 

} 

ありがとうございます!!!

答えて

0

これは私が使用しているものです。

public class AlarmHelper { 
    private static final String TAG = "AlarmHelper"; 
    private static final int REQUEST_CODE = 12377; 
    private static final int REQUEST_CODE_OVERTIME = 12376; 

    private AlarmHelper() { 
    } 

    public static void scheduleAlarm(Context c) { 
     AlarmManager manager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(c, REQUEST_CODE, new Intent(c, ClockInBroadcastReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT); 


     //Set next clock-in time 
     GregorianCalendar time = new GregorianCalendar(); 
     time.add(...,...); //Whatever you want to add. 


     if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 
      manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent); 
     } else if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 
      manager.setExact(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent); 
     } else { 
      manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent); 
     } 

    } 

} 
+0

これは役に立ちましたか? –

関連する問題