0

アラーム マネージャからのサービスの実行に問題があります。

Facebook の友達の名前の日に所有者に通知するアプリを作成しています。すべてうまく機能しますが、通知は表示されません。

次のように、PendingIntent を作成して AlarmManager を設定する AlarmTask を設定しました。

public void run() {


    // Request to start are service when the alarm date is upon us

    Intent intent = new Intent(context, NotifyService.class);
    intent.putExtra(NotifyService.INTENT_NOTIFY, true);
    intent.putExtra("notifyID", ID);
    PendingIntent pendingIntent = PendingIntent.getService(context, ID, intent, 0);

    // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
    am.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
}

ID はすべての nameday に固有です。

今私の NotifyService で、これらを設定しました:

 @Override
public void onCreate() {
    super.onCreate();
    System.out.println("NOTIFICATION SERVICE onCreate()");
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    System.out.println("INTENT RECIEVED: " + intent + " " + flags + " " + startId);

    // If this service was started by out AlarmTask intent then we want to show our notification
    if(intent.getBooleanExtra(INTENT_NOTIFY, false)){
        int ID = intent.getIntExtra("notifyID", -1);
        showNotification(ID);
    }
    // We don't care if this service is stopped as we have already delivered our notification
    return START_STICKY;
}

アプリを起動すると両方のメソッドが一度実行されますが、通知が表示されても何も起こりません。

AlarmManager が本当に PendingIntent を実行するかどうかをテストする方法はありますか? むしろ IntentService を使用する必要がありますか? なぜ、どのように?

どうもありがとう。

次のように、BroadcastReciever に変更しようとしました。

public class NotificationBroadcastReciever extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("BROADCAST RECIEVED");

}

}

AlarmTask ビットは次のように変更されます。

Intent intent = new Intent("NotificationBroadcast");
    intent.putExtra(NotifyService.INTENT_NOTIFY, true);
    intent.putExtra("notifyID", ID);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), ID, intent, 0);
     System.out.println("date for notification: " + date.get(Calendar.DAY_OF_MONTH) + "." + date.get(Calendar.MONTH) + "." + date.get(Calendar.YEAR));
     System.out.println("epoch time in milils: " + date.getTimeInMillis());
    // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
    am.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);

関連するマニフェスト部分は次のようになります。

  <receiver 
        android:name="cz.cvut.kubispe2.jmeniny.NotificationBroadcastReciever" 
        android:exported="false">
        <intent-filter>
            <action android:name="NotificationBroadcast" />
        </intent-filter>
    </receiver>

設定される日付がエポック時間と等しいかどうかを確認しましたが、それでも onRecieve メソッドは呼び出されません。

4

3 に答える 3

2

アプリを起動すると両方のメソッドが一度実行されますが、通知が表示されても何も起こりません。

_WAKEUPアラームは、BroadcastReceiverではなくにルーティングされる場合にのみ、デバイスのウェイクアップが保証されServiceます。実行している時間が非常に短い (1 ~ 2 ミリ秒) 限り、その作業onReceive()BroadcastReceiver. あなたが現在行っている仕事Serviceは資格があります。

それを超えて、adb shell dumpsys alarmアラームが予定されていると思われる時間にスケジュールされていることを確認するために使用します。

むしろ IntentService を使用する必要がありますか?

Serviceそれは確かに、現在の実装でリークしている通常の よりも優れたオプションです。ただし、_WAKEUP制限は依然として保持されているため、ギャップを埋めるために を書きました。WakefulIntentService繰り返しますが、現在行っている作業が限られているため、 a を使用するだけBroadcastReceiverで十分です。

于 2013-04-30T14:07:32.173 に答える
0

最終的に解決したようです。ブロードキャストレシーバーを使用して、エラーの場所を見つけました。他のすべての引数は通常どおりに処理されるため、Calendar は月の引数を 1 ~ 12 ではなく 0 ~ 11 にします。 . だから、テストのときは、今日ではなく、5月末に通知を出していました。

とにかく、助けてくれてありがとう、とても感謝しています。

于 2013-04-30T21:46:11.603 に答える
0

アプリケーションコンテキストを使用してみてください。

PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), ID, intent, 0);

Androidログを操作します。次に、コンソールで実行されているかどうかを確認します

于 2013-04-30T13:53:07.490 に答える