Wednesday, January 29, 2014

Repeating alarms : Android tutorial

Program description:

This program will demo how to create repeating alarms, which will trigger broadcast receivers.


This program has one activity with one button, and a broadcast receiver.
On clicking the button we will set an alarm which will trigger after 5 seconds from now.
That alarm will keep repeating for every next 5 seconds. Every time it triggers it will send a broadcast receiver. Broadcast receiver will print "got" message for every 5 seconds.

Before using this program, you should know  how to create alarms with broadcast receiver.

First Activity


package com.techpalle.b15_alarm_asg1;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
 AlarmManager am;
 Button b;
 ActivityManager acm;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  acm = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  List l = acm.getRunningTasks(10);

  b = (Button) findViewById(R.id.button1);
  am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent in = new Intent();
    in.setAction("com.techpalle.action.ALARM_REPEAT");
    PendingIntent pi = PendingIntent.getBroadcast(
      getApplicationContext(),
      0, in, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, 
      System.currentTimeMillis()+5000,
      5000, 
      pi);
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

Broadcast receiver 
File name : AlarmRecvr.java
package com.techpalle.b15_alarm_asg1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmRecvr extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
  Toast.makeText(context, "got", 0).show();
 }

}

xml file for first activity 
File name : activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/hello_world" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_below="@+id/textView1"  
     android:layout_toRightOf="@+id/textView1"  
     android:text="Start repeating.." />  
 </RelativeLayout>  

Manifest file
File name : AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.techpalle.b15_alarm_asg1"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.techpalle.b15_alarm_asg1.MainActivity"  
       android:label="@string/app_name">  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
     <receiver android:name="AlarmRecvr">  
       <intent-filter >  
         <action android:name="com.techpalle.action.ALARM_REPEAT"/>  
       </intent-filter>  
     </receiver>  
   </application>  
 </manifest>  
hi

Download complete code : Click to download

Android interview questions on Power manager and wake locks:
What is sleep mode? What will happen to CPU when LCD is turned off?
What are the two components that runs in Sleep mode also?
Every day night at 12 o clock I need to post some images to Facebook, in that case I will set repeating alarm for every day night 12 am. But to upload images I want to start service, how should I do this ?
What is the difference between Intent, Sticky Intent, and Pending Intent?

1 comment: