2010-12-29 45 views
182

私のアプリが起動したら、特定のアラーム(AlarmManager経由で登録済み)が既に設定され、動作しているかどうかをチェックしたい。 googleからの結果は、これを行う方法がないことを示しているようです。これはまだ正しいですか?新しいアラームを作成するためのアクションが実行される前に、このチェックを行う必要があります。AlarmManagerに既にアラームが設定されているかどうかを確認する方法は?

+0

のために働くようです。 – AnixPasBesoin

答えて

-3

あなたは目覚まし時計の話している - あなたはここからダウンロードできるソース - https://android.googlesource.com/platform/packages/apps/AlarmClock

それとも、特定のタスクを実行しなければならない時間を設定するので話している - http://developer.android.com/reference/android/app/AlarmManager.html

+0

名前をDeskClockに変更しました:http://android.git.kernel.org/?p=platform/packages/apps/DeskClock.git;a=summary – CommonsWare

+0

私はAlarmManagerを使用して自分のコードでアラームを設定しています。私は、アラームが特定の意図に対して既に設定されているかどうかを確認する方法を見つけることができません。プログラムでは、ユーザーがアラームを設定してからアプリを閉じることができるので、アラームが設定されていて「見えない」かどうかをアプリがいつ開かれるかを知りたい。 – ron

+0

AlarmManagerクラスはそのような機能を提供していないようです。ここでAlarmManagerのソースを見ることができます - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/app/AlarmManager.java あなたが本当に必死なら、それを拡張する(またはIAlarmManagerを再実装する)ことができ、独自のアラームマネージャを作成することができます。過剰殺戮。 – fiction

1

イムtheresの印象でこれを行う方法はありませんが、それはいいですね。

Alarm_last_set_timeをどこかに記録し、On_boot_starter BroadcastReciever:BOOT_COMPLETEDちょっとしたことをすることで、同様の結果が得られます。

277

ronが投稿したコメントをフォローして、ここに詳細な解決策があります。あなたはこのような保留中の意図で繰り返しアラームを登録したとしましょう:

Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION"); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, 
             intent, PendingIntent.FLAG_UPDATE_CURRENT); 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.add(Calendar.MINUTE, 1); 

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pendingIntent); 

あなたはそれがアクティブであるかどうかを確認するような方法があるに:

boolean alarmUp = (PendingIntent.getBroadcast(context, 0, 
     new Intent("com.my.package.MY_UNIQUE_ACTION"), 
     PendingIntent.FLAG_NO_CREATE) != null); 

if (alarmUp) 
{ 
    Log.d("myTag", "Alarm is already active"); 
} 

キーここFLAG_NO_CREATEはありますjavadoc:if the described PendingIntent **does not** already exists, then simply return null(新しいものを作成するのではなく)に記載されています。

+8

アクションストリングだけでインテントを使用する必要がありますか?私はクラス、新しいインテント(コンテキスト、MyClass.class)を指定しようとしましたが、うまくいかないようです。アラームが実行されていても常にnullを返します。 – toc777

+5

toc777では、マニフェストのインテントフィルタで宣言されたアクションに一致する文字列である必要はありません。xml –

+4

クリス、私の問題を引き起こしていたもう一つの問題でした。上記の目的は実際にはうまくいきます:) – toc777

7

私には2つのアラームがあります。私は、イベントを識別するために、代わりにアクションのエキストラとの意図を使用しています:

Intent i = new Intent(context, AppReciever.class); 
i.putExtra("timer", "timer1"); 

事がデフのエキストラと意図(アラーム)は文句を言わないユニークであるということです。アラームが作成された方法ここれる

boolean alarmUp = (PendingIntent.getBroadcast(context, MyApp.TIMER_1, i, 
        PendingIntent.FLAG_NO_CREATE) != null); 

をして::アラームがアクティブかそうでないかを識別することができてとても、私はデフrequestCode -sを定義する必要がありました、これを必要とするかもしれ他人のために

public static final int TIMER_1 = 1; 
public static final int TIMER_2 = 2; 

PendingIntent pending = PendingIntent.getBroadcast(context, TIMER_1, i, 
      PendingIntent.FLAG_CANCEL_CURRENT); 
setInexactRepeating(AlarmManager.RTC_WAKEUP, 
      cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending); 
pending = PendingIntent.getBroadcast(context, TIMER_2, i, 
      PendingIntent.FLAG_CANCEL_CURRENT); 
setInexactRepeating(AlarmManager.RTC_WAKEUP, 
      cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending); 
+0

インテントエクストラを使用すると、この解決策が私に役立ちました。 1つの変更だけがサービスを使用しているので、私はそれを「PendingIntent.getService」に変更しました – Pankaj

92

、ここに答えがあります。

使用adb shell dumpsys alarm

あなたは、アラームが設定されていると、彼らはアラームの発生したとの間隔しようとしているときを知ることができます。このアラームが何回呼び出されたか。

+4

これは正しい答えでなければなりません – user1406716

+1

これはまさに –

+24

です。非常に知って良い。 – JustSomeGuy

36

受取人の作業例(一番上の回答はちょうどアクションを伴うものでした)。

//starting 
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(getActivity(), MyReceiver.class); 
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//my custom string action name 
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//used unique ID as 1001 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), aroundInterval, pendingIntent);//first start will start asap 

//and stopping 
Intent intent = new Intent(getActivity(), MyReceiver.class);//the same as up 
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//the same as up 
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//the same as up 
alarmManager.cancel(pendingIntent);//important 
pendingIntent.cancel();//important 

//checking if alram is working with pendingIntent 
Intent intent = new Intent(getActivity(), MyReceiver.class);//the same as up 
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//the same as up 
boolean isWorking = (PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_NO_CREATE) != null);//just changed the flag 
Log.d(TAG, "alarm is " + (isWorking ? "" : "not") + " working..."); 

私はアラームマネージャのためのドキュメントからの私の​​

+1

これで十分ではないかと思います。 PendingIntentがAlarmManagerに登録され、両方のキャンセルメソッドによって停止された場合、上記の 'isWorking'は引き続きtrueになります。 PendingIntentはAlarmManagerから削除されていないようで、インスタンスを返すことになります。どのようにしてアラームがオン/オフされたかを効果的にどのように認識するのですか? – johnDisplayClass

+0

これは実際には完全に機能しました。注意点:setAction()とrequestCode()は、すべてのgetBroadcast()とそのデバイスからアプリをアンインストールする価値が同じである必要があります。それは私を捕まえた。ありがとう – johnDisplayClass

+0

素晴らしい作品です。ありがとう! – Ambran

29

注意にも、この引用を説明しました:

すでに と、スケジュールこのテントのアラーム(がある場合Intent.filterEqualsによって定義されている2つのインテントの等価性)、 は削除され、この1つに置き換えられます。

アラームを作成するかどうかを決定する場合は、アラームが存在するかどうかを確認する必要はありません。あなたのアプリが起動するたびに作成してください。設定した過去のアラームはすべて置き換えられます。

以前に作成されたアラームにどれくらいの時間が残っているのか、またはそのようなアラームが存在するかどうか本当に知る必要がある場合は、別の方法が必要です。これらの質問に答えるには、アラームを作成するときに共有のプレフィックスデータを保存することを検討してください。アラームが設定された瞬間に時計のタイムスタンプを保存し、アラームが鳴ると予想される時間と繰り返し周期(繰り返しアラームを設定した場合)を保存することができます。

+0

私の意見では、これは受け入れられた答えでなければなりません。 OPに特別な状況があり、アラームを再表示しないことを正当化しない限り、 –

+0

私の場合、アラームが既に設定されているかどうかを知りたい場合は、新しいアラームを作成するか、 –

3

私は、adbシェルからロングを抽出し、タイムスタンプに変換して赤で表示する単純な(愚かなまたはそうでない)bashスクリプトを作った。

echo "Please set a search filter" 
read search 

adb shell dumpsys alarm | grep $search | (while read i; do echo $i; _DT=$(echo $i | grep -Eo 'when\s+([0-9]{10})' | tr -d '[[:alpha:][:space:]]'); if [ $_DT ]; then echo -e "\e[31m$(date -d @$_DT)\e[0m"; fi; done;) 

それを試して、それが偽のブール値を与えるように)

2
Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION"); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(
        sqlitewraper.context, 0, intent, 
        PendingIntent.FLAG_NO_CREATE); 

FLAG_NO_CREATEは保留中の意図を作成されていません。

  boolean alarmUp = (PendingIntent.getBroadcast(sqlitewraper.context, 0, 
        new Intent("com.my.package.MY_UNIQUE_ACTION"), 
        PendingIntent.FLAG_NO_CREATE) != null); 

      if (alarmUp) { 
       System.out.print("k"); 

      } 

      AlarmManager alarmManager = (AlarmManager) sqlitewraper.context 
        .getSystemService(Context.ALARM_SERVICE); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
        System.currentTimeMillis(), 1000 * 60, pendingIntent); 

AlarmManagerが意図保留中の値をチェックした後、それは保留中意向AlarmManagerを更新ザ・フラッグので真なります。

  boolean alarmUp1 = (PendingIntent.getBroadcast(sqlitewraper.context, 0, 
        new Intent("com.my.package.MY_UNIQUE_ACTION"), 
        PendingIntent.FLAG_UPDATE_CURRENT) != null); 
      if (alarmUp1) { 
       System.out.print("k"); 

      } 
5

はちょうど別の解決策を見つけ、あなたの問題を解決し、答えを検証したり、独自のソリューションを投稿してください私

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class); 

boolean isWorking = (PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_NO_CREATE) != null); 
if (isWorking) {Log.d("alarm", "is working");} else {Log.d("alarm", "is not working");} 

if(!isWorking) { 
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    int timeNotif = 5 * 60 * 1000;//time in ms, 7*24*60*60*1000 for 1 week 
    Log.d("Notif", "Notification every (ms): " + timeNotif); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), timeNotif, pendingIntent); 
    } 
+0

Marshmallowでは、アプリケーションを強制停止した後、getBroadcast()はnull以外を返しますが、アラームは設定されません。 – hopia

関連する問題