16 BroadCast Receiver using Manifest file
Broadcasts overview
Android apps can send or receive broadcast
messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These
broadcasts are sent when an event of interest occurs. For example, the Android
system sends broadcasts when various system events occur, such as when the
system boots up or the device starts charging. Apps can also send custom
broadcasts, for example, to notify other apps of something that they might be
interested in (for example, some new data has been downloaded).
Apps can register to receive specific
broadcasts. When a broadcast is sent, the system automatically routes
broadcasts to apps that have subscribed to receive that particular type of
broadcast.
NOTE:
However, you must be careful not to abuse the
opportunity to respond to broadcasts and run jobs in the background that can
contribute to a slow system performance.
About system broadcasts
The system automatically sends broadcasts when
various system events occur, such as when the system switches in and out of
airplane mode. System broadcasts are sent to all apps that are subscribed to
receive the event.
Following are some of the important system wide generated
intents.
1.android.intent.action.BATTERY_LOW : Indicates low battery condition on the device.
2.android.intent.action.BOOT_COMPLETED : This is broadcast once, after the system has
finished booting
3.android.intent.action.CALL : To perform a call to someone specified by the data4. 4.android.intent.action.DATE_CHANGED : The date has changed
1.android.intent.action.BATTERY_LOW : Indicates low battery condition on the device.
2.android.intent.action.BOOT_COMPLETED : This is broadcast once, after the system has
finished booting
3.android.intent.action.CALL : To perform a call to someone specified by the data4. 4.android.intent.action.DATE_CHANGED : The date has changed
5.android.intent.action.REBOOT :
Have the device
6.android.net.conn.CONNECTIVITY_CHANGE : The mobile network or wifi connection is changed(or reset)
6.android.net.conn.CONNECTIVITY_CHANGE : The mobile network or wifi connection is changed(or reset)
Receiving broadcasts
Apps can receive broadcasts in two ways:
1) manifest-declared receivers
2) context-registered receivers.
Manifest-declared receivers
If you declare a broadcast receiver in your
manifest, the system launches your app (if the app is not already running) when
the broadcast is sent.
Program:
1) manifest-declared receivers:
open AndroidManifest.xml file and add the
following code
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcastreceivers1">
<application
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".MyBroadcast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGED"/>
</intent-filter>
</receiver>
</application>
</manifest>
program:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.widget.Toast;
public class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Toast.makeText(context,
"BOOT
COMPLETED",
Toast.LENGTH_SHORT).show();
}
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
Toast.makeText(context,
"CONNECTIVITY
CHANGED",
Toast.LENGTH_SHORT).show();
}
}
}
2) context-registered
receivers.
package com.example.powerreceiver;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
SampleBroad sbrd=new SampleBroad();
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override protected void onStart()
{
super.onStart();
IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
//filter.addAction(); to
add more than one registerReceiver(sbrd,filter);
}
@Override protected void onStop()
{
super.onStop();
unregisterReceiver(sbrd);
}
}
package com.example.powerreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.widget.Toast;
public class SampleBroad extends BroadcastReceiver {
@Override public void onReceive(Context
context, Intent intent) {
if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
{
boolean c=intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);
if(c)
Toast.makeText(context,
" no
Connectivity", Toast.LENGTH_SHORT).show();
else Toast.makeText(context, " Connectivity", Toast.LENGTH_SHORT).show();
}
}
}
Comments
Post a Comment