0

データベースに時間が保存されている場合、12時間ごとに通知を表示するAndroidアプリを開発しています。そのため、データベースにデータを入力または編集するたびに、現在のアラームマネージャーをキャンセルし、新しいアラームマネージャーを開始して、見逃さないようにします。また、再起動時に、alarmmanagerを呼び出しました。ブロードキャストレシーバーでは、データベースのエントリがチェックされ、見つかった場合は通知が設定され、アプリが自動的に開かれます。

そのため、日付を手動で変更してアプリをテストすると、アプリは期待どおりに機能します。再起動してもアプリは機能します。ただし、アプリを14時間近くアイドル状態にしておくと、通知は設定されませんが、アプリを開いてその後、通知が設定されるように一時停止します。

これが私がalarmmanagerと呼ぶ方法です。インテントalarmintent=new Intent(context、package.Alarm_Manager.class);

    alarmintent.putExtra("note","Notify");
    sender = PendingIntent.getBroadcast(context , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);            
    alarm_manger = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarm_manger.cancel(sender);
    Calendar cal = Calendar.getInstance();
    long now = cal.getTimeInMillis();
    alarmintent = new Intent(context, package.Alarm_Manager.class);
    alarmintent.putExtra("note","Notification");
    sender = PendingIntent.getBroadcast(context , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);            
    alarm_manger = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm_manger.setRepeating(AlarmManager.RTC_WAKEUP, now, AlarmManager.INTERVAL_HALF_DAY, sender);

これは放送受信機です

@Override
public void onReceive(Context context, Intent intent)
{
       NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
       Calendar cal = Calendar.getInstance();
       date = (int)(cal.getTimeInMillis()/1000);
       Notification notification = new Notification(R.drawable.vlcsnap_396460 , "Notify" , System.currentTimeMillis());
       PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
       notification.setLatestEventInfo(context, "App", "Notify" , contentIntent);
       notification.flags = Notification.FLAG_INSISTENT;
       manger.notify( 0 , notification);
   }
4

2 に答える 2

0

alarm_manager.cancel(sender);を設定した場合、電話をかける必要はありません。PendingIntent.FLAG_CANCEL_CURRENT.

への電話

alarm_manger.setRepeating(AlarmManager.RTC_WAKEUP, now, AlarmManager.INTERVAL_HALF_DAY, sender);

アラームを設定した時点で既に経過しているため、すぐにアラームがトリガーされます。

使用することをお勧めします

now + DateUtils.HOUR_IN_MILLIS / 2 

triggerAtMillis パラメータの

より短い間隔でスケジュールしようとしましたか? トリガーされますか?

于 2013-01-24T10:03:00.150 に答える
0

Alarm_Manager コードを見た後、BroadcastReceiver オブジェクトでこれを直接行うのは違法だと思います。見積もり:

この BroadcastReceiver がタグを介して起動された場合、この関数から戻った後、オブジェクトは生きていません。

setLatestEventInfo()BroadcastReceiver から通知される Service を作成し、Service がそれ自体 ( this) を Context として呼び出していることを確認する以外に方法はないと思います。

アプリの実行中に非同期 Broadcast が機能しているときに失敗する理由は、おそらく、BroadcastReceiver に提供された Context が、アプリが実行されていないときに BroadcastReceiver への呼び出しの間だけ存続するためです。そのため、BroadcastReceiver が一時的なコンテキストと共に終了した後にのみ実行される通知サービスには、有効なコンテキストがありません。

アプリが実行されると、ブロードキャストにはおそらくアクティビティまたはアプリケーション オブジェクトがコンテキストとして付属します。これは、通知マネージャーが実行されるときにも有効です。

お役に立てれば。

更新: IntentService で十分です。そのためのフルタイムのサービスは必要ありません。

更新 2:いくつかのスニペット。

<service android:name=".MyIntentService" android:label="@string/my_intent_service_name" />

public final class MyIntentService extends IntentService {
    public MyIntentService() {
    super("service name");
    // set any properties you need
    }
    @Override
    public void onCreate() {
        super.onCreate();
        // do init, e.g. get reference to notification service
    }
    @Override
    protected void onHandleIntent(Intent intent) {
    // handle the intent
        // ...especially:
        notification.setLatestEventInfo(this, "App", "Notify" , contentIntent);
        // ...
    }
}

public final class MyAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, MyIntentService.class));
    }
}
于 2013-01-24T10:09:01.003 に答える