Wednesday, July 23, 2014

Broad Cast Receiver in Android How it Works


It is use to display power of betray how much betray have power, how much % etc power have in device current state.

Broadcast receiver should not use any time because of power consumption are be there.
Broadcast receiver designed to receive broadcast notification information, and make corresponding processing components. Such as notifying a low charge, take pictures; send text messages, calls, etc.

·         It is use to register with two ways
·         1 :- manifest.xml
·         2:-context.registerReceiver() to register
·         Broad cast receiver can receive only  onReceive(Context,Intent)


In Manifest.xml we can declare this:-
<receiver android:name=".BatteryReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BATTERY_CHANGED" />
        <action android:name="android.intent.action.BATTERY_LOW" />
    </intent-filter>
</receiver>

Broad cast receiver is mainley use for monotring power of betry and level of betry and charging state.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

It determine current state of charging now
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);



float batteryPct = level / (float)scale

It determines current state it scale level.

Code:-


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

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    // assumes WordService is a registered service
    Intent intent = new Intent(context, WordService.class);
    context.startService(intent);
  }
} 





Events

Event Description
Intent.ACTION_BOOT_COMPLETED Boot completed. Requires the android.permission.RECEIVE_BOOT_COMPLETED permission.
Intent.ACTION_POWER_CONNECTED Power got connected to the device.
Intent.ACTION_POWER_DISCONNECTED Power got disconnected to the device.
Intent.ACTION_BATTERY_LOW Triggered on low battery. Typically used to reduce activities in your app which consume power.
Intent.ACTION_BATTERY_OKAY Battery status good again.


Thanks It May help you To understand about Broad Cast Receiver.....

No comments:

Post a Comment