Android Course © 2011 University of Science – HCM City . M.Sc. Bui Tan Loc btloc@fit.hcmus.edu.vn Department of Software Engineering, Faculty of Information Technology, University of Science – Ho Chi Minh City, Viet Nam BroadcastReceiver Android Course © 2011 University of Science – HCM City . Objectives • After completing this module, you will able to: • Create Broadcast Intent. • Broadcast messages between components with the sendBroadcast method. • Listen to user Broadcast Intent. • Listen to Native Broadcast. 2 Android Course © 2011 University of Science – HCM City . Contents • Broadcast Intents • Broadcast Receivers • Native Broadcast • Case Study for Native Broadcast 3 Android Course © 2011 University of Science – HCM City . Broadcast Intents • Broadcast messages between components with the sendBroadcast method. • Make an application more open, by broadcasting to current and other applications. 4 Android Course © 2011 University of Science – HCM City . Using Broadcast Intent to send broadcast Intent i = new Intent(); i.setAction("com.btloc.android.demo.simplebroadcast"); i.putExtra("MESSAGE_KEY", "Simple message"); sendBroadcast(i); 5 Android Course © 2011 University of Science – HCM City . Broadcast Receivers • Listen to Broadcast Intents. • Must be registered (either in code or within the application manifest). • Use Intent Filter to specify which Intents it is listening for. 6 Android Course © 2011 University of Science – HCM City . Listening to Broadcast Intent public class SimpleBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String msg = intent.getStringExtra("MESSAGE_KEY"); Log.d("onReceive", msg); } } 7 Android Course © 2011 University of Science – HCM City . Registering Broadcast Intent within the app manifest <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".SimpleBroadcastDemoActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="SimpleBroadcastReceiver"> <intent-filter> <action android:name="com.btloc.android.demo.simplebroadcast"/> </intent-filter> </receiver> </application> 8 Intent i = new Intent(); i.setAction("com.btloc.android.demo.simplebroadcast"); i.putExtra("MESSAGE_KEY", "Simple message"); sendBroadcast(i); Android Course © 2011 University of Science – HCM City . Registering Broadcast Intent in code private BroadcastReceiver receiver = new SimpleBroadcastReceiver(); @Override protected void onResume() { super.onResume(); IntentFilter filter = new IntentFilter(); filter.addAction("com.btloc.android.demo.simplebroadcast"); registerReceiver(receiver, filter); } @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver); } 9 Android Course © 2011 University of Science – HCM City . Native Broadcast • ACTION_CAMERA_BUTTON – The camera button has been pushed. • ACTION_TIMEZONE_CHANGED - The setting for the time zone has changed. • ACTION_BOOT_COMPLETED – The boot progress has been completed • ACTION_BATTERY_LOW - A warning that the battery is low. • ACTION_HEADSET_PLUG - A headset has been plugged into the device, or unplugged from it. • ACTION_SCREEN_ON - The screen has been turned on. • 10 [...]... android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.telephony.TelephonyManager; android.util.Log; public class PhoneCallReceiver extends BroadcastReceiver { Context context = null; private static final String TAG = "Phone call"; private ITelephony telephonyService; © 2011 University of Science – HCM City Android Course CallBlocker App – Step 1: Define PhoneCallReceiver @Override... developer.android.com/guide/topics/intents/intents-filters.html • Android App Course: • Intents, Intent Filters, Broadcasts, Broadcast Receivers: • http://sites.google.com/site/androidcoursearchive/lectures © 2011 University of Science – HCM City Android Course References & Further readings • Professional Android 2 Application Development, Reto Meier, Wiley Publishing (2010) • Intents, Broadcast Receivers, • • • • Adapters, and the Internet, Chapter 5... For answerRingingCall() method • For endCall() method • For read-phone-state methods © 2011 University of Science – HCM City Android Course SMSBlocker App – Step 1: Define SMSReceiver public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent){ String[] blockedNumbers = { "123", "456" }; Bundle bundle = intent.getExtras(); if (bundle != null)... Native Broadcast • Call Blocker Application • Using android.intent.action.PHONE_STATE • SMS blocker Applcation • Using android.provider.Telephony.SMS_RECEIVED © 2011 University of Science – HCM City Android Course CallBlocker App – Step 1: Define PhoneCallReceiver import java.lang.reflect.Method; import com.android.internal.telephony.ITelephony; import import import import import android.content.BroadcastReceiver;... android:name="android.intent.action.MAIN" /> < /receiver> © 2011 University of Science – HCM City Android Course Questions or Discussions © 2011 University... android:name="android.intent.action.MAIN" /> < /receiver> © 2011 University of Science – HCM City Android Course Giving the permission • ... HCM City Android Course References & Further Readings • Pro Android 3, Mark Murphy, Apress Publishing (2011) • Building and Consuming Services, • • • • • • Chapter 11 Exploring Handlers, Chapter 13 Broadcast Receivers and LongRunning Services, Chapter 14 Exploring the Alarm Manager, Chapter 15 Exploring Maps and Locationbased Services, Chapter 17 Using the Telephony APIs, Chapter 18 Using Sensors, Chapter... (2011) • Invoking a Service, Chapter 31 © 2011 University of Science – HCM City Android Course References & Further Readings • Learning Android, Marko Gargenta, O'Reilly Media Publishing (2011) • Broadcast Receivers, Chapter 11 • System Services, Chapter 13 © 2011 University of Science – HCM City ... "); TelephonyManager telephony = (TelephonyManager) context getSystemService(Context.TELEPHONY_SERVICE); © 2011 University of Science – HCM City Android Course CallBlocker App – Step 1: Define PhoneCallReceiver try { Class c = Class.forName(telephony.getClass().getName()); Method m = c.getDeclaredMethod("getITelephony"); m.setAccessible(true); telephonyService = (ITelephony) m.invoke(telephony); for... SmsMessage.createFromPdu((byte[]) pdus[0]); String phoneNumber = msg.getOriginatingAddress(); for (int i = 0; i < blockedNumbers.length; i++){ if (phoneNumber.compareTo(blockedNumbers[i]) == 0) { abortBroadcast(); } } } } } © 2011 University of Science – HCM City Android Course SMSBlocker App – Step 2: Configure AndroidManifest.xml . University of Science – HCM City . Listening to Broadcast Intent public class SimpleBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent). University of Science – HCM City . Registering Broadcast Intent in code private BroadcastReceiver receiver = new SimpleBroadcastReceiver(); @Override protected void onResume() { super.onResume(); . android.content .BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.TelephonyManager; import android.util.Log; public class PhoneCallReceiver extends BroadcastReceiver