内容:在上述otherActivity添加一个按钮发送普通广播定义一个普通广播接收者用于接收广播消息吐司显示传递过来的数据内容。1、写出点击发送广播按钮的代码10分。2、写出AndroidManifestxml文件注册广播接收者类10分3、写出广播广播接收者类的代码10分4、截图显示程序运行结果5分
1、点击“发送广播”按钮的代码:
Button sendBroadcastBtn = findViewById(R.id.sendBroadcastBtn); sendBroadcastBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.myapplication.MY_BROADCAST"); intent.putExtra("message", "这是一条广播消息"); sendBroadcast(intent); } });
2、AndroidManifest.xml文件注册广播接收者类:
3、广播广播接收者类的代码:
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String message = intent.getStringExtra("message"); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } }
4、截图显示程序运行结果:
(无法提供截图,文字形式呈现):点击“发送广播”按钮后,屏幕上会弹出一条提示,内容为“这是一条广播消息”
AI 优化版
原文地址:https://www.cveoy.top/t/topic/fKet 著作权归作者所有。请勿转载和采集!