Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm Manager Example

I want to implement a schedule function in my project. So I Googled for an Alarm manager program but I can`t find any examples.

Can anyone help me with a basic alarm manager program?

like image 359
Rajamohan Sugumaran Avatar asked Dec 16 '10 09:12

Rajamohan Sugumaran


People also ask

What is an alarm Manager?

android.app.AlarmManager. This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.

How do I set up an alarm manager?

This example demonstrates how do I implement alarm manager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How does an application get access to the alarm manager Mcq?

How does an application get access to the AlarmManager? Use the AlarmManager() constructor to create an instance of the AlarmManager. Use the AlarmManager. newInstance() method to retrieve the singleton instance of the AlarmManager.


1 Answers

This is working code. It wakes CPU every 10 minutes until the phone turns off.

Add to Manifest.xml:

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

Code in your class:

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; import android.widget.Toast;  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, "");         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);     } } 

Set Alarm from Service:

package yourPackage;  import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder;  public class YourService extends Service {     Alarm alarm = new Alarm();     public void onCreate()     {         super.onCreate();            }      @Override     public int onStartCommand(Intent intent, int flags, int startId)      {         alarm.setAlarm(this);         return START_STICKY;     }     @Override            public void onStart(Intent intent, int startId)     {         alarm.setAlarm(this);     }      @Override     public IBinder onBind(Intent intent)      {         return null;     } } 

If you want to set alarm repeating at phone boot time:

Add permission and the service 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> ... <service         android:name=".YourService"         android:enabled="true"         android:process=":your_service" > </service> 

And create a new class:

package yourPackage;  import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;  public class AutoStart extends BroadcastReceiver {        Alarm alarm = new Alarm();     @Override     public void onReceive(Context context, Intent intent)     {            if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))         {             alarm.setAlarm(context);         }     } } 
like image 144
XXX Avatar answered Oct 09 '22 02:10

XXX