BroadcastReceiver within a Service not receiving broadcasts Android

前端 未结 2 1822
陌清茗
陌清茗 2020-12-21 01:02

I\'ve got this app, in which users update certain variables in an Activity, and this Activity passes the new variables to a IntentService using a BroadcastReceiver. However,

2条回答
  •  清歌不尽
    2020-12-21 01:23

    protected class UpdateReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            Log.d("receiver", "Got message: ");
            //state=3;
            //Toast.makeText(context, "got it", Toast.LENGTH_SHORT).show();
        }
    };
    

    In the onCreate() method or where relevant.

    mReceiver = new UpdateReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction("");
    this.registerReceiver(mReceiver, filter);
    

    Now you should be able to send a broadcast and it be picked up.

    Intent intent = new Intent("");
    // Add what you want to add to the intent right here.
    .sendBroadcast(intent);
    

提交回复
热议问题