BroadcastReceiver within a Service not receiving broadcasts Android

前端 未结 2 1809
陌清茗
陌清茗 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("<your receivers intent goes here>");
    this.registerReceiver(mReceiver, filter);
    

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

    Intent intent = new Intent("<your receivers intent goes here>");
    // Add what you want to add to the intent right here.
    <context-handle>.sendBroadcast(intent);
    
    0 讨论(0)
  • 2020-12-21 01:25

    this Activity passes the new variables to a IntentService using a BroadcastReceiver.

    That makes no sense. Use startService() to send a command to an IntentService. And an IntentService should not have a BroadcastReceiver, because the IntentService will be destroyed as soon as onHandleIntent() completes and therefore will never receive the broadcast.

    I've tried using LocalBroadcastManager in this project since the broadcasts are all local but eclipse doesn't seem to be able to import the compatibility class.

    :: shrug ::

    Here is a sample project with Eclipse project files that uses LocalBroadcastManager. I encountered no particular Eclipse issues when creating the project.

    0 讨论(0)
提交回复
热议问题