Android alarm example

Alarm is a useful feature in Android. It allows you do action at a specific time or repeat the action at a time interval. For example, you can implement the alarm feature to check internet connection, or to notify the user when a new record is added to your database in every five minutes. The system alarm service still works although you exit the application. Uninstalling the application will remove the alarm service.


In this alarm example tip, I am going to show you how to use the alarm feature to create a notification to the user at a time interval. To accomplish the task, first you need to create a class that extends the BroadcastReceiver class. You will override the onReceive() method to generate a notification to the user when it receives a PendingIntent object sent from an AlarmManager instance. To generate a notification to the user, you need to create an instance of Notification that wraps the PendingIntent object by using NotificationCompat.Builder. Then use the notify() method of NotificationManager to make the notification come out.


package com.example.andtip;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;


public class BReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
   NotificationManager notificationManager;
   Notification myNotification;  
  
public void onReceive(Context c, Intent i) {
       PendingIntent pi = PendingIntent.getBroadcast(c, 0, new Intent("com.example.andtip"),0 );
       myNotification=new NotificationCompat.Builder(c)
     .setContentTitle("This is a notification from the example alarm application.")
       .setContentText("Notification")      
       .setTicker("Notification!")
       .setWhen(System.currentTimeMillis())
     .setContentIntent(pi)
       .setDefaults(Notification.DEFAULT_SOUND)               
       .setAutoCancel(true)
       .setSmallIcon(R.drawable.ic_launcher)
     .build();
        
       notificationManager = (NotificationManager)c.getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
      
      }
}

The BReceiver class does not receive the PendingIntent object sent from the instance of AlarmManager unless you define a receiver in the AndroidManifest.xml. You will add the following code to the AndroidManifest.xml file above the closing tag of the application (</application>). You need to change the package name to your package name. In this example, my package name is com.example.andtip.

<receiver android:name="com.example.andtip.BReceiver" >
     <intent-filter>
       <action android:name="com.example.andtip" />
     </intent-filter>
</receiver>


In the MainActivity class, you will write code to do the alarm setup task. First, you need to create a PendingIntent object that wraps the package name. Then create an AlarmManager instance with the getSystemService method. Finally, call the setRepeating method of the AlarmManager to send the PendingIntent object at a time interval. If you want the alarm to fire an action at a specific time rather than repeating the action at the time interval, you need to use the set method instead of the setRepeating method.

package com.example.andtip;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
PendingIntent pi;
AlarmManager amanager;
   protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
  
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }
  
   public void onStart(){
   super.onStart();
   setUpAlarm(1);
  
   }
  
   public void setUpAlarm(int n){
   //create an instance of PendingIntent
pi = PendingIntent.getBroadcast( this, 0, new Intent("com.example.andtip"),0 );
//get AlarmManager instance
    amanager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE ));
   //get a Calendar object with current time
   Calendar cal = Calendar.getInstance();
   //Alarm repeating in every n minutes
   amanager.setRepeating( AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), n* 60*1000, pi );
     

}
   }


Adroid-alarm-example

Posted by: Dara | post date: 06-23-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.