14

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 not need to have any permission to use the Alarm Manager Service.

Is that true?

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
tomtu
  • 181
  • 1
  • 1
  • 6

5 Answers5

13

Yes, it is true. You do not have to add any special service. Keep in mind that when the handset is restarted the alarms you have set will be lost, so you may want to re-schedule them at boot time, which requires the android.permission.RECEIVE_BOOT_COMPLETED permission.

Cristian
  • 198,401
  • 62
  • 356
  • 264
9

I dont know why not any one mention this permission

But according to android documentation , you should use SET_ALARM permission

Documentation

Allows an application to broadcast an Intent to set an alarm for the user.

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
0

It wakes CPU every 10 minutes until the phone turns off.

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

<receiver  android:process=":remote" android:name="Alarm"></receiver>

If you want set alarm repeating at phone boot time:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>

For more details : Alarm Manager Example

Community
  • 1
  • 1
Ponmalar
  • 6,871
  • 10
  • 50
  • 80
0

Add to Manifest.xml:

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

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:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <receiver android:name=".AutoStart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
</receiver>
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
kaushikSuman
  • 381
  • 2
  • 18
  • I just did a test as your code, but I encountered an error: V/onPause:(10099): on pause V/updateReceiver(10099): update the apps D/AndroidRuntime(10099): Shutting down VM W/dalvikvm(10099): threadid=1: thread exiting with uncaught exception (group=0x4001e578) E/AndroidRuntime(10099): FATAL EXCEPTION: main E/AndroidRuntime(10099): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.tcm.alarm.intent.UPDATE flg=0x40000004 (has extras) } in com.tcm.alarm.AlarmActivity$updateReceiver@406ccfb0 E/AndroidRuntime(10099): at – tomtu Jun 28 '12 at 11:44
  • why I could not input Enter to form the comment – tomtu Jun 28 '12 at 11:48
-2

Would like to add few bits to what Cristian Said

Even if you use android.permission.RECEIVE_BOOT_COMPLETED permission your application will run properly on 2.X.X devices.

But in 4.x devices the broadvast receiver will not work on Boot until and unless you start application manually

Vipul
  • 27,808
  • 7
  • 60
  • 75