Android CourseObjectives • After completing this module, you will able to: • Create Broadcast Intent.. • Broadcast messages between components with the sendBroadcast method.. • Listen to
Trang 2Android Course
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
Trang 5Android Course
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
Trang 6Android Course
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
Trang 7Android Course
Listening to Broadcast Intent
@Override
// TODO Auto-generated method stub
String msg = intent.getStringExtra( "MESSAGE_KEY" );
Log.d( "onReceive" , msg);
} }
7
Trang 9Android Course
Registering Broadcast Intent in code
Trang 10Android Course
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
Trang 11Android Course
Case Study for Native Broadcast
• Call Blocker Application
• Using android.intent.action.PHONE_STATE
• SMS blocker Applcation
• Using android.provider.Telephony.SMS_RECEIVED
11
Trang 12Context context = null ;
12
Trang 13Log.d( TAG , "Receving " );
TelephonyManager telephony = (TelephonyManager) context getSystemService(Context TELEPHONY_SERVICE );
13
Trang 14telephonyService = (ITelephony) m.invoke(telephony);
for (int i = 0; i < blockedNumbers.length; i++){
e.printStackTrace();
} }
}
14
Trang 15package name must be com.android.internal.telephony FileName : ITelephony.aidl
Trang 18Android Course
SMSBlocker App – Step 1: Define SMSReceiver
SmsMessage.createFromPdu(( byte []) pdus[0]);
String phoneNumber = msg.getOriginatingAddress();
abortBroadcast();
} }
} } }
18
Trang 20Android Course
Questions or Discussions
20
Trang 21Android Course
References & Further Readings
• Intents and Intent Filters:
• http://
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
21
Trang 22Android Course
References & Further readings
• Professional Android 2 Application Development , Reto
Meier, Wiley Publishing (2010)
• Intents, Broadcast Receivers, Adapters, and the Internet , Chapter 5
• Map, Geocoding, and Location-based Services , Chapter 8
• Working in the background , Chapter 9
• Telephony and SMS , Chapter 12
• Bluetooh, Network, and Wi-fi , Chapter 13
22
Trang 23Android Course
References & Further Readings
• Android™ Wireless Application Development Second Edition , Shane
Conder, Lauren Darcey,
Addison-Wesley Professional Publishing (2010)
• Using SMS , Chapter 16
• Working with Wi-Fi , Chapter 19
• Monitoring the Battery , Chapter 19
23
Trang 24Android Course
References & Further Readings
• Android in Practice 1 Edition ,
Charlie Collins, Michael D Galpin, and Matthias Kaeppler, Manning
Trang 25Android 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 Running Services , Chapter 14
Long-• Exploring the Alarm Manager , Chapter 15
• Exploring Maps and based Services , Chapter 17
Location-• Using the Telephony APIs , Chapter 18
25
Trang 2626
Trang 27Android Course
References & Further Readings
• Learning Android , Marko
Gargenta, O'Reilly Media
Publishing (2011)
• Broadcast Receivers , Chapter 11
• System Services , Chapter 13
27