ảnh minh hoa -Đào tạo Android
Broadcast Receiver phản hồi các thông tin phát ra từ các ứng dụng khác hoặc từ chính hệ thống. Những thông báo này thỉnh thoảng được gọi là các event hoặc intent. tỉ dụ, các áp dụng cũng có thể khởi tạo các tín hiệu broadcast để thông tin cho áp dụng khác biết rằng một số dữ liệu đã được về tới thiết vị và là có sẵn cho chúng để sử dụng, nên chi Broadcast Receiver phiên dịch thông báo đó và khởi tạo hành động hạp.
Sau đây là hai bước quan trọng để làm Broadcast Receiver làm việc cho các Intent: −
Tạo Broadcast Receiver.
Đăng ký Broadcast Receiver.
Có thể có một bước bổ sung nếu bạn đang triển khai các Custom Intent của bạn, thì khi đó bạn sẽ phải tạo và phát các Intent đó.
Tạo Broadcast Receiver trong Android
Một Broadcast Receiver được triển khai như là một lớp con của lớp BroadcastReceivervà ghi đè phương thức onReceive(), nơi mà mỗi thông báo được nhận như là một thông số của đối tượng Intent.
public class MyReceiver extends BroadcastReceiver @Override public void onReceive(Context context, Intent intent) Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Đăng ký Broadcast Receiver trong Android
Một áp dụng nghe các Intent được phát ra cụ thể bằng cách đăng ký một Broadcast Receiver trong AndroidManifest.xml file. giả tỉ chúng ta đang đăng ký MyReceiver cho system event (sự kiện được tạo từ hệ thống) đã được tạo là ACTION_BOOT_COMPLETED, mà được kích hoạt bởi hệ thống một khi hệ điều hành Android đã hoàn thành tiến trình boot.
BROADCAST-RECEIVER
android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name="MyReceiver"> android:name="android.intent.action.BOOT_COMPLETED">
hiện thời, bất cứ khi nào thiết bị Android của bạn được boot, thì thông tin sẽ được nhận bởi MyReceiver và trình khai triển logic bên trong phương thức onReceive() sẽ được thực thi.
Có một số system event được định tức thị là các trường final static trong lớp Intent. Bảng dưới liệt kê một số system event quan trọng:
Eventtrình bày
android.intent.action.BATTERY_CHANGEDthông báo này chứa dạng nạp, chừng độ, và thông tin khác về pin
android.intent.action.BATTERY_LOWChỉ dạng low battery trên thiết bị
android.intent.action.BATTERY_OKAYChỉ rằng pin giờ là tốt sau khi low battery
android.intent.action.BOOT_COMPLETEDĐây là tín hiệu broadcast thông tin sau khi hệ thống đã kết thúc boot
android.intent.action.BUG_REPORTChỉ activity để ít một bug
android.intent.action.CALLthông tin một lời gọi tới ai đó được xác định bởi dữ liệu
android.intent.action.CALL_BUTTONNgười dùng nhấn nút call để tới Dialer (trình gọi điện) hoặc giao diện UI hợp khác để tạo một cuộc gọi
android.intent.action.DATE_CHANGEDDate đã được thay đổi
android.intent.action.REBOOTReboot thiết bị
Tín hiệu Custom Intent
Nếu bạn muốn chính ứng dụng của bạn nên tạo và gửi các Custom Intent, thì khi đó bạn sẽ phải tạo và gửi các Intent đó bằng việc sử dụng phương thức sendBroadcast() bên trong lớp Activity của bạn. Nếu bạn sử dụng phương thức sendStickyBroadcast(Intent) thì Intent là sticky, nghĩa là Intent bạn đang gửi vẫn ở đâu đó sau khi tín hiệu chấm dứt.
public void broadcastIntent(View view) Intent intent = new Intent(); intent.setAction("com.tutorialspoint.CUSTOM_INTENT"); sendBroadcast(intent);
Ở đây com.tutorialspoint.CUSTOM_INTENT cũng có thể được đăng ký theo cách tương tự như chúng ta đã đăng ký intent được tạo ra từ hệ thống (system intent).
android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name="MyReceiver"> android:name="com.tutorialspoint.CUSTOM_INTENT">
tỉ dụ
Ví dụ này sẽ giải thích cách tạo BroadcastReceiver để chặn Custom Intent. Khi bạn đã quen với Custom Intent, bạn có thể lập trình áp dụng để chặn các system intent. Bạn theo các bước sau để sửa đổi ứng dụng Android đã tạo trong chương Ví dụ Hello World. −
Bướctrình diễn.#
1Bạn sẽ dùng Android Studio để tạo một ứng dụng Android và đặt tên nó làMy Application dưới một package là com.example.My Application như đã giảng giải trong chương Ví dụ Hello World
2Sửa đổi phương thức MainActivity.java để thêm phương thức broadcastIntent()
3Tạo một MyReceiver.java dưới một package com.example.My Application để định nghĩa một BroadcastReceiver
4Một áp dụng có thể xử lý một hoặc nhiều Custom Intent và System Intent mà không có bất cứ hạn chế nào. Mỗi Intent bạn muốn chặn phải được đăng ký trong AndroidManifest.xml file dùng thẻ
5Sửa đổi nội dung mặc định của res/layout/activity_main.xml file để bao một button để thông báo Intent
6Không cần sửa đổi string.xml file, Android Studio sẽ để ý điều đó
7Chạy ứng dụng để chạy Android Emulator và soát kết quả các thay đổi đã thực hiện trong áp dụng
Sau đây là nội dung của activity file đã được sửa đổi src/com.example.My Application/MainActivity.java. File này có thể bao các phương thức nền móng. Chúng ta đã thêm phương thức broadcastIntent() để phát một Custom Intent.
package com.example.My Application;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.content.Intent;import android.view.View;public class MainActivity extends Activity @Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); @Override public boolean onCreateOptionsMenu(Menu menu) getMenuInflater().inflate(R.menu.activity_main, menu); return true; // broadcast a custom intent. public void broadcastIntent(View view) Intent intent = new Intent(); intent.setAction("com.tutorialspoint.CUSTOM_INTENT"); sendBroadcast(intent);
Còn đây là nội dung của src/com.example.My Application/MyReceiver.java:
package com.example.My Application;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver extends BroadcastReceiver @Override public void onReceive(Context context, Intent intent) Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Sau đây là nội dung đã sửa đổi của AndroidManifest.xml file. Ở đây, chúng ta đã thêm thẻ để bảo vệ service:
xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.My Application" android:versionCode="1" android:versionName="1.0" > android:minSdkVersion="8" android:targetSdkVersion="22" /> android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name=".MainActivity" android:label="@string/title_activity_main" > android:name="android.intent.action.MAIN" /> android:name="android.intent.category.LAUNCHER"/> android:name="MyReceiver"> android:name="com.tutorialspoint.CUSTOM_INTENT">
Nội dung của res/layout/activity_main.xml để bao một button để phát Custom Intent. −
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Example of Broadcast" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point " android:textColor="#ff87ff09" android:textSize="30dp" android:layout_above="@+id/imageButton" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/abc" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="Broadcast Intent" android:onClick="broadcastIntent" android:layout_below="@+id/imageButton" android:layout_centerHorizontal="true" />
Và nội dung của res/values/strings.xml để định nghĩa hai hằng mới: −
name="menu_settings">Settings name="title_activity_main">My Application
Chạy áp dụng Hello World! vừa sửa đổi ở trên. ví thử bạn đã tạo AVD trong khi cài đặt. Để chạy vận dụng từ Android Studio, mở activity file và nhấn tượng trưng Run từ thanh phương tiện. −
hiện nay, để phát Custom Intent, bạn nhấn vào nút Broadcast Intent nó sẽ phát"com.tutorialspoint.CUSTOM_INTENT" mà sẽ bị chặn bởi BroadcastReceiver đã đăng ký là MyReceiver và màn hình mô phỏng sẽ như sau: −
Bạn có thể triển khai BroadcastReceiver khác để chặn các System intent như Bootup, date changed, low battery, …

0 nhận xét:
Đăng nhận xét