質問

開始から 20 分後にコードのブロックをトリガーする必要があります。 AlarmManager 設定されています。

誰かが使用方法のサンプルコードを見せてくれませんか AlarmManager Androidでは?

数日間コードをいじってみましたが、うまくいきません。

役に立ちましたか?

解決

「いくつかのサンプルコード」それはAlarmManagerに来るときそれは容易ではありません。

ここでAlarmManagerのセットアップを示すスニペットます:

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);

この例では、私はsetRepeating()を使用しています。あなたはワンショットアラームをしたい場合は、あなただけのset()を使用します。あなたがset()する初期パラメータに使うとアラームが同じ時間ベースで起動するための時間を与えるようにしてください。上記の私の例では、私はAlarmManager.ELAPSED_REALTIME_WAKEUPを使用していますので、私のタイムベースはSystemClock.elapsedRealtime()です。

ここでは、この技術を示すのより大きなサンプルプロジェクトがあります。

他のヒント

Android のサンプルコードには良い例がいくつかあります

.\android-sdk\samples\android-10\ApiDemos\src\com\example\android\apis\app

チェックすべきものは次のとおりです。

  • アラームコントローラー.java
  • ワンショットアラーム.java

まず、アラームが鳴ったときにそれを聞くことができる受信機が必要です。AndroidManifest.xml ファイルに以下を追加します

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

次に、次のクラスを作成します

public class MyAlarmReceiver extends BroadcastReceiver { 
     @Override
     public void onReceive(Context context, Intent intent) {
         Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
     }
}

次に、アラームをトリガーするには、次のコマンドを使用します (たとえば、メイン アクティビティで)。

AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);

.


あるいは、さらに良いのは、すべてを処理するクラスを作成して、次のように使用することです。

Bundle bundle = new Bundle();
// add extras here..
MyAlarm alarm = new MyAlarm(this, bundle, 30);

こうすることで、すべてを 1 か所にまとめられます (編集することを忘れないでください)。 AndroidManifest.xml)

public class MyAlarm extends BroadcastReceiver {
    private final String REMINDER_BUNDLE = "MyReminderBundle"; 

    // this constructor is called by the alarm manager.
    public MyAlarm(){ }

    // you can use this constructor to create the alarm. 
    //  Just pass in the main activity as the context, 
    //  any extras you'd like to get later when triggered 
    //  and the timeout
     public MyAlarm(Context context, Bundle extras, int timeoutInSeconds){
         AlarmManager alarmMgr = 
             (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent intent = new Intent(context, MyAlarm.class);
         intent.putExtra(REMINDER_BUNDLE, extras);
         PendingIntent pendingIntent =
             PendingIntent.getBroadcast(context, 0, intent, 
             PendingIntent.FLAG_UPDATE_CURRENT);
         Calendar time = Calendar.getInstance();
         time.setTimeInMillis(System.currentTimeMillis());
         time.add(Calendar.SECOND, timeoutInSeconds);
         alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
                      pendingIntent);
     }

      @Override
     public void onReceive(Context context, Intent intent) {
         // here you can get the extras you passed in when creating the alarm
         //intent.getBundleExtra(REMINDER_BUNDLE));

         Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
     }
}

行う必要があるのは、まずスケジュールする必要があるインテントを作成することです。次に、そのインテントの pendingIntent を取得します。アクティビティ、サービス、ブロードキャストをスケジュールできます。 MyActivity などのアクティビティをスケジュールするには:

  Intent i = new Intent(getApplicationContext(), MyActivity.class);
  PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),3333,i,
  PendingIntent.FLAG_CANCEL_CURRENT);

この pendingIntent を AlarmManager に与えます。

  //getting current time and add 5 seconds in it
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.SECOND, 5);
  //registering our pending intent with alarmmanager
  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), pi);

これで MyActivity は次のようになります アプリケーションを停止したりデバイスがスリープ状態になったりしても、アプリケーションの起動から 5 秒後に起動されます。 (RTC_WAKEUP オプションによる)。完全なサンプルコードを読むことができます アクティビティ、サービス、ブロードキャストのスケジュール #Android

私はコメントすることを望んだが、<50の担当者は、そうここに行きます。

:あなたが5.1または上で実行しているとあなたが分未満の間隔を使用している場合、この問題が発生したことをフレンドリーリマインダー
Suspiciously short interval 5000 millis; expanding to 60 seconds

ここを参照してください。

あなたはAlarmmanagerからサービスを呼び出したいいくつかのサンプルコード:

PendingIntent pi;
AlarmManager mgr;
mgr = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(DataCollectionActivity.this, HUJIDataCollectionService.class);    
pi = PendingIntent.getService(DataCollectionActivity.this, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() , 1000, pi);

あなたはいけないuserpermissionsを依頼する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top