|
| 1 | +package com.jayfeng.lesscode.core; |
| 2 | + |
| 3 | +import android.app.AlarmManager; |
| 4 | +import android.app.PendingIntent; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | + |
| 8 | +public final class AlarmLess { |
| 9 | + |
| 10 | + /** |
| 11 | + * 开启定时器 |
| 12 | + * @param context |
| 13 | + * @param triggerAtMillis |
| 14 | + * @param pendingIntent |
| 15 | + */ |
| 16 | + public static void $startIntent(Context context, int triggerAtMillis, PendingIntent pendingIntent) { |
| 17 | + AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 18 | + manager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * 停止定时器 |
| 23 | + * @param context |
| 24 | + * @param pendingIntent |
| 25 | + */ |
| 26 | + public static void $stop(Context context, PendingIntent pendingIntent) { |
| 27 | + AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 28 | + manager.cancel(pendingIntent); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * 开启轮询闹钟 |
| 33 | + * @param context |
| 34 | + * @param triggerAtMillis |
| 35 | + * @param cls |
| 36 | + * @param action |
| 37 | + */ |
| 38 | + public static void $startAction(Context context, int triggerAtMillis, Class<?> cls, String action) { |
| 39 | + Intent intent = new Intent(context, cls); |
| 40 | + intent.setAction(action); |
| 41 | + PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| 42 | + $startIntent(context, triggerAtMillis, pendingIntent); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * 停止轮询闹钟 |
| 47 | + * @param context |
| 48 | + * @param cls |
| 49 | + * @param action |
| 50 | + */ |
| 51 | + public static void $stop(Context context, Class<?> cls, String action) { |
| 52 | + Intent intent = new Intent(context, cls); |
| 53 | + intent.setAction(action); |
| 54 | + PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| 55 | + $stop(context, pendingIntent); |
| 56 | + } |
| 57 | +} |
0 commit comments