1

アプリを閉じて2時間後に停止しても、10分ごとに通知を送信するアラームが必要です。本当に必要です。

私は私を助けてくれるように警戒している専門家が欲しいです、それは私のGPのために私は明日それを提出しなければなりません

どんな助けでもありがたいです

4

3 に答える 3

6

AlarmManagerの使用方法について非常に詳細な回答を書きました。ここで読むことができます。

あなたの特定のシナリオについて。cancel()メソッドstopSchedulingを使用して、スケジューラにメソッドを追加できます。

public class TaskScheduler {
    PendingIntent mPendingIntent;
    AlarmManager mAlarmManager;

    public static void startScheduling(Context context) {
            Intent intent = new Intent(context, MyReceiver.class);
            mPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 600, pendingIntent);
    }

    public static void stopScheduling() {
       mAlarmManager.cancel(mPendingIntent);
    }
}

したがって、スケジューリングを停止したい場合は、そのメソッドを呼び出すことができます。

2時間後に自動的に停止する簡単な方法はわかりませんが、AlarmManager2時間後に開始する別のスケジューラを作成して、最初のスケジューラを削除できます。

段階的に行うことをお勧めします。最初に単純なアラームを実装し、パラメーターをいじってスケジューリングがどのように機能するかを確認してから、このアラームを削除してみてください。これが機能したら、あとは基本的なプログラミング タスクにすぎないため、簡単に行うことができます。また、基本的なアラームの設定に必要なすべての情報が記載されているので、ここに書いた詳細な回答をお読みください。

于 2012-05-07T14:50:30.893 に答える
4

AlarmManagerで読む

これが例です

于 2012-05-07T14:47:47.537 に答える
2

As Tim and Amokrane said, The AlarmManager is your friend ;)

http://developer.android.com/reference/android/app/AlarmManager.html

Example:

    // get a Calendar object with current time
 Calendar cal = Calendar.getInstance();
 // add 5 minutes to the calendar object
 cal.add(Calendar.MINUTE, 5);
 Intent intent = new Intent(ctx, AlarmReceiver.class);
 intent.putExtra("alarm_message", "O'Doyle Rules!");
 // In reality, you would want to have a static variable for the request code instead of 192837
 PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 // Get the AlarmManager service
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
于 2012-05-07T14:54:05.873 に答える