Pages

Monday, July 23, 2012

How to setUp a repeating Alarm in Android?

Today I am going to show you how to set up a repeating alarm in android. In the previous post I showed you how to set up a simple alarm. An in this post I will show you how to set Up a Repeating Alarm.


For this you need an extra class just to notify that the repeating alarm is called.
Let’a look at the java code.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pack.coderzheaven;
 
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.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class AlarmDemo extends Activity {
    Toast mToast;
      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
 
            Button button = (Button)findViewById(R.id.one_shot);
            button.setOnClickListener(mOneShotListener);
            button = (Button)findViewById(R.id.start_repeating);
            button.setOnClickListener(mStartRepeatingListener);
            button = (Button)findViewById(R.id.stop_repeating);
            button.setOnClickListener(mStopRepeatingListener);
        }
 
        private OnClickListener mOneShotListener = new OnClickListener() {
            public void onClick(View v) {
 
                Intent intent = new Intent(AlarmDemo.this, MyAlarmReceiver.class);
                PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this,
                        0, intent, 0);
 
                // We want the alarm to go off 10 seconds from now.
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND, 10);
 
                // Schedule the alarm!
                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
 
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(AlarmDemo.this,"Alarm Started",
                        Toast.LENGTH_LONG);
                mToast.show();
            }
        };
 
        private OnClickListener mStartRepeatingListener = new OnClickListener() {
            public void onClick(View v) {
 
                Intent intent = new Intent(AlarmDemo.this, RepeatingAlarm.class);
                PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this,
                        0, intent, 0);
 
                // We want the alarm to go off 5 seconds from now.
                long firstTime = SystemClock.elapsedRealtime();
                firstTime += 5*1000;
 
                // Schedule the alarm!
                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                                firstTime, 5*1000, sender);
 
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(AlarmDemo.this, "Rescheduled",
                        Toast.LENGTH_LONG);
                mToast.show();
            }
        };
 
        private OnClickListener mStopRepeatingListener = new OnClickListener() {
            public void onClick(View v) {
                // Create the same intent, and thus a matching IntentSender, for
                // the one that was scheduled.
                Intent intent = new Intent(AlarmDemo.this, RepeatingAlarm.class);
                PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this,
                        0, intent, 0);
 
                // And cancel the alarm.
                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.cancel(sender);
 
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(AlarmDemo.this,"Repeating Alarm Unscheduled.",
                        Toast.LENGTH_LONG);
                mToast.show();
            }
        };
    }
Here we have three buttons one to set up a simple Alarm that is a one shot alarm and other button is for starting a repeating alarm and the third button for stopping the repeating alarm.
Now let’s see how the layout looks like.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
 
    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="Alarm Demo from CoderzHeaven"/>
 
    <Button android:id="@+id/one_shot"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Start Alarm">
        <requestFocus />
    </Button>
 
    <Button android:id="@+id/start_repeating"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Start Repeating Alarm" />
 
     <Button android:id="@+id/stop_repeating"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Stop Repeating Alarm" />
 
</LinearLayout>
Now create a new class named “RepeatingAlarm.java” and copy this code into it. This class receives the repeating alarm intent.
The “MyAlarmReceiver.java” file is included in this post. Please copy that also
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package pack.coderzheaven;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
 
public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context,"Repeating Alarm Received", Toast.LENGTH_SHORT).show();
    }
}
Here is the AndroidManifest file
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AlarmDemo"
                  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=".MyAlarmReceiver" android:process=":remote" />
    <receiver android:name=".RepeatingAlarm" android:process=":remote" />
    </application>
</manifest>
Now run it and see the result.



No comments:

Post a Comment

Popular Posts