廣播分爲普通廣播和有序廣播,廣播的註冊方式分爲動態註冊和靜態註冊
動態註冊廣播是在代碼中進行註冊,這種註冊方式下廣播的生命週期隨Activity,不一定同時創建,但是基本同時銷燬。動態註冊廣播的步驟爲:創建廣播接收者,創建過濾器、添加action、註冊廣播,發送廣播
靜態註冊廣播生命週期不受Activity的生命週期影響,這種註冊方式是在AndroidManifest.xml文件中進行配置。
普通廣播的註冊
package com.example.mybroadcast.registerbroadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.mybroadcast.R;
/**
* 動態註冊廣播,此註冊廣播的方式下,廣播的生命週期跟隨Activity,不一定同生,但一般共死
*/
public class DynamicRegisterActivity extends AppCompatActivity {
private Button button;
public static final String ACTION_TEST = "com.example.mybroadcast.TEST";
DynamicRegisterReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_register);
button = findViewById(R.id.btn_dynamic_register);
receiver = new DynamicRegisterReceiver();
/**
* 定義過濾器,添加Action,註冊廣播
*/
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_TEST);
registerReceiver(receiver,intentFilter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//發送廣播
Intent intent = new Intent();
intent.setAction(ACTION_TEST);
sendBroadcast(intent);
}
});
}
/**
* 創建廣播接收者
*/
class DynamicRegisterReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(ACTION_TEST.equals(action)){
Toast.makeText(context,"動態註冊廣播成功",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//註銷廣播
if(receiver != null){
unregisterReceiver(receiver);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_dynamic_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="動態註冊廣播"
android:layout_centerInParent="true"/>
</RelativeLayout>
下面是靜態註冊廣播
package com.example.mybroadcast.registerbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* 靜態註冊廣播,監聽開機
* 監聽手機的開機事件需要在AndroidManifest.xml申請權限
*/
public class StaticRegisterReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("Action爲:"+action);
Toast.makeText(context,"開機了!!!!!",Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybroadcast">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".orderbroadcast.SendOrderBroadcast">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".registerbroadcast.DynamicRegisterActivity">
</activity>
<receiver android:name=".registerbroadcast.StaticRegisterReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".orderbroadcast.HighLevelReceiver">
<!-- priority屬性設置廣播接收者的優先級,取值爲-1000~1000,數值越大優先級越高 -->
<intent-filter android:priority="1000">
<action android:name="com.example.mybroadcast.SEND_ORDER"/>
</intent-filter>
</receiver>
<receiver android:name=".orderbroadcast.LowLevelReceiver">
<intent-filter android:priority="-1000">
<action android:name="com.example.mybroadcast.SEND_ORDER"/>
</intent-filter>
</receiver>
</application>
</manifest>
有序廣播
有序廣播在註冊時要注意爲其設置優先級,有序廣播有終止廣播繼續傳遞的方法,有序廣播可以攜帶數據傳遞,有更改數據的方法。
package com.example.mybroadcast.orderbroadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.mybroadcast.R;
/**
* 發送有序廣播
*/
public class SendOrderBroadcast extends AppCompatActivity {
Button button;
public static final String ACTION_SEND_ORDER = "com.example.mybroadcast.SEND_ORDER";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_order_broadcast);
button = findViewById(R.id.btn_send_order);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(ACTION_SEND_ORDER);
Bundle bundle = new Bundle();
bundle.putCharSequence("msg","這是一個有序廣播");
intent.putExtras(bundle);
sendOrderedBroadcast(intent,null,null,null, Activity.RESULT_OK,null,bundle);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".orderbroadcast.SendOrderBroadcast">
<Button
android:id="@+id/btn_send_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="發送有序廣播"/>
</LinearLayout>
package com.example.mybroadcast.orderbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
* 優先級高的廣播接收者
*/
public class HighLevelReceiver extends BroadcastReceiver {
private static final String TAG = "HighLevelReceiver:";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
String msg = bundle.getCharSequence("msg").toString();
Log.d(TAG,msg);
/**
* abortBroadcast();此方法可以終止有序廣播繼續往下傳播
*/
abortBroadcast();
//設置信息並將信息再放入廣播中
bundle.putCharSequence("msg","信息已經改變");
setResultExtras(bundle);
}
}
package com.example.mybroadcast.orderbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
* 優先級低的廣播接收者
*/
public class LowLevelReceiver extends BroadcastReceiver {
private static final String TAG = "LowLevelReceiver:";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
String msg = bundle.getCharSequence("msg").toString();
Log.d(TAG,msg);
}
}
-----僅爲自己的學習筆記