Using AlarmManager to start a Scheduled Activity

In this exercise, we are going to implement alarmManager to start a Scheduled Activity.

The main activity(AndroidScheduledActivity.java) start a AlarmManager to trigger BroadcastReceiver(MyScheduledReceiver.java) repeatly. In the onReceive() method of MyScheduledReceiver, it start another activity(MyScheduledActivity.java) indirectly. Such that the activity(MyScheduledActivity.java) will be start in scheduled interval.

main.xml, layout of the main activity

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Scheduled Activity"
/>


main activity, AndroidScheduledActivity.java
package com.exercise.AndroidScheduledActivity;

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.View;
import android.widget.Button;

public class AndroidScheduledActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStart = (Button)findViewById(R.id.start);
buttonStart.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getBaseContext(),
MyScheduledReceiver.class);

PendingIntent pendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);

AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pendingIntent);
finish();
}});
}

}


BroadcastReceiver, MyScheduledReceiver.java

package com.exercise.AndroidScheduledActivity;

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

public class MyScheduledReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);

}

}


Layout of the scheduled activity, layout_scheduledactivity.xml

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="--- Scheduled Activity ---"
/>
android:id="@+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Dismiss"
/>


Scheduled activity, MyScheduledActivity.java
package com.exercise.AndroidScheduledActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyScheduledActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scheduledactivity);

Button buttonDismiss = (Button)findViewById(R.id.dismiss);

buttonDismiss.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}});
}

}


Finally, update AndroidManifest.xml with new activity of MyScheduledActivity, and new receiver of MyScheduledReceiver.

package="com.exercise.AndroidScheduledActivity"
android:versionCode="1"
android:versionName="1.0">



android:label="@string/app_name">






android:name="MyScheduledReceiver" />



download filesDownload the files.

Next:
- Cancel Alarm using alarmManager.cancel(pendingIntent)

Related Article:
- A simple example of Alarm Service, using AlarmManager

Post a Comment

Previous Post Next Post

Contact Form