Using a broadcast intent/broadcast receiver to send messages from a service to an activity

前端 未结 4 1133
轻奢々
轻奢々 2020-12-02 06:32

So I understand (I think) about broadcast intents and receiving messages to them.

So now, my problem/what I can\'t work out is how to send a message from the o

4条回答
  •  甜味超标
    2020-12-02 07:13

    To broadcast an intent:

    Intent intent = new Intent("com.yourcompany.testIntent");
    intent.putExtra("value","test");
    sendBroadcast(intent);
    

    To receive the same intent use:

    IntentFilter filter = new IntentFilter("com.yourcompany.testIntent");
            BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                  String value =  intent.getExtras().getString("value");
                }
            };
    registerReceiver(receiver, filter);
    

提交回复
热议问题