温馨提示×

android中sendbroadcast方法怎么调用

小亿
208
2024-06-03 10:26:08
栏目: 编程语言
Android开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Android中,可以通过以下方式调用sendBroadcast方法:

  1. 在Activity或者Service中调用sendBroadcast方法:
Intent intent = new Intent("your_custom_action");
intent.putExtra("key", "value");
sendBroadcast(intent);
  1. 在BroadcastReceiver中调用sendBroadcast方法:
public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // 在BroadcastReceiver中调用sendBroadcast方法
        Intent newIntent = new Intent("your_custom_action");
        newIntent.putExtra("key", "value");
        context.sendBroadcast(newIntent);
    }
}

需要注意的是,调用sendBroadcast方法时需要指定一个action,其他应用程序可以通过这个action来接收广播。同时,也可以通过putExtra方法传递额外的数据给接收者。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:android中sendbroadcast方法有什么作用

0