开发者

Alarm Notification

开发者 https://www.devze.com 2023-03-05 21:47 出处:网络
can anyone now to how to play notification or sound or vibration using Alar开发者_如何学PythonmManager/service for a particular time and for a selected interval of time. Like from 6:00 PM to 9:00 Pm w

can anyone now to how to play notification or sound or vibration using Alar开发者_如何学PythonmManager/service for a particular time and for a selected interval of time. Like from 6:00 PM to 9:00 Pm with regular interval of 15 mins


Add to Manifest.xml:

...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver  android:process=":remote" android:name="Alarm"></receiver>
...

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();
         // Put here your vibration and sound notifycation 
         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 * 15, 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);
     }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号