-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.java
More file actions
105 lines (95 loc) · 3.2 KB
/
Timer.java
File metadata and controls
105 lines (95 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.efalk.util;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* Schedule and receive periodic alarms.
* Don't forget to register this in the manifest
* <receiver android:name="Timer" android:enabled="true" />
*
* Modify (or subclass) this to be useful.
*/
public class Timer extends BroadcastReceiver {
static private final String TAG = "Timer";
/**
* Modify or override this in a subclass to be useful.
*/
@Override
public void onReceive(Context ctx, Intent intent) {
Toast toast = Toast.makeText(ctx, "Alarm dingding", Toast.LENGTH_LONG);
Log.d(TAG, "Alarm dingding");
toast.show();
}
/**
* Schedule a one-time notification.
* @param ctx Context
* @param atTime Time (in ms) when the notification should occur
* @param pi PendingIntent to use; may be null
* @return the PendingIntent
* atTime is relative to System.currentTimeMillis()
* If pi is null, a new PendingIntent will be created.
*/
public static PendingIntent schedule(Context ctx, long atTime,
PendingIntent pi)
{
if (pi == null)
pi = getIntent(ctx);
AlarmManager mgr =
(AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC_WAKEUP, atTime, pi);
return pi;
}
/**
* Schedule a one-time notification.
* @param ctx Context
* @param atTime Time (in ms) when the notification should occur
* @return PendingIntent used for the notification
* The PendingIntent can be re-used
*/
public static PendingIntent schedule(Context ctx, long atTime)
{
return schedule(ctx, atTime, null);
}
/**
* Schedule a repeating notification.
* @param ctx Context
* @param atTime Time (in ms) when the notification should occur
* @param interval Time (in ms) between notifications
* @param pi PendingIntent to use; may be null
* @return the PendingIntent
* Use the returned PendingIntent to cancel the notification.
* If pi is null, a new PendingIntent will be created.
*/
public static void schedule(Context ctx, long atTime, long interval,
PendingIntent pi)
{
if (pi == null)
pi = getIntent(ctx);
AlarmManager mgr =
(AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, atTime, interval, pi);
}
/**
* Cancel a notification.
* @param ctx Context -- any context will do
* @param pi PendingIntent to cancel
* Use the returned PendingIntent to cancel the notification.
* If pi is null, a new PendingIntent will be created.
*/
public static void cancel(Context ctx, PendingIntent pi) {
AlarmManager mgr =
(AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
mgr.cancel(pi);
}
/**
* Utility: return a PendingIntent that will launch this receiver
*/
public static PendingIntent getIntent(Context ctx) {
Intent intent = new Intent(ctx, Timer.class);
return PendingIntent.getBroadcast(ctx, 0, intent, 0);
}
}