AlarmManager in Android

AlarmManager is used to start the future actions.Example: Birthday SMS scheduler.

AlarmManager is a class which is used to access the system alarm service.These allow you to schedule your application to be run at some point in the future.When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.

When the device is in sleep mode registered alarms are retained,but will be cleared if it is turned off and rebooted.You do not instantiate this class directly, instead of that we can get it through Context.getSystemService(Context.ALARM_SERVICE).

Create Android Project with name as "AlarmManagerDemo" with package name "com.etr.alarmmanagerdemo". Create Activity with name as "AlarmManagerActivity" and it will start the Alarm. This is the code for AlarmManagerActivity.

package com.etr.alarmmanagerdemo;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AlarmManagerActivity extends Activity {
EditText interval_value;
TextView interval_value_label;
Button start, stop;
AlarmManager alarmManager;
PendingIntent pendingIntent;
long interval;

@Override
protected void onCreate(Bundle savedInstanceState) {
try {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager);

interval_value = (EditText) findViewById(R.id.interval_time);
interval_value_label = (TextView) findViewById(R.id.interval_time_label);
start = (Button) findViewById(R.id.start_alarm);
stop = (Button) findViewById(R.id.stop_alarm);

interval_value_label.setText("Secounds");
start.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(AlarmManagerActivity.this,
AlarmService.class);
pendingIntent = PendingIntent.getService(
AlarmManagerActivity.this, 0, intent, 0);
alarmManager = (AlarmManager) AlarmManagerActivity.this
.getSystemService(Context.ALARM_SERVICE);
String value = interval_value.getText().toString();
if (!value.equals("")) {
interval = Long.valueOf(value);
}
if (interval > 0) {
alarmManager.setRepeating(AlarmManager.RTC, 0,
interval * 1000, pendingIntent);
}
}
});

stop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
alarmManager.cancel(pendingIntent);
}
});

} catch (Exception e) {
Log.v("AlarmManager Exception", Log.getStackTraceString(e));
}
}

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

}

Create "AlarmService" class. it will start by AlarmManager.

package com.etr.alarmmanagerdemo;

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;

public class AlarmService extends IntentService {
Handler handler = null;

public AlarmService() {
super("AlarmService");
handler = new Handler();
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

@Override
protected void onHandleIntent(Intent intent) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Service onHandled",
Toast.LENGTH_LONG).show();
}
});
}

@Override
public void onDestroy() {
super.onDestroy();
}

}

Paste below code to "activity_alarm_manager.xml"

<LinearLayout 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:orientation="vertical">

    <LinearLayout 
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <EditText 
            android:id="@+id/interval_time"
            android:layout_width="wrap_content"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:layout_marginLeft="05dp"
            android:inputType="number"/>
        <TextView 
            android:id="@+id/interval_time_label"
            android:layout_width="wrap_content"
            android:layout_marginRight="05dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"/>
        
    </LinearLayout>
    
    <Button 
  android:layout_marginTop="30dp"  
        android:id="@+id/start_alarm"
        android:layout_gravity="center"
        android:text="@string/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
     <Button 
  android:layout_marginTop="30dp"
        android:id="@+id/stop_alarm"
        android:layout_gravity="center"
        android:text="@string/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Paste below code to "strings.xml" file

<string name="start">Start Alarm</string>

<string name="stop">Stop Alarm</string>

Paste below code to "AndroidManifest.xml" inside of application element.

<service android:name=".AlarmService"/>

you can download source code here







Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService