Академический Документы
Профессиональный Документы
Культура Документы
1. Intents
2. Alarm Managers & Job Schedulers
3. Content Providers
4. Notification Manager (Local Notifications & Push
Notifications)
5. Activity vs Fragments
6. Service
7. Broadcast Receiver
8. Contexts
Intro to Broadcasts
1. Applications can register for various system events (intents) like boot complete or battery low.
2. Android system sends broadcast when specific system events occur similar to publish-
subscribe pattern. System broadcasts are sent to all apps that are subscribed to receive the
event.
3. 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).
1. Normal Broadcasts
1. Context Registered Receivers (Global)
2. Manifest Registered Receivers (Global)
3. Local Broadcast
In non-ordered mode, broadcasts are sent to all interested receivers “at the same
time”. This basically means that one receiver can not interfere in any way with what
other receivers will do neither can it prevent other receivers from being executed.
One example of such broadcast is the ACTION_BATTERY_LOW one.
2. Ordered Broadcasts
Imagine this:
• You have dozen apps that listen for connectivity change events. You
leave home to go grocery shopping.
• The connection for the home wifi drops once you exit your apartment.
Android system sends the CONNECTIVITY_ACTION broadcast,
and causes all of those dozen apps to wake up and react to that
change.
Since all those apps register for these broadcasts in their AndroidManifest,
they’re always woken up to receive these events. Even when they’re not in the
foreground or even running at all.
Registering Broadcasts
3. Now create a separate class (Your broadcast Listener name which extends
from BroadcastReceiver)
Broadcast Listeners?!
Steps to register for Broadcast listener for system events(Implicit)
public class ScreenReceiver extends BroadcastReceiver {
@Override String TAG = "ScreenReceiver";
protected void onCreate(Bundle savedInstanceState) { @Override
super.onCreate(savedInstanceState); public void onReceive(Context context, Intent intent) {
setContentView(R.layout.main_activity);
if(intent.getAction() !=null) {
ScreenReceiver screenReceiver = new ScreenReceiver(); if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
IntentFilter intentFilter = new IntentFilter(); Log.i(TAG, "Screen is off now");
intentFilter.addAction(Intent.ACTION_SCREEN_ON); }else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
intentFilter.addAction(Intent.ACTION_SCREEN_OFF); Log.i(TAG, "Screen is on now");
registerReceiver(screenReceiver, intentFilter); }
} }
}
}
MainActivity
YourReceiver
@Override
public void onDestroy(){ It has maximum limit of 10secs, do not do any
super.onDestroy(); asynchronous operations which may take
unregisterReceiver(screenReceiver); more time, do not do heavy database operations or
} networking operations in broadcast.
@Override
protected void onCreate(Bundle savedInstanceState) { public class CustomListener extends BroadcastReceiver {
super.onCreate(savedInstanceState); String TAG = " CustomListener ";
setContentView(R.layout.main_activity); @Override
public void onReceive(Context context, Intent intent) {
CustomListener customListener = new CustomListener(); Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
IntentFilter intentFilter1 = new IntentFilter(); }
intentFilter1.addAction("smd.custom.listener"); }
registerReceiver(customListener, intentFilter1);
}
YourReceiver
//call on onclick (for testing)
public void sendBroadcast(View view) {
Log.i("sending..", "button clicked");
Intent localIntent = new Intent();
localIntent.addCategory(Intent.CATEGORY_DEFAULT); <receiver android:name=".ui.CustomListener">
localIntent.setAction("smd.custom.listener");
sendBroadcast(localIntent); <intent-filter>
} <action android:name="smd.custom.listener" />
<category
android:name="android.intent.category.DEFAULT" />
MainActivity </intent-filter>
</receiver>
If you use android: exported = "false“ attribute
in manifest, then no apps can communicate or
invoke your receiver Declare in Manifest
What is LocalBroadcastManager?
1. Broadcast data won’t leave your app, so don’t need to worry about leaking private
data.
2. It is not possible for other applications to send these broadcasts to your app, so you
don’t need to worry about having security holes they can exploit.
3. It is more efficient than sending a global broadcast through the system.
4. No overhead of system-wide broadcast.
How to work with local broadcast?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
};
3 Receivers with same action which broadcast receiver will be called first??
Depending upon their “priority”, high priority means execution first. Priority is
set in their manifest tag.
Sending Ordered Broadcast
@Override
protected void onCreate(Bundle savedInstanceState) { public class CustomListener extends BroadcastReceiver {
super.onCreate(savedInstanceState); String TAG = " CustomListener ";
setContentView(R.layout.main_activity); } @Override
public void onReceive(Context context, Intent intent) {
//call on onclick (for testing) Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
public void sendBroadcast(View view) { }
}
Intent localIntent = new Intent();
localIntent.addCategory(Intent.CATEGORY_DEFAULT); public class CustomListener2 extends BroadcastReceiver {
localIntent.setAction("smd.custom.listener"); String TAG = " CustomListener2 ";
sendOrderedBroadcast(localIntent,null); @Override
} public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Clicked!!", Toast.LENGTH_SHORT).show();
} YourReceiver
}
<receiver android:name=".ui.CustomListener">
<intent-filter>
<action android:name="smd.custom.listener" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
MainActivity <receiver android:name=".ui.CustomListener2">
<intent-filter>
<action android:name="smd.custom.listener" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Home Exercise: