靜態和動態註冊廣播以及向廣播接收器傳送廣播資訊
阿新 • • 發佈:2022-05-31
靜態和動態註冊廣播
對於動態註冊,需要開啟程式才能接收廣播,對於靜態註冊則彌補這個短板,但是靜態註冊的廣播接收器對於所有應用都可以接收
<!-- 靜態註冊廣播-->
<receiver android:name=".MyBrocastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.neostra.test.Action"/>
</intent-filter>
</receiver>
packagecom.neostra.test;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
BroReceiver broReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*動態註冊廣播*/
broReceiver=new BroReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.neostra.test.Action2");
registerReceiver(broReceiver,intentFilter);//註冊廣播接收器 引數:廣播接收器,意圖過濾(過濾出指定Action的內容)
Log.d("oy","廣播註冊成功");
findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*給靜態註冊的廣播接收者傳送廣播*/
/*Intent intent = new Intent("com.neostra.test.Action");//指定傳送的剛播訊息
intent.setComponent(new ComponentName("com.neostra.test", "com.neostra.test.MyBrocastReceiver"));//指定廣播接收者
sendBroadcast(intent);*/
/*給動態註冊的廣播接收者傳送廣播發送廣播*/
Intent intent = new Intent();
intent.setAction("com.neostra.test.Action2");//指定傳送的廣播訊息
//intent.setComponent(new ComponentName("com.neostra.test", "com.neostra.test.BroReceiver"));//指定廣播接收者
sendBroadcast(intent);
}
});
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broReceiver);//登出動態註冊的廣播
}
}
靜態和動態註冊廣播
<!-- 靜態註冊廣播-->
<receiver android:name=".MyBrocastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.neostra.test.Action"/>
</intent-filter>
</receiver>
package com.neostra.test;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
BroReceiver broReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*動態註冊廣播*/
broReceiver=new BroReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.neostra.test.Action2");
registerReceiver(broReceiver,intentFilter);//註冊廣播接收器 引數:廣播接收器,意圖過濾(過濾出指定Action的內容)
Log.d("oy","廣播註冊成功");
findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*給靜態註冊的廣播接收者傳送廣播*/
/*Intent intent = new Intent("com.neostra.test.Action");//指定傳送的剛播訊息
intent.setComponent(new ComponentName("com.neostra.test", "com.neostra.test.MyBrocastReceiver"));//指定廣播接收者
sendBroadcast(intent);*/
/*給動態註冊的廣播接收者傳送廣播發送廣播*/
Intent intent = new Intent();
intent.setAction("com.neostra.test.Action2");//指定傳送的廣播訊息
//intent.setComponent(new ComponentName("com.neostra.test", "com.neostra.test.BroReceiver"));//指定廣播接收者
sendBroadcast(intent);
}
});
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broReceiver);//登出動態註冊的廣播
}
}