which permissions an android application need in order to use the Alarm Manager Service?

后端 未结 5 1576
小蘑菇
小蘑菇 2021-02-19 20:17

If an android application wants to use the Alarm Manager Service, then which permissions the application needs to have?

I have tested that it seems that application does

5条回答
  •  滥情空心
    2021-02-19 20:38

    Add to Manifest.xml:

    
    ...
    
    

    code:

      package YourPackage;
        import android.app.AlarmManager;
        import android.app.PendingIntent;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.os.PowerManager;
    
    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, "YOUR TAG");
                 wl.acquire();
    
                 // Put here YOUR code.
                 Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
    
                 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); // Millisec * Second * Minute
         }
    
         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);
         }
        }
    

    If you want set alarm repeating at phone boot time:

    Add permission to Manifest.xml:

    
        
            
                
            
    
    

提交回复
热议问题