When I am working with broadcast receivers, I get a little confused about methods like sendBroadcast and registerReciever. Both gives the same result, and working functionality are also the same. But, what is the reason behind to work with both?
For example, If I try to get the result of my battery level, I am using the coding like
public void onButtonClick(View view)
{
IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
BraodcastReceiver br=new BroadcastReceiver();
registerReceiver(br,intentFilter);
}
or
public void onButtonClick(View view)
{
Intent intent=new Intent(Intent.ACTION_BATTERY_CHANGED);
sendBroadcast(intent);
}
What are the differences between this two methods? How they will work? Can you please give me the reasons?
My BroadcastReceiver class:
public class MyBroadcastClass extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
Log.d("BatteryLevel",level);
}
}