Set an Alarm from My application

前端 未结 2 924
借酒劲吻你
借酒劲吻你 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 11:57

    This is working code:

    Add to Manifest.xml:

    ...
    
    ...
    
    ...
    

    Code:

    public class Alarm extends BroadcastReceiver 
    {    
         @Override
         public void onReceive(Context context, Intent intent) 
         {   
             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
             wl.acquire();
    
             Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show();
    
             wl.release();
         }
    
         public void SetAlarm(Context context)
         {
             AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             Intent i = new Intent(context, Alarm.class);
             PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
             am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi);
         }
    
         public void CancelAlarm(Context context)
         {
             Intent intent = new Intent(context, Alarm.class);
             PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
             AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
             alarmManager.cancel(sender);
         }
    }
    

提交回复
热议问题