2016-12-05 13 views
1

こんにちはこれはスタックオーバーフローに関する私の最初の質問です。私はマルチタイマであるAndroid用のアプリを書いています。私が設定した方法は、onStop()が呼び出されたときに、すべてのタイマーをキャンセルし、AlarmManagerの4つの保留中のインテントを開始し、時間をカウントダウンし続けます。ユーザーがアプリに戻ると、AlarmManagerのタイマーはキャンセルされ、カウントダウンを続ける新しいタイマーが作成されます。私はsharedpreferencesを使用してすべての値を保存しています。複数のアラームbrodcastreceiver android

私の問題は私のBroadcastReceiverクラスにあると確信しています。何が起きているのですか?私はアプリを起動し、複数のタイマーを起動し、戻るボタンを押して(onStopに電話して)、アプリに戻り、すべてのタイマーを停止してリセットします。私はもう一度やり直してください(再びonStopを呼んでください)、BroadcastReceiverは4回発砲します。タイマーがすべてリセットされているため、発砲してはいけません。

また、問題がOnReceiveにあると思うのは、私が4つのpendingIntentsを設定していないからです。それが私の実際の質問です。 1つのBroadcastReceiverクラスに4つのpendingIntentsをセットアップすることは可能ですか?もしそうなら、私はどうしたらいいですか?

また、アラームが既に再生されていて、アラームマネージャーの別のアラームが終了しても、アラーム音が再び再生されず、既に通知されている通知のテキストを変更するように設定したいと考えています。

P.S.私はAndroidとプログラミング一般には本当に新しいです。私はこれが愚かな質問であり、私の投稿の書式設定があまり良くない場合は謝罪します。私は終わった

public class AlarmReceiver extends BroadcastReceiver { 

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

    Intent notificationIntent = new Intent(context, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    NotificationCompat.Builder timerNotificationBuilder = new NotificationCompat.Builder(context); 
    Uri notification = Uri.parse("android.resource://" + "com.example.iinewmanii.professionalkitchentimer" + "/" + R.raw.alarm_clock_short); 
    timerNotificationBuilder.setSound(notification) 
      .setPriority(2) 
      .setColor(Color.rgb(239, 82, 79)) 
      .setContentTitle("Professional Kitchen timer") 
      .setAutoCancel(true) 
      .setContentText("Timer 1 Has Finished") 
      .setSmallIcon(R.mipmap.ic_warning_white_24dp) 
      .setWhen(System.currentTimeMillis()) 
      .setContentIntent(pendingIntent); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       timerNotificationBuilder.setCategory(CATEGORY_ALARM); 
      } 

    Notification timerNotification = timerNotificationBuilder.build(); 
    timerNotification.flags |= Notification.FLAG_INSISTENT; 
    NotificationManager timerNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    timerNotificationManager.notify(0, timerNotification); 
} 

}

答えて

0

:アラームの

public void removeAlarmManager() { 
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    Intent intent = new Intent(this, AlarmReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    PendingIntent senderTwo = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    PendingIntent senderThree = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    PendingIntent senderFour = PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    am.cancel(sender); 
    am.cancel(senderTwo); 
    am.cancel(senderThree); 
    am.cancel(senderFour); 
} 

BroadcastReceiverクラス:AlarmManagerを除去するための

public void setAlarmManager() { 
    long wakeUpTime = timerPreferences.getStartTime() + millisToCount; 
    long wakeUpTimeTwo = timerPreferences.getStartTimeTwo() + millisToCountTwo; 
    long wakeUpTimeThree = timerPreferences.getStartTimeThree() + millisToCountThree; 
    long wakeUpTimeFour = timerPreferences.getStartTimeFour() + millisToCountFour; 

    Log.v(TAG, "Millis For AM " + millisToCount); 
    Log.v(TAG, "Millis Two For AM " + millisToCountTwo); 
    Log.v(TAG, "Millis Three For AM " + millisToCountThree); 
    Log.v(TAG, "Millis Four For AM " + millisToCountFour); 

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    Intent intent = new Intent(this, AlarmReceiver.class); 

    if (millisToCount > 0) { 
     PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTime, sender), sender); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, sender); 
     } 
     Log.v(TAG, "Alarm Manager Set"); 
    } 

    if (millisToCountTwo > 0) { 
     PendingIntent senderTwo = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeTwo, senderTwo), senderTwo); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeTwo, senderTwo); 
     } 
     Log.v(TAG, "Alarm Manager Two Set"); 
    } 

    if (millisToCountThree > 0) { 
     PendingIntent senderThree = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeThree, senderThree), senderThree); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeThree, senderThree); 
     } 
     Log.v(TAG, "Alarm Manager Three Set"); 
    } 

    if (millisToCountFour > 0) { 
     PendingIntent senderFour = PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeFour, senderFour), senderFour); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeFour, senderFour); 
     } 
     Log.v(TAG, "Alarm Manager Four Set"); 
    } 
} 

コード:AlarmManagerを設定するための

コードこれを自分で解決する。私がする必要があったのは、私がキャンセルしたいアラームを識別するために、AlarmReceiverクラスが得ることができるインテントに追加項目を追加することでした。私は保留中のインテントに渡されたIDがこれを行うことができると考えましたが、その値は明らかに渡されません。ここに私のコードです。

セットアラームマネージャ:

private void setAlarmManager() { 
    long wakeUpTime = timerPreferences.getStartTime() + millisToCount; 
    long wakeUpTimeTwo = timerPreferences.getStartTimeTwo() + millisToCountTwo; 
    long wakeUpTimeThree = timerPreferences.getStartTimeThree() + millisToCountThree; 
    long wakeUpTimeFour = timerPreferences.getStartTimeFour() + millisToCountFour; 

    Log.v(TAG, "Millis For AM " + millisToCount); 
    Log.v(TAG, "Millis Two For AM " + millisToCountTwo); 
    Log.v(TAG, "Millis Three For AM " + millisToCountThree); 
    Log.v(TAG, "Millis Four For AM " + millisToCountFour); 

    Log.v(TAG, "Paused Time For AM " + pausedTime); 
    Log.v(TAG, "Paused Time Two For AM " + pausedTimeTwo); 
    Log.v(TAG, "Paused Time Three For AM " + pausedTimeThree); 
    Log.v(TAG, "Paused Time Four For AM " + pausedTimeFour); 

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    Intent intent = new Intent(this, AlarmReceiver.class); 

    if (millisToCount > 0) { 
     intent.putExtra("timerNumberOneId", 1); 
     PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTime, sender), sender); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, sender); 
     } 
     Log.v(TAG, "Alarm Manager Set"); 
    } 

    if (millisToCountTwo > 0) { 
     intent.putExtra("timerNumberTwoId", 2); 
     PendingIntent senderTwo = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeTwo, senderTwo), senderTwo); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeTwo, senderTwo); 
     } 
     Log.v(TAG, "Alarm Manager Two Set"); 
    } 

    if (millisToCountThree > 0) { 
     intent.putExtra("timerNumberThreeId", 3); 
     PendingIntent senderThree = PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeThree, senderThree), senderThree); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeThree, senderThree); 
     } 
     Log.v(TAG, "Alarm Manager Three Set"); 
    } 

    if (millisToCountFour > 0) { 
     intent.putExtra("timerNumberFourId", 4); 
     PendingIntent senderFour = PendingIntent.getBroadcast(this, 4, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeFour, senderFour), senderFour); 
     } else { 
      am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeFour, senderFour); 
     } 
     Log.v(TAG, "Alarm Manager Four Set"); 
    } 
} 

削除アラームマネージャ:

private void removeAlarmManager() { 
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    Intent intent = new Intent(this, AlarmReceiver.class); 

    intent.getIntExtra("timerNumberOneId", 0); 
    PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    intent.getIntExtra("timerNumberTwoId", 0); 
    PendingIntent senderTwo = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    intent.getIntExtra("timerNumberThreeId", 0); 
    PendingIntent senderThree = PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    intent.getIntExtra("timerNumberFourId", 0); 
    PendingIntent senderFour = PendingIntent.getBroadcast(this, 4, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    am.cancel(sender); 
    am.cancel(senderTwo); 
    am.cancel(senderThree); 
    am.cancel(senderFour); 

    Log.v(TAG, "Alarms Removed"); 
} 

AlarmReceiverクラス:

public class AlarmReceiver extends BroadcastReceiver { 

PrefUtils timerPreferences = null; 
private final static String ALARM_NOTIFICATION = "alarm_notification"; 
private final int timerOneNotifColor = Color.argb(255, 239, 82, 79); 
private final int timerTwoNotifColor = Color.argb(255, 250, 225, 85); 
private final int timerThreeNotifColor = Color.argb(255, 94, 171, 92); 
private final int timerFourNotifColor = Color.argb(255, 250, 150, 27); 
private final int timerOneLightColor = Color.argb(255, 255, 0, 0); 
private final int timerTwoLightColor = Color.argb(255, 255, 255, 0); 
private final int timerThreeLightColor = Color.argb(255, 0, 255, 0); 
private final int timerFourLightColor = Color.argb(255, 255, 55, 0); 

private void alarmNotification(int timerNumber, Context context, int notifColor, int lightColor, Intent intent) { 
    int timerNumberOneId = intent.getIntExtra("timerNumberOneId", 0); 
    int timerNumberTwoId = intent.getIntExtra("timerNumberTwoId", 0); 
    int timerNumberThreeId = intent.getIntExtra("timerNumberThreeId", 0); 
    int timerNumberFourId = intent.getIntExtra("timerNumberFourId", 0); 
    int notifId = 0; 

    Uri notification = Uri.parse("android.resource://" + "com.professionalkitchentools.iinewmanii.professionalkitchentimer" + "/" + R.raw.alarm_clock_short); 

    intent = new Intent(context, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent sender = PendingIntent.getActivity(context, 1, intent, 0); 
    PendingIntent senderTwo = PendingIntent.getActivity(context, 2, intent, 0); 
    PendingIntent senderThree = PendingIntent.getActivity(context, 3, intent, 0); 
    PendingIntent senderFour = PendingIntent.getActivity(context, 4, intent, 0); 

    NotificationCompat.Builder timerNotificationBuilder = new NotificationCompat.Builder(context); 
    int lightFlashingInterval = 500; 
    timerNotificationBuilder.setPriority(2) 
      .setColor(notifColor) 
      .setSound(notification) 
      .setLights(lightColor, lightFlashingInterval, lightFlashingInterval) 
      .setContentTitle("Timer " + timerNumber + " has finished") 
      .setContentText("Professional Kitchen Timer") 
      .setSmallIcon(R.mipmap.ic_warning_white_24dp) 
      .setWhen(System.currentTimeMillis()) 
      .setShowWhen(true) 
      .setGroup(ALARM_NOTIFICATION) 
      .setAutoCancel(true); 
    if (timerNumberOneId == 1) { 
     notifId = 1; 
     timerNotificationBuilder.setContentIntent(sender); 
    } 

    if (timerNumberTwoId == 2) { 
     notifId = 2; 
     timerNotificationBuilder.setContentIntent(senderTwo); 
    } 

    if (timerNumberThreeId == 3) { 
     notifId = 3; 
     timerNotificationBuilder.setContentIntent(senderThree); 
    } 

    if (timerNumberFourId == 4) { 
     notifId = 4; 
     timerNotificationBuilder.setContentIntent(senderFour); 
    } 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     timerNotificationBuilder.setCategory(CATEGORY_ALARM); 
    } 

    Notification timerNotification = timerNotificationBuilder.build(); 
    timerNotification.flags |= Notification.FLAG_INSISTENT; 
    NotificationManager timerNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    timerNotificationManager.notify(notifId, timerNotification); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    timerPreferences = new PrefUtils(context); 
    int timerNumberOneId = intent.getIntExtra("timerNumberOneId", 0); 
    int timerNumberTwoId = intent.getIntExtra("timerNumberTwoId", 0); 
    int timerNumberThreeId = intent.getIntExtra("timerNumberThreeId", 0); 
    int timerNumberFourId = intent.getIntExtra("timerNumberFourId", 0); 

    if (timerNumberOneId == 1) { 
     alarmNotification(timerNumberOneId, context, timerOneNotifColor, timerOneLightColor, intent); 
     timerPreferences.setTimerOneRunning(false); 
     timerPreferences.setStartTime(0); 
     timerPreferences.setOriginalTime(0); 
     timerPreferences.setTimerPaused(false); 
     timerPreferences.setPausedTime(0); 
    } 

    if (timerNumberTwoId == 2) { 
     alarmNotification(timerNumberTwoId, context, timerTwoNotifColor, timerTwoLightColor, intent); 
     timerPreferences.setTimerTwoRunning(false); 
     timerPreferences.setStartTimeTwo(0); 
     timerPreferences.setOriginalTimeTwo(0); 
     timerPreferences.setTimerTwoPaused(false); 
     timerPreferences.setPausedTimeTwo(0); 
    } 

    if (timerNumberThreeId == 3) { 
     alarmNotification(timerNumberThreeId, context, timerThreeNotifColor, timerThreeLightColor, intent); 
     timerPreferences.setTimerThreeRunning(false); 
     timerPreferences.setStartTimeThree(0); 
     timerPreferences.setOriginalTimeThree(0); 
     timerPreferences.setTimerThreePaused(false); 
     timerPreferences.setPausedTimeThree(0); 
    } 

    if (timerNumberFourId == 4) { 
     alarmNotification(timerNumberFourId, context, timerFourNotifColor, timerFourLightColor, intent); 
     timerPreferences.setTimerFourRunning(false); 
     timerPreferences.setStartTimeFour(0); 
     timerPreferences.setOriginalTimeFour(0); 
     timerPreferences.setTimerFourPaused(false); 
     timerPreferences.setPausedTimeFour(0); 
    } 
} 

}

関連する問題