2

一度発火すると通知を起動する繰り返しアラームを設定する必要があります。

手順:

  1. 日付の詳細を取得するインターフェイスがあります(考えただけで、「アラームの設定」機能を表示するインテントを起動する必要があるかもしれません)。詳細の積は、アラームメソッドに入力する必要のある日時文字列です。
  2. アラームを設定するときは、ユーザーが削除できるように、アラームへの参照を作成する必要があります。(またはそうでない場合は、ベストプラクティスは何ですか?ユーザーは「アラーム」セクションからのみアラームを削除できる可能性があります)
  3. アラームが鳴ると、通知が作成されます。

警報システムがどのように機能するのかわかりません。アラームはおそらく無音で、おそらく振動があります。セットアップは簡単ですか?

サービスが必要ですか、それとも放送受信者がその仕事をしますか?

基本的に、私はいくつかのポインタが必要です。私はこれについて正しく考えていますか?そこにチュートリアルはありますか(私は何も見つかりませんでした)。前もって感謝します。

4

1 に答える 1

11

アプリで AlarmService を使用する方法のウォークスルーを次に示します。

  1. x 分で起動するように AlarmManager を設定します。

  2. アラームに応答して、サービスを開始します。

  3. 通知を作成し、サービスに新しいアラームを設定して、さらに x 分後に再び起動させます。

  4. サービス自体がシャットダウンします。

1.

    Intent alarmIntent = new Intent(this, MyAlarm.class);
    long scTime = 60* 10000;// 10 minutes
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent);

2.

    public class MyAlarm extends BroadcastReceiver
    {

       @Override
       public void onReceive(Context context, Intent intent) {
          Log.d("Alarm Recieved!", "YAAAY");
          Intent i = new Intent(context, InviteService.class);
          context.startService(i);
       }
    }

3.

      public class InviteService extends IntentService
 {

  /** 
   * A constructor is required, and must call the super IntentService(String)
   * constructor with a name for the worker thread.
   */

  public InviteService() {
      super("InviteService");
  }

  /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */

  @Override
  protected void onHandleIntent(Intent intent) {

                  String ns = Context.NOTIFICATION_SERVICE;
                  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

                  int icon = R.drawable.logo;
                  CharSequence tickerText = "New Invite!";
                  long when = System.currentTimeMillis();

                  Notification notification = new Notification(icon, tickerText, when);
                  notification.flags |= Notification.FLAG_AUTO_CANCEL;
                  notification.defaults |= Notification.DEFAULT_VIBRATE;

                  Context context = getApplicationContext();
                  CharSequence contentTitle = "Title";
                  CharSequence contentText = "Text";
                  Intent notificationIntent = new Intent(this, Destination.class);
                  Bundle partyBundle = new Bundle();                    

                  PendingIntent contentIntent = PendingIntent.getActivity(this, SOME_ID, notificationIntent, 0);

                  notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

                  int NOTIFICATION_ID = SOME_ID;

                  Log.d("NOTIFICATION_ID", "" + NOTIFICATION_ID);
                  mNotificationManager.notify(NOTIFICATION_ID, notification);

4.(同じクラスで)

      Intent alarmIntent = new Intent(this, MyAlarm.class);
      long scTime = 60*1000;//mins
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent);

      stopService(intent);
      }
    }

お役に立てれば!

編集

サービスを使用する理由

BroadcastReceiver で多くの処理を行うのは賢明ではありません。BroadcastReciever で何らかの処理を行うことはできますが、Service でこれを行う方が安全です。この StackOverflow question BroadcastReceiver vs Serviceでいくつかの情報を見つけることができます。

于 2012-08-30T16:17:25.687 に答える