Android 详解广播机制
广播的种类
静态广播和动态广播
接受系统广播
extends BroadcastReceiver{}
onReceive(Context context, Intent intent)
onReceive方法中用来处理接收到广播后,处理的逻辑
动态注册监听网络变化
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeRecever = new NetworkChangeRecever();
registerReceiver(networkChangeRecever, intentFilter);
静态注册
findViewById(R.id.button).setOnClickListener(view -> {
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
intent.setPackage(getPackageName());
sendBroadcast(intent);
});
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>
动态注册
intentFilter = new IntentFilter("MY_ACTION");
bootCompleteReceiver = new BootCompleteReceiver();
registerReceiver(bootCompleteReceiver, intentFilter);
findViewById(R.id.button1).setOnClickListener(view -> {
sendBroadcast(new Intent("MY_ACTION"));
});
注意点:
// 记得加上setPackage,否则新版本安卓广播可能用不了
intent.setPackage(getPackageName());
有序广播 (带优先级)
findViewById(R.id.button).setOnClickListener(view -> {
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
intent.setPackage(getPackageName());
sendOrderedBroadcast(intent,null);// sendOrderedBroadcast这里多传一个null 参数
});
AndroidManifest.xml,在
增加android:priority优先级
<receiver
android:name=".AnotherBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST" />
</intent-filter>
</receiver>
abortBroadcast();表示这条广播截断,后面的广播接收器无法再收到这条广播。
本地广播
//发送
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
//接收
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
LocalReceiver localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);
//销毁
localBroadcastManager.unregisterReceiver(localReceiver);
//继承BroadcastReceiver
class LocalReceiver extends BroadcastReceiver{
//在onReceive复写方法中写逻辑
}
广播的实践---强制下线
public class MainActivity extends BaseActivity {
private static final String wAccount = "admin";
private static final String wPassword = "123";
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.login_button).setOnClickListener(v -> {
EditText accountView = findViewById(R.id.account);
EditText passwordView = findViewById(R.id.password);
String account = accountView.getText().toString();
String password = passwordView.getText().toString();
if (wAccount.equals(account) && wPassword.equals(password)) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "account or password is wrong", Toast.LENGTH_SHORT).show();
}
});
}
}
public class MainActivity2 extends BaseActivity {
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout2);
findViewById(R.id.force_offline).setOnClickListener(v -> {
sendBroadcast(new Intent("FORCE_OFFLINE"));
});
}
}
public class BaseActivity extends AppCompatActivity {
private OfflinReceiver receive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityController.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityController.removeActivity(this);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receive);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("FORCE_OFFLINE");
receive = new OfflinReceiver();
registerReceiver(receive, intentFilter);
}
class OfflinReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Warning");
builder.setMessage("you will be forced offline,please try login again");
builder.setCancelable(false);
builder.setPositiveButton("OK", (dialog, which) -> {
ActivityController.finishAll();
Intent intent1 = new Intent(context, MainActivity.class);
context.startActivity(intent1);
});
builder.show();
}
}
}
public class ActivityController {
public static List<Activity> activityList = new ArrayList<>();
public static void addActivity(Activity activity) {
activityList.add(activity);
}
public static void removeActivity(Activity activity) {
activityList.remove(activity);
}
public static void finishAll() {
for (Activity activity : activityList) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
标签:void,intentFilter,广播,详解,Intent,new,Android,intent
From: https://www.cnblogs.com/zybao/p/16908356.html