Sunday, October 13, 2013

Alarm via BoardCast Reciever Android

Hi Guys,

This tutorial might be helpful for leaning about the Alarm with Broadcast Receiver in android.
A Broadcast Receiver is an Android component which allows you to register for system or application events. All registered receivers for an event will be notified by the Android run-time once this event happens. 

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

If registering a receiver in your Activity.onResume() implementation, you should unregistered it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregistered in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.  More detail about broadcast receiever Here.

Alarm Manager class provides access to the system alarm services. 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. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes.

There are two way we can registered and unregistered the broadcast receive.
1. Dynamic way
2. Static Way 

Dynamic way:-

You can register a receiver dynamically via the Context.registerReceiver() method. You can also dynamically unregister receiver by using Context.unregisterReceiver() method.

Static Way:-

You can use the PackageManager class to enable or disable receivers registered in your AndroidManifest.xml file.
For Register
ComponentName receiver = new ComponentName(this, AlarmManagerBR.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
For UnRegister
ComponentName receiver = new ComponentName(this, AlarmManagerBR.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

main_activity.xml




MainActivity.java

package com.sunil.br;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

private Button btnstartalarm=null;
private Button btncancelalarm=null;
private Button btnenablebr=null;
private Button btndiablebr=null;
AlarmManager amanager=null;
PendingIntent pi=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnstartalarm = (Button)findViewById(R.id.button_startalarm);
btncancelalarm = (Button)findViewById(R.id.button_cancelalarm);
btnenablebr = (Button)findViewById(R.id.button_enablebr);
btndiablebr = (Button)findViewById(R.id.button_disablebr);

btnstartalarm.setOnClickListener(this);
btndiablebr.setOnClickListener(this);
btncancelalarm.setOnClickListener(this);
btnenablebr.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {

if(arg0==btnstartalarm)
{
amanager=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent= new Intent(this, AlarmManagerBR.class);
pi=PendingIntent.getBroadcast(this, 0, intent, 0);
//After after 2 seconds
amanager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*4, pi);
Toast.makeText(this, "Start Repating Alarm", Toast.LENGTH_SHORT).show();

}
else if (arg0==btncancelalarm) {

amanager.cancel(pi);
Toast.makeText(this, "Canceled Alarm", Toast.LENGTH_SHORT).show();
}

else if(arg0==btnenablebr){

ComponentName receiver = new ComponentName(this, AlarmManagerBR.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Enable Boradcast Reciever", Toast.LENGTH_SHORT).show();
}
else if (arg0==btndiablebr) {

ComponentName receiver = new ComponentName(this, AlarmManagerBR.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Diable Boradcast Reciever", Toast.LENGTH_SHORT).show();
}
}
}

AlarmManagerBR.java

package com.sunil.br;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmManagerBR extends BroadcastReceiver{

@SuppressLint("SimpleDateFormat")
@Override
public void onReceive(Context arg0, Intent arg1) {

StringBuilder sb=new StringBuilder();
SimpleDateFormat format=new SimpleDateFormat("hh:mm:ss a");
sb.append(format.format(new Date()));
Toast.makeText(arg0, sb, Toast.LENGTH_SHORT).show();
}

}

Manifest.xml


















You can download the source code BoradCastRecieverExample
Cheers Guys!!

Rocky

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Tipss.