1.以动态注册方式使用自定义广播。
MainActivity
public class MainActivity extends AppCompatActivity {
private TextView tv;
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle mybundle = intent.getExtras();
String str1 = mybundle.getString("data1");
String str2 = mybundle.getString("data2");
Toast.makeText(context, str1+" " +str2,Toast.LENGTH_LONG).show();
tv.setText(str1+" " +str2);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter myintentfliter = new IntentFilter();
myintentfliter.addAction("com.example.broadcast.MY_BROADCAST");
registerReceiver(myReceiver, myintentfliter);
tv = findViewById(R.id.tv);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent myintent = new Intent("com.example.broadcast.MY_BROADCAST");
Bundle bundle = new Bundle();
bundle.putString("data1", "自定义广播与接收案例");
bundle.putString("data2", "李亚琦666");
myintent.putExtras(bundle);
sendBroadcast(myintent);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
主布局
<TextView
android:id="@+id/tv"
android:layout_width="338dp"
android:layout_height="40dp"
android:layout_marginTop="32dp"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv" />
2.以静态注册方式实现的自定义广播及接收。
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
intent.setPackage("com.example.ex622");
Bundle bundle = new Bundle();
bundle.putString("datal", "666");
bundle.putString("data2", "张粤~...Ov0...");
intent.putExtras(bundle);
sendBroadcast(intent);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
MyReceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent){
Bundle mybundle = intent.getExtras();
String str1 = mybundle.getString("data1");
String str2 = mybundle.getString("data2");
Toast.makeText(context,str1+" " +str2,Toast.LENGTH_LONG).show();
}
}
主布局
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
清单文件
<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">
<receiver
android:name="com.example.test5_a.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcast.MY_BROADCAST"/>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>