Set an Alarm from My application

前端 未结 2 909
借酒劲吻你
借酒劲吻你 2021-02-06 11:20

I want to create an alarm object from my application. I am writing a To-Do application which will have an option to set an Alarm on the phone.

I wanna set the Date and T

2条回答
  •  囚心锁ツ
    2021-02-06 12:23

    As @stealthcopter said, the AlarmManager is used to raise an Alarm your application can catch and then do something. Here is a little example I threw together from other posts, tutorials, and work I've done.

    Main.java

    Intent i = new Intent(this, OnAlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i,
                                                         PendingIntent.FLAG_ONE_SHOT);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 10);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
    

    OnAlarmReceiver.java

    public class OnAlarmReceiver extends BroadcastReceiver{
    
    @Override
    public void onReceive(Context context, Intent intent) {
        WakeIntentService.acquireStaticLock(context);
        Intent i = new Intent(context, AlarmService.class);
        context.startService(i);
    }}
    

    WakeIntentService.java

    public abstract class WakeIntentService extends IntentService {
    
        abstract void doReminderWork(Intent intent);
    
        public static final String LOCK_NAME_STATIC = "com.android.voodootv.static";
        private static PowerManager.WakeLock lockStatic = null;
    
        public static void acquireStaticLock(Context context) {
            getLock(context).acquire();
        }
    
        synchronized private static PowerManager.WakeLock getLock(Context context) {
            if (lockStatic == null) {
                PowerManager powManager = (PowerManager) context
                        .getSystemService(Context.POWER_SERVICE);
                lockStatic = powManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        LOCK_NAME_STATIC);
                lockStatic.setReferenceCounted(true);
            }
            return (lockStatic);
        }
    
        public WakeIntentService(String name) {
            super(name);
        }
    
        @Override
        final protected void onHandleIntent(Intent intent) {
            try {
                doReminderWork(intent);
            } finally {
                getLock(this).release();
            }
        }}
    

    AlarmService.java

    public class AlarmService extends WakeIntentService {
    
        public AlarmService() {
            super("AlarmService");
        }
    
        @Override
        void doReminderWork(Intent intent) {
            NotificationManager manager = (NotificationManager)
                    getSystemService(NOTIFICATION_SERVICE);
            Intent notificationIntent = new Intent(this, Main.class);
            PendingIntent pi = PendingIntent.getActivity(this, 0,
                    notificationIntent, PendingIntent.FLAG_ONE_SHOT);
            Notification note = new Notification(R.drawable.icon, "Alarm",
                    System.currentTimeMillis());
            note.setLatestEventInfo(this, "Title", "Text", pi);
            note.defaults |= Notification.DEFAULT_ALL;
            note.flags |= Notification.FLAG_AUTO_CANCEL;
            int id = 123456789;
            manager.notify(id, note);
        }
    }
    

    This example will create a notification on the status bar after 10 seconds.

    Hope it helps.

    Also First post here :)

    Oh almost forgot,

    AndroidManifest.xml

    
    
    
    
    

提交回复
热议问题