bài tập android

17
7/23/2019 Bài t p android http://slidepdf.com/reader/full/bai-tap-android 1/17 Lab 6: Broadcast Receiver S dụng Broadcast Intent để gửi thông điệ  p gia các ng dng. Broadcast Receiver đượ c thiết k ế để nhn các broadcast intent. Ví d làm sao để biết có tin nhắn đến, hoc có cuc gọi đến, đang sạc pin hay đã rút sạc,…  Cách thc gi Broadcast Intent  To mt intent Intent intent =new Intent("com.hieu.copyToApp");  Gi hàm sendBroadcast() ca Activity sendBroadcast(intent);   Có hai dạng broadcast đượ c nhn   Normal broadcast o đượ c gi bở i sendBroadcast()  Ordered broadcast o đượ c gi bở i sendOrderedBroadcast()   Có 2 cách đăng ký Broadcast Receiver: Đăng ký trong coding: Lắng nghe mọi thứ trong Intent- filter, nếu tắt ứng dụng sẽ không lắng nghe nên ít được dùng.  Đăng ký trong Manifest: Nó trở thành dịch vụ, tự động lắng nghe  mọi thứ trong Intent –  filter kể cả khi đã đóng ứng dụng. Thường được sử dụng.  Broadcast Receiver mới được kế thừa từ class  BroadcastReceiver  và override hàm onReceive()  Mẫu hiện thực Broadcast Receiver  

Upload: taivisaothe2000

Post on 19-Feb-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 1/17

Lab 6: Broadcast Receiver

Sử dụng Broadcast Intent để gửi thông điệ p giữa các ứng dụng. Broadcast Receiver đượ cthiết k ế để nhận các broadcast intent. Ví dụ làm sao để biết có tin nhắn đến, hoặc có cuộc

gọi đến, đang sạc pin hay đã rút sạc,… 

 

Cách thức gửi Broadcast Intent

  Tạo một intent

Intent intent =new Intent("com.hieu.copyToApp"); 

 

Gọi hàm sendBroadcast() của Activity

sendBroadcast(intent); 

 

Có hai dạng broadcast đượ c nhận

   Normal broadcast

o  đượ c gửi bở i sendBroadcast() 

  Ordered broadcast

o  đượ c gửi bở i sendOrderedBroadcast() 

 

Có 2 cách đăng ký Broadcast Receiver:

Đăng ký trong coding: Lắng nghe mọi thứ trong Intent- filter, nếu tắt ứng dụng sẽ khônglắng nghe nên ít được dùng. 

Đăng ký trong Manifest: Nó trở thành dịch vụ, tự động lắng nghe  mọi thứ trong Intent –  filter kể cả khi đã đóng ứng dụng. Thường được sử dụng.  

Broadcast Receiver mới được kế thừa từ class BroadcastReceiver  và override hàm

onReceive() 

 

Mẫu hiện thực Broadcast Receiver  

Page 2: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 2/17

 

Bài 1: Tạo ứng dụng nhận tin nhắn

Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

xmlns:tools="http://schemas.android.com/tools" 

android:id="@+id/LinearLayout1"  

android:layout_width="match_parent"  

android:layout_height="match_parent"  

android:orientation="vertical"  

tools:context=".MainActivity" > 

<TextView 

android:id="@+id/textView1" 

android:layout_width="match_parent"  

android:layout_height="wrap_content"  

android:text="TextView" /> 

</LinearLayout> 

Cấ p quyền trong Manifest

Page 3: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 3/17

 

<uses-permission android:name="android.permission.RECEIVE_SMS"  /> 

Code:

public class MainActivity extends Activity { 

BroadcastReceiver receiver=null; 

protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState);  

setContentView(R.layout.activity_main);  //tạo bộ lọc để lắng nghe tin nhắn gửi tới 

IntentFilter filter=new IntentFilter 

("android.provider.Telephony.SMS_RECEIVED");  

//tạo bộ lắng nghe 

receiver=new BroadcastReceiver() { 

//hàm này sẽ tự động được kích hoạt khi có tin nhắn gửi tới 

public void onReceive(Context arg0, Intent arg1) { 

//Tui tách ra hàm riêng để xử lý 

//chú ý là dữ liệu trong tin nhắn được lưu trữ trong arg1 

processReceive(arg0, arg1); 

}; 

//đăng ký bộ lắng nghe vào hệ thống 

registerReceiver(receiver, filter); 

protected void onDestroy() { 

super.onDestroy(); 

Page 4: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 4/17

 //hủy bỏ đăng ký khi tắt ứng dụng 

unregisterReceiver(receiver); 

public void processReceive(Context context, Intent intent) 

Toast.makeText(context, "Hello .. có tin nhắn tới đó",

Toast.LENGTH_LONG).show(); 

TextView lbl=(TextView) findViewById(R.id.textView1); 

//pdus để lấy gói tin nhắn 

String sms_extra="pdus"; 

Bundle bundle=intent.getExtras(); 

//bundle trả về tập các tin nhắn gửi về cùng lúc 

Object []objArr= (Object[]) bundle.get(sms_extra); 

String sms=""; 

//duyệt vòng lặp để đọc từng tin nhắn 

for(int i=0;i<objArr.length;i++) 

//lệnh chuyển đổi về tin nhắn createFromPdu 

SmsMessage smsMsg=SmsMessage. 

createFromPdu((byte[]) objArr[i]); 

//lấy nội dung tin nhắn 

String body=smsMsg.getMessageBody(); 

//lấy số điện thoại tin nhắn 

String address=smsMsg.getDisplayOriginatingAddress(); 

sms+=address+":\n"+body+"\n"; 

//hiển thị lên giao diện 

lbl.setText(sms); 

Page 5: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 5/17

 } 

Bài 2: Tạo ứng dụng nhận biết trạng thái sạc pin/ không sạc pin.

Đăng ký nhận thông tin từ hệ thống chúng ta phải tạo một đối tượng IntentFilter để lọcnhững thông tin cần thiết, ở đây chúng ta sẽ lọc hai sự kiện cắm sạc và rút sạc. 

IntentFilter filter = new IntentFilter(); // Đăng ký lọc sự kiện cắm sạc và rút sạc cho intentfilter filter.addAction("android.intent.action.ACTION_POWER_CONNECTED");  filter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");  Sau khi đăng ký lọc sự kiện ta phải đăng ký với hệ th ống xử lý các sự kiện đó qua BroadcastReceiver 

// Khởi tạo BroadcastReceiver BroadcastReceiver receiver = new BroadcastReceiver() { 

// Phương thức này sẽ được hệ thống gọi khi nhận được sự kiện đang sạcpin 

@Override public void onReceive(Context context, Intent intent) { 

// nội dung công việc cần làm khi sự kiện đã đăng ký xảy ra // nếu sự kiện nhận được là kết nối sạc 

if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){  textView.setText(" Đang sạc pin"); 

} // nếu sự kiện nhận đưọc là rút sạc  if(intent.getAction().endsWith(Intent.ACTION_POWER_DISCONNECT

ED)){ textView.setText(" Đã rút sạc"); 

} } 

// Đăng ký nhận và xử lý các sự kiện  registerReceiver(receiver, filter); // được viết trong Activity 

Hủy đăng ký 

public void onDestroy(){ // hủy đăng ký if (receiver != null) { 

unregisterReceiver(receiver); } super.onDestroy(); 

Page 6: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 6/17

Bài 3: Tạo app trả lời tin nhắn ứng dụng trong trường hợp khẩn

cấp

1. 

Thêm các permission vào file Manifiest.xml để có quyền nhận và trả lờ itin nhắn đồng thờ i lấy vị trí hiện tại.

2.  Thêm các thư viện cho MainActivity.java 

3. 

Thêm các thẻ String trong file String.xml 

<uses-permission android:name="android.permission.RECEIVE_SMS"/> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

import java.io.IOException;

import java.util.ArrayList;import java.util.List;import java.util.Locale;import java.util.concurrent.locks.ReentrantLock;import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.location.Address;import android.location.Geocoder;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.telephony.SmsManager;import android.telephony.SmsMessage;import android.util.Log;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.ListView; 

Page 7: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 7/17

 4.  Thêm các control cho giao diện trong file main_layout.xml

Và giao diện sẽ như sau:

<string name="allClearButtonText">I am Safe and Well</string> <string name="maydayButtonText">MAYDAY! MAYDAY! MAYDAY!</string> <string name="setupautoresponderButtonText">Setup Auto Responder</string> <string name="allClearText">I am safe and well. Worry not!</string> <string name="maydayText">Tell my mother I love her.</string> <string name="querystring">are you OK?</string> 

<string name="querylistprompt">These people want to know if you\’reok</string> <string name="includelocationprompt">Include Location in Reply</string> 

<TextView android:id="@+id/labelRequestList" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/querylistprompt" android:layout_alignParentTop="true" /> 

<LinearLayout android:id="@+id/buttonLayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_alignParentBottom="true"> <CheckBox android:id="@+id/checkboxSendLocation" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/includelocationprompt"/> <Button android:id="@+id/okButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/allClearButtonText"/> <Button android:id="@+id/notOkButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/maydayButtonText"/> <Button android:id="@+id/autoResponder" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/setupautoresponderButtonText"/> 

</LinearLayout> <ListView android:id="@+id/myListView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/labelRequestList" android:layout_above="@id/buttonLayout"/> 

Page 8: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 8/17

 

5. 

Tạo mảng Array List chứ a kiểu dữ  liệu String để hiển thị lên màn hình

khi có tin nhắn đến và các sự  kiện Click của các button

Page 9: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 9/17

 

Sau đó ta lần lượ t thêm code vào các hàm này tùy vào mục đích của ứ ng

dụng

ReentrantLock lock;CheckBox locationCheckBox;ArrayList<String> requesters;ArrayAdapter<String> aa;

@Override 

 protected  void  onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

lock = new ReentrantLock();requesters = new ArrayList<String>();wireUpControls();

}

 private void  wireUpControls() {locationCheckBox = (CheckBox)

findViewById(R.id.checkboxSendLocation);ListView myListView = (ListView)

findViewById(R.id.myListView );int layoutID = android.R.layout.simple_list_item_1;aa = new ArrayAdapter<String>(this, layoutID, requesters);myListView.setAdapter(aa);Button okButton = (Button) findViewById(R.id.okButton);okButton.setOnClickListener(new View.OnClickListener() {

 public void  onClick(View view) {respond(true, locationCheckBox.isChecked());

}});Button notOkButton = (Button) findViewById(R.id.notOkButton);notOkButton.setOnClickListener(new View.OnClickListener() {

 public void  onClick(View view) {

respond(false, locationCheckBox.isChecked());}});Button autoResponderButton = (Button)

findViewById(R.id.autoResponder );autoResponderButton.setOnClickListener(new 

View.OnClickListener() { public void  onClick(View view) {

startAutoResponder();}

});}

 public void  respond( boolean ok,  boolean includeLocation) {

}

 private void  startAutoResponder() {} 

Page 10: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 10/17

6.  Tạo Broadcast Receiver để lắng nghe sự  kiện nhận tin nhắn và hàm lấynội dung tin nhắn cũng như địa chỉ ngườ i gử i

7.  Override 2 phương thức onResume và onPause để register Broadcast vàunregister nó

 public static final String SMS_RECEIVED  =

"android.provider.Telephony.SMS_RECEIVED";

BroadcastReceiver emergencyResponseRequestReceiver = new BroadcastReceiver() {@Override  public void  onReceive(Context context, Intent intent) {

if (intent.getAction().equals(SMS_RECEIVED )) {String queryString = getString(R.string.querystring )

.toLowerCase();Bundle bundle = intent.getExtras();if (bundle != null) {Object[] pdus = (Object[]) bundle.get("pdus");SmsMessage[] messages = new SmsMessage[pdus.length];for (int i = 0; i < pdus.length; i++)

messages[i] = SmsMessage.createFromPdu(( byte[])pdus[i]);

for (SmsMessage message : messages) {if (message.getMessageBody().toLowerCase()

.contains(queryString))

requestReceived(message.getOriginatingAddress());}

}}

}};

 public void  requestReceived(String from) {}

@Override  protected  void  onResume() {

IntentFilter filter = new IntentFilter(SMS_RECEIVED );

registerReceiver(emergencyResponseRequestReceiver, filter);

super.onResume(); 

}

@Override  protected  void  onPause() {

unregisterReceiver(emergencyResponseRequestReceiver);super.onPause();

Page 11: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 11/17

8.  Update phương thứ c requestReceived để thêm dữ  liệu nhận đượ c vàoListView

9.  Đế đây thì chạy ứ ng dụng lên và gử i tin nhắn đến thì sẽ đượ c k ết quả 

như sau: 

 public void  requestReceived(String from) {if (!requesters.contains(from)) {

lock.lock();

requesters.add(from);aa.notifyDataSetChanged();lock.unlock();

}} 

Page 12: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 12/17

10. Tiếp theo ta update phương thức respond để có thể trả lờ i tin nhắn: public void  respond( boolean ok,  boolean includeLocation) {

String okString = getString(R.string.allClearText);String notOkString = getString(R.string.maydayText);String outString = ok ? okString : notOkString;ArrayList<String> requestersCopy = (ArrayList<String>)

requesters.clone();for (String to : requestersCopy)

respond(to, outString, includeLocation);}

Page 13: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 13/17 

 private void  respond(String to, String response,  boolean includeLocation) {

lock.lock();requesters.remove(to);aa.notifyDataSetChanged();lock.unlock();

SmsManager sms = SmsManager.getDefault();// Send the message sms.sendTextMessage(to, null, response, null, null);StringBuilder sb = new StringBuilder();if (includeLocation) {

String ls = Context.LOCATION_SERVICE ;LocationManager lm = (LocationManager)

getSystemService(ls);Location l =

lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);if (l == null)

sb.append("Location unknown.");else {

sb.append("I’m @:\n");

sb.append(l.toString() + "\n");List<Address> addresses;Geocoder g = new 

Geocoder(getApplicationContext(),Locale.getDefault());

try {addresses =

g.getFromLocation(l.getLatitude(),l.getLongitude(), 1);

if (addresses != null) {Address currentAddress =

addresses.get(0);if 

(currentAddress.getMaxAddressLineIndex() > 0) {for (int i = 0; i <currentAddress

.getMaxAddressLineIndex(); i++) {

sb.append(currentAddress.getAddressLine(i));sb.append("\n");

}} else {

if (currentAddress.getPostalCode() != null)

sb.append(currentAddress.getPostalCode());

}}

} catch (IOException e) {Log.e("SMS_RESPONDER", "IO Exception.",

e);}ArrayList<String> locationMsgs =

sms.divideMessage(sb.toString());

for (String locationMsg : locationMsgs)sms.sendTextMessage(to, null,

locationMsg, null, null);}

Page 14: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 14/17

11. Tiếp theo ta tạo Broadcast để gử i tin nhắn (cũng làm giống như lúc tạoBroadcast nhận tin nhắn)

và thêm trong 2 phương thứ c onResum và onPause

 public static final String SENT_SMS  =

"com.paad.emergencyresponder.SMS_SENT";

 private BroadcastReceiver attemptedDeliveryReceiver = new BroadcastReceiver() {@Override  public void  onReceive(Context _context, Intent _intent) {

if (_intent.getAction().equals(SENT_SMS )) {if (getResultCode() != Activity.RESULT_OK ) {

String recipient =_intent.getStringExtra("recipient");

requestReceived(recipient);}

}}

}; 

@Override  protected  void  onResume() {

registerReceiver(emergencyResponseRequestReceiver, filter);IntentFilter attemptedDeliveryfilter = new 

IntentFilter(SENT_SMS );registerReceiver(attemptedDeliveryReceiver,attemptedDeliveryfilter);super.onResume();

}

@Override 

 protected  void  onPause() {unregisterReceiver(attemptedDeliveryReceiver);super.onPause();

Page 15: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 15/17

12. Đến đây là đã xong, chạy ứ ng dụng và kiểm tra k ết quả 

Page 16: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 16/17

 

Page 17: Bài tập android

7/23/2019 Bài tập android

http://slidepdf.com/reader/full/bai-tap-android 17/17

Bài tập:

Bài 1: Tạo app bắt cuộc gọi đến với BroadcastReceiverHướng d ẫn

 

Cấ p quyền ở  file AndroidManifest.xml

<receiver android:name=".IncomingCall">

<intent-filter>

<action android:name="android.intent.action.PHONE_STATE" />  

</intent-filter>

</receiver>

<uses-permission android:name="android.permission.READ_PHONE_STATE">  

</uses-permission>

Vi ế t ti ếp … 

Bài 2: Tạo ứng dụng kiểm tra mức pin dưới 10% thì phát cảnh

báo cho người dùng.