建立一个android工程,修改main.xml文件并编写Activity代码,实现三个按钮分别可以发送广播、代码注册广播和代码取消注册广播,点击发送广播按钮可以发送无序广播,若广播接收者已经注册广播,则吐司提示“收到广播”,否则无吐司提示
时间: 2023-12-09 19:03:42 浏览: 214
Android 利用广播发送通知
好的,我会根据你的要求来回答你的问题。首先,这里是一个简单的实现代码:
main.xml文件:
```
<?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="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btn_send_broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="发送广播"/>
<Button
android:id="@+id/btn_register_broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="代码注册广播"/>
<Button
android:id="@+id/btn_unregister_broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代码取消注册广播"/>
</LinearLayout>
```
MainActivity.java文件:
```
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver broadcastReceiver;
private IntentFilter intentFilter;
private Button btnSendBroadcast;
private Button btnRegisterBroadcast;
private Button btnUnregisterBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
private void initView() {
btnSendBroadcast = findViewById(R.id.btn_send_broadcast);
btnRegisterBroadcast = findViewById(R.id.btn_register_broadcast);
btnUnregisterBroadcast = findViewById(R.id.btn_unregister_broadcast);
}
private void initListener() {
btnSendBroadcast.setOnClickListener(v -> {
Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_BROADCAST");
sendBroadcast(intent);
});
btnRegisterBroadcast.setOnClickListener(v -> {
if (broadcastReceiver == null) {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "收到广播", Toast.LENGTH_SHORT).show();
}
};
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcast.MY_BROADCAST");
registerReceiver(broadcastReceiver, intentFilter);
}
});
btnUnregisterBroadcast.setOnClickListener(v -> {
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
}
}
```
这段代码实现了在Activity中发送广播以及代码注册和取消注册广播的功能。在这里,我们定义了一个BroadcastReceiver,用于接收广播。在“代码注册广播”按钮的监听器中,我们判断了broadcastReceiver是否为空,如果为空则创建一个新的broadcastReceiver实例并注册,否则不重复注册。在“代码取消注册广播”按钮的监听器中,我们判断了broadcastReceiver是否为空,如果不为空则取消注册并将broadcastReceiver置为空。同时,在onDestroy()方法中,我们也判断了broadcastReceiver是否为空,如果不为空则取消注册并将其置为空。
阅读全文