概念:
一直接发送数据:
Intent i=new Intent(MainActivity.this,MyBC.class);
i.putExtra("txt","hello wrold");
sendBroadcast(i);
二。动态注册
MainActivity界面
private final MyBC myBC=new MyBC();
发送
Intent i=new Intent(MyBC.ACTION);//或者用这种方式
i.putExtra("txt","hello wrold");//数据
sendBroadcast(i);//发送数据、
注册
registerReceiver(myBC,new IntentFilter(MyBC.ACTION));/
取消注册
unregisterReceiver(myBC);
MyBC界面
public static final String ACTION="com.example.bc.intent.action.MyBC";//定义action
public void onReceive(Context context, Intent intent) {
//接受数据
}
MainActivity发送注册取消注册广播数据,MyBC界面接受数据
展示:
一 MainActivity界面中有三个butto通过点击button注册,发送,取消注册广播
package com.example.bc;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnSend;
private Button btnRegister;
private Button btnUnRegister;
private final MyBC myBC=new MyBC();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSend=(Button)findViewById(R.id.btn);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent i=new Intent(MainActivity.this,MyBC.class);
Intent i=new Intent(MyBC.ACTION);//或者用这种方式
i.putExtra("txt","hello wrold");//数据
sendBroadcast(i);//发送数据
}
});
btnRegister=(Button)findViewById(R.id.btnRegister);//注册广播接收器
btnUnRegister=(Button)findViewById(R.id.btnUnregister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
registerReceiver(myBC,new IntentFilter(MyBC.ACTION));//注册广播接收器
System.out.println("----registerReceiver");
}
});
btnUnRegister=(Button)findViewById(R.id.btnUnregister);//取消广播接收器
btnUnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
unregisterReceiver(myBC);//取消广播接受起
System.out.println("----unregisterReceiver");
}
});
}
}
MyBC界面,直接接受数据
package com.example.bc;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBC extends BroadcastReceiver {
public static final String ACTION="com.example.bc.intent.action.MyBC";
public void onReceive(Context context, Intent intent) {
System.out.println("----------------onReceive");
String str=intent.getStringExtra("txt");
System.out.println("-----------------------"+str); //接收到MainActivity中的数据
}
}
activity_main.xml
添加3个button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="135dp"
android:layout_marginLeft="135dp"
android:layout_marginTop="137dp"
android:text="Send"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginTop="278dp"
android:text="Register"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnUnregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="271dp"
android:layout_marginEnd="33dp"
android:layout_marginRight="33dp"
android:text="UnRegister"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
注册receiver
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bc">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBC"> </receiver>
</application>
</manifest>