Ví dụ về Broadcast Receivers

0
370

Ví dụ này sẽ giải thích cách tạo Broadcast Receiver để đánh chặn mục đích tùy chỉnh. Một khi bạn đã quen thuộc với ý định tùy chỉnh, sau đó bạn có thể lập trình ứng dụng của bạn để đánh chặn các hệ thống tạo ra ý định. Vì vậy, hãy làm theo các bước sau để sửa đổi ứng dụng Android mà chúng tôi đã tạo trong ví dụ Hello World Example –

Bước Mô tả
1 Bạn sẽ sử dụng Android studio để tạo một ứng dụng Android và đặt tên nó là Ứng dụng của tôi dưới một gói com.example.oktot.myapplication như được giải thích trong chương Hello World Example.
2 Sửa đổi tệp main activity MainActivity.java để thêm phương thức broadcastIntent ().
3 Tạo tệp tin java mới gọi là MyReceiver.java trong gói com.example.oktot.myapplication để khai báo BroadcastReceiver.
4 Một ứng dụng có thể xử lý một hoặc nhiều ý định tùy chỉnh và nội dung hệ thống mà không bị hạn chế. Mọi mục đích bạn muốn đánh chặn phải được đăng ký trong tệp AndroidManifest.xml của bạn bằng thẻ <receiver … />
5 TIẾNG VIỆT

Sửa đổi nội dung mặc định của tệp res / layout / activity_main.xml để hiện nút phát nội dung.

6 Không cần sửa đổi tệp chuỗi string.xml trong studio Android.
7 Chạy ứng dụng trên bộ mô phỏng Android và xác minh kết quả của những thay đổi được thực hiện trong ứng dụng.

Sau đây là nội dung của tệp main activity MainActivity.java. Tập tin này có thể bao gồm các phương pháp chu kỳ cơ bản. Chúng tôi đã thêm phương thức broadcastIntent() để phát thông báo tùy chỉnh.

package com.example.oktot.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

   /** Gọi khi activity đầu tiên được tạo. */
   @Override
   
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }


   // broadcast một nội dung tùy chỉnh
      
   public void broadcastIntent(View view){
      Intent intent = new Intent();
      intent.setAction("com.oktot.CUSTOM_INTENT"); sendBroadcast(intent);
   }
}
Sau đây là nội dung của MyReceiver.java:
package com.example.oktot.myapplication;

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 tệp AndroidManifest.xml. Ở đây chúng tôi đã thêm thẻ <receiver … /> để khai báo dịch vụ của chúng ta:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.oktot.myapplication">

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
		
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   
      <receiver android:name="MyReceiver">
         <intent-filter>
            <action android:name="com.oktot.CUSTOM_INTENT">
            </action>
         </intent-filter>

      </receiver>
   </application>

</manifest>
Sau đây sẽ là nội dung của tệp res/layout/activity_main.xml để khai báo một nút phát nội dung tùy chỉnh của chúng ta –
<RelativeLayout 
   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">
   
   <TextView
      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" />
      
   <TextView
      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" />
      
   <Button
      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" />

</RelativeLayout>
Hãy thử chạy Hello World của chúng tôi đã sửa đổi! Tôi cho rằng bạn đã tạo ra AVD của bạn trong khi thiết lập môi trường. Để chạy ứng dụng từ Android studio, mở một trong các tệp hoạt động của dự án và nhấp vào biểu tượng Run từ thanh công cụ. Android Studio cài đặt ứng dụng trên AVD của bạn và khởi động ứng dụng đó và nếu mọi thứ đều ổn với thiết lập và ứng dụng của bạn, nó sẽ hiển thị cửa sổ Emulator sau –
Bây giờ để phát nội dung tùy chỉnh, hãy nhấp vào nút Broadcast Intent
No automatic alt text available.
Bạn có thể thử triển khai BroadcastReceiver khác để tạo ra các thông báo như  khởi động hệ thống, ngày thay đổi, pin thấp …

This site uses Akismet to reduce spam. Learn how your comment data is processed.