[go: up one dir, main page]

0% found this document useful (0 votes)
97 views9 pages

Alarm Manager

The Android AlarmManager allows applications to schedule tasks to run at specific times in the future. It uses a wake lock to keep the CPU powered on to ensure scheduled broadcasts are handled even if the phone is asleep. Developers can use AlarmManager to schedule intents to launch activities or broadcast receivers at certain times by setting repeating or one-time alarms. The example code shows how to build an alarm application that plays an audio file after a delay entered by the user by setting an alarm with AlarmManager and having it trigger a broadcast receiver.

Uploaded by

Omkar Ghag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views9 pages

Alarm Manager

The Android AlarmManager allows applications to schedule tasks to run at specific times in the future. It uses a wake lock to keep the CPU powered on to ensure scheduled broadcasts are handled even if the phone is asleep. Developers can use AlarmManager to schedule intents to launch activities or broadcast receivers at certain times by setting repeating or one-time alarms. The example code shows how to build an alarm application that plays an audio file after a delay entered by the user by setting an alarm with AlarmManager and having it trigger a broadcast receiver.

Uploaded by

Omkar Ghag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Android AlarmManager

Android AlarmManager allows you to access system alarm.

By the help of Android AlarmManager in android, you can schedule your


application to run at a specific time in the future. It works whether your
phone is running or not.

The Android AlarmManager holds a CPU wake lock that provides guarantee
not to sleep the phone until broadcast is handled.

Android AlarmManager Example


AlarmManager example that runs after a specific time provided by user

activity_main.xml
You need to drag only anedittext and a button as given below.

File: activity_main.xml
1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and
roid"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. tools:context=".MainActivity" >
6.
7. <EditText
8. android:id="@+id/time"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:layout_alignParentLeft="true"
12. android:layout_alignParentTop="true"
13. android:layout_marginTop="28dp"
14. android:ems="10" //The em is simply the font size
15. android:hint="Number of seconds"
16. android:inputType="numberDecimal" />
17.
18. <Button
19. android:id="@+id/button1"
20. android:layout_width="wrap_content"
21. android:layout_height="wrap_content"
22. android:layout_alignRight="@+id/time"
23. android:layout_below="@+id/time"
24. android:layout_marginRight="60dp"
25. android:layout_marginTop="120dp"
26. android:text="Start" />
27.
</RelativeLayout>

Activity class

The activity class starts the alarm service when user clicks on the button.

File: MainActivity.java

1. package com.example.alarmexample;

2. import android.app.Activity;
3. import android.app.AlarmManager;
4. import android.app.PendingIntent;
5. import android.content.Intent;
6. import android.os.Bundle;
7. import android.view.View;
8. import android.view.View.OnClickListener;
9. import android.widget.Button;
10. import android.widget.EditText;
11. import android.widget.Toast;
12.
13. public class MainActivity extends Activity {
14. Button b1;
15.
16. @Override
17. protected void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.activity_main);
20. b1=(Button) findViewById(R.id.button1);
21.
22. b1.setOnClickListener(new OnClickListener() {
23.
24. @Override
25. public void onClick(View v) {
26. // TODO Auto-generated method stub
27. startAlert();
28. }
29. });
30.
31. } public void startAlert() {
32. EditText text = (EditText) findViewById(R.id.time);
33. int i = Integer.parseInt(text.getText().toString());
34. Intent intent = new Intent(this, MyBroadcastReceiver.class);
35. PendingIntent pendingIntent = PendingIntent.getBroadcast(
36. this.getApplicationContext(), 234324243, intent, 0);
37. AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM
_SERVICE);
38. alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTim
eMillis() + (i * 1000), pendingIntent);
39. Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH
_LONG).show();
40. }
41. }

createBroadcastReceiver class that starts alarm.

File: MyBroadcastReceiver.java
1. package com.example.alarmexample;
2. import android.content.BroadcastReceiver;
3. import android.content.Context;
4. import android.content.Intent;
5. import android.media.MediaPlayer;
6. import android.widget.Toast;
7.
8. public class MyBroadcastReceiver extends BroadcastReceiver {
9. MediaPlayer mp;
10. @Override
11. public void onReceive(Context context, Intent intent) {
12. mp=MediaPlayer.create(context, R.raw.alrm );
13. mp.start();
14. Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();

15. }
}

File: AndroidManifest.xml

You need to provide a receiver entry in AndroidManifest.xml file.

1. <receiver android:name="MyBroadcastReceiver" >


2. </receiver>
Full code of AndroidManifest.xml file.
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="com.example.alarmexample"
4. android:versionCode="1"
5. android:versionName="1.0" >
6.
7. <uses-sdk
8. android:minSdkVersion="8"
9. android:targetSdkVersion="16" />
10.
11. <uses-permission android:name="android.permission.VIBRATE" />
12.
13. <application
14. android:allowBackup="true"
15. android:icon="@drawable/ic_launcher"
16. android:label="@string/app_name"
17. android:theme="@style/AppTheme" >
18. <activity
19. android:name="com.example.alarmexample.MainActivity"
20. android:label="@string/app_name" >
21. <intent-filter>
22. <action android:name="android.intent.action.MAIN" />
23.
24. <category android:name="android.intent.category.LAUNCHE
R" />
25. </intent-filter>
26. </activity>
27.
28. <receiver android:name="MyBroadcastReceiver" >
29. </receiver>
30. </application>
31.
</manifest>
MyAlarm
Steps to add java class
Select the first option under java folder then create new class

Stepsshould be perform before to write following line

mp= MediaPlayer.create(this, R.raw.song);

Steps to add raw directory


To add song click on raw and copy song from its destination and paste it as follows

Code in MainActivity.java

package mumbai.deepa.com.myalarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivityextends AppCompatActivity {


EditTexte1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.editText);
}

public void myalarm(View V)


{
int time = Integer.parseInt(e1.getText().toString());
time = time*1000;
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent(this,AlarmServiceClass.class);
PendingIntent p = PendingIntent.getService(this,0,i,0);

am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+time,p);
Toast.makeText(this,"Alarm Set...",Toast.LENGTH_SHORT).show();
}
}

code in .xml (text)only onclick

<RelativeLayoutandroid:layout_height="match_parent" android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="33dp"
android:ems="10"
android:inputType="number" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:hint="SEt Alarm"
android:onClick="myalarm"/>
</RelativeLayout>

In design add two controls (Number and button)


Save and execute

You might also like