Program description:
This program will demo how to create alarms which will repeat at irregular intervals.
This program has one activity with one button, and a broadcast receiver.
On clicking the button, we will set an alarm for next 1 second, which triggers broadcast receiver.
Our program aim is to repeat next alarm at 2 seconds, next at 4 seconds, next at 8 seconds and so on.. For this requirement we can't use setRepeating() function of alarm manager.
We have to set the alarm manually in the receiver.
Once the first alarm (which was set for 1 second) is triggered, it will call broadcast receiver. In the receiver set alarm for next 2 seconds from that time. Which will again call same broadcast receiver after 2 seconds. Again set one more alarm for next 4 seconds in that receiver. For this we have to write a generic logic, because alarm has to repeat for n number of times.
First Activity:
This program will demo how to create alarms which will repeat at irregular intervals.
This program has one activity with one button, and a broadcast receiver.
On clicking the button, we will set an alarm for next 1 second, which triggers broadcast receiver.
Our program aim is to repeat next alarm at 2 seconds, next at 4 seconds, next at 8 seconds and so on.. For this requirement we can't use setRepeating() function of alarm manager.
We have to set the alarm manually in the receiver.
Once the first alarm (which was set for 1 second) is triggered, it will call broadcast receiver. In the receiver set alarm for next 2 seconds from that time. Which will again call same broadcast receiver after 2 seconds. Again set one more alarm for next 4 seconds in that receiver. For this we have to write a generic logic, because alarm has to repeat for n number of times.
First Activity:
package com.techpalle.b15_alarm_asg2; import android.os.Bundle; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { AlarmManager am; Button b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); b = (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent in = new Intent(); in.setAction("com.techpalle.action.ALARM_INDEF"); int i=1; in.putExtra("val", i); PendingIntent pi = PendingIntent.getBroadcast( getApplicationContext(), 0, in, 0); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, 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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub return super.onOptionsItemSelected(item); } }
Broadcast receiver
File name : AlarmRecvr.java
package com.techpalle.b15_alarm_asg2; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.view.ViewDebug.FlagToString; import android.widget.Toast; public class AlarmRecvr extends BroadcastReceiver { //public static int i = 1; @Override public void onReceive(Context context, Intent intent) { int i = intent.getExtras().getInt("val"); i = i*2; Toast.makeText(context, "Got", 0).show(); Intent in = new Intent(); in.setAction("com.techpalle.action.ALARM_INDEF"); in.putExtra("val", i); PendingIntent pi = PendingIntent.getBroadcast( context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i*1000), pi); } }
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_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="41dp"
android:layout_marginTop="70dp"
android:text="Start alarms.." />
</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_asg2"
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_asg2.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_INDEF"/>
</intent-filter>
</receiver>
</application>
</manifest>
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?
Can i make alarm to trigger at different times on different dates.plz help me to do this.
ReplyDeleteMadhuri, Yes it is possible.
ReplyDeletethe latest release of their own version of the activity tracker. Complete Alarms
ReplyDelete