动态注册步骤:
1.继承BroadCastReceiver类,自定义一个广播接收器,并且重写onreceive方法.
2.activity对广播进行动态注册使用,首先实例化IntentFilter,并调用其addAction方法设置Action值,然后实例化广播对象,最后调用registerReceiver方法,进行注册,方法参数为IntentFilter和自定义的广播实例。
3.记得取消绑定
发送自定义广播的方法:
Intent(Action act);
sendBroadcast(intent);//无序广播
sendOrderedBroadcast(intent,null);//有序广播
广播接收器中截断广播的方法(有序广播使用)
abortBroadcast();
使用本地广播
因为是在应用程序内部进行传递信息,因此只能进行动态注册来使用,使用LocalBroadcastManager类来进行广播管理。
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
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.Toast;
public class LocalBroadcastActivity extends AppCompatActivity implements View.OnClickListener {
private LocalBroadcastManager localBroadcastManager;
private LocalBroadcastActivity.myBroadcastReceiver myBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_broadcast);
localBroadcastManager= LocalBroadcastManager.getInstance(this);
findViewById(R.id.btn_send).setOnClickListener(this);
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.example.recyclerview.xixia");
myBroadcastReceiver = new myBroadcastReceiver();
localBroadcastManager.registerReceiver(myBroadcastReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(myBroadcastReceiver);
}
@Override
public void onClick(View view) {
Intent intent=new Intent("com.example.recyclerview.xixia");
localBroadcastManager.sendBroadcast(intent);
}
class myBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "本地广播", Toast.LENGTH_SHORT).show();
}
}
}
第一行代码中的BroadcastBestpretice
package com.example.broadcastbestpractice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends BaseActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.forceOffLinear);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent("com.example.broadcastbestpractice.BroadcasrReceive.forceLinea");
sendBroadcast(intent);
}
});
}
}
package com.example.broadcastbestpractice;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity {
private EditText user_name;
private EditText user_password;
private Button submit;
private static final String TAG = "LoginActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d(TAG, "是否加载activity_login页面");
user_name = findViewById(R.id.user_name);
user_password =findViewById(R.id.user_password);
submit=findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(user_name.getText().toString().equals("admit") && user_password.getText().toString().equals("123")){
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(LoginActivity.this, "请输入正确的用户名和密码", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MyStudyApplication">
<activity android:name=".MainActivity"></activity>
<activity
android:name=".LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动强制下线"
android:id="@+id/forceOffLinear"
android:textSize="19sp"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginStart="100dp"/>
</LinearLayout>
package com.example.broadcastbestpractice.Util;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
public class ActivityCollector {
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();
}
}
activityList.clear();
}
}
package com.example.broadcastbestpractice;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.broadcastbestpractice.BroadcasrReceive.ForceOffLineReceive;
import com.example.broadcastbestpractice.Util.ActivityCollector;
public class BaseActivity extends AppCompatActivity {
private ForceOffLineReceive receive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcastbestpractice.BroadcasrReceive.forceLinea");
receive = new ForceOffLineReceive();
registerReceiver(receive, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
if (receive != null)
unregisterReceiver(receive);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
package com.example.broadcastbestpractice.BroadcasrReceive;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.appcompat.app.AlertDialog;
import com.example.broadcastbestpractice.LoginActivity;
import com.example.broadcastbestpractice.Util.ActivityCollector;
public class ForceOffLineReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
alertDialog.setTitle("Warning");
alertDialog.setMessage("You are forced to be offline ,please try to login again");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCollector.finishAll();
Intent intent1=new Intent(context, LoginActivity.class);
context.startActivity(intent1);
}
});
alertDialog.show();
}
}