Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public void Alarm(){
  2. Intent intent = new Intent("AlarmService");
  3. PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
  4. long firstTime = SystemClock.elapsedRealtime() + 10 * 1000;
  5. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  6. if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
  7. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  8. am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, sender) ;
  9. } else {
  10. am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, sender);
  11. }
  12. } else {
  13. am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, sender);
  14. }
  15. }
  16.  
  17. BroadcastReceiver alarmReceiver;
  18. @Override
  19. public void onCreate() {
  20. // TODO Auto-generated method stub
  21. super.onCreate();
  22. alarmReceiver = new BroadcastReceiver(){
  23. @Override
  24. public void onReceive(Context context, Intent intent) {
  25. // TODO Auto-generated method stub
  26. count++;
  27. Toast.makeText(getApplicationContext(),count+"times",Toast.LENGTH_LONG);
  28. Alarm();
  29. }
  30. };
  31. registerReceiver(alarmReceiver, new IntentFilter("AlarmService"));
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement