0

im trying to send string from service to activity - but i cant do this. I found a lot of examples - and nothing not work. Im using LocalBroadcastManger i add this part of code to mainactivity.

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             Logger.e("RECEIVEEEEER!!!!!!!!!!!!!!!!");
        }
    };

and in onResume/onStart

LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(
                mMessageReceiver, new IntentFilter("myIntentKey"));

and in service i add this code

Intent intent = new Intent("myIntentKey");
intent.putExtra("key", "keyfromservice");        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent)

Service start from push notification . Main activity extends from "CordovaActivity". When code in service start - my broadcast receiver not get message(or call blabla) But if sendBroadcast run from main activity then this code work. Guys whats wrong ? Main problem that in service i have some calculation after i need send this new calculation to activity for showing on main UI. Please any can help me ? why i cant send String from Service to Activity.

UPD i have activity which extends from cordovaActivity( so my UI its webpage). I write js part which i call from java for update data on screen. When pushNotificaiton coming - i start service here

 @Override
    public void onMessageReceived(String from, Bundle data) {

for understanding logs in manifest for service i write

 <service android:exported="false" android:name=".services.MyService" android:process=":myProcess">

so in logcat i can pick my process for monitoring.

SOLUTION when i remove

android:process=":myProcess"

broadcast start working as need. Thank to @Mike.M

Peter
  • 2,480
  • 6
  • 39
  • 60
  • did you try to replace the `getApplicationContext()` with `this` in both places? The Context of the Service and the Activity should be the right one. – Christian Jan 23 '17 at 16:51
  • yes i tried this. No changes – Peter Jan 23 '17 at 17:06
  • well the rest of the code seems right. Did you check the service really calls the code and the receiver is not unregistered? No other idea from my side, sorry. – Christian Jan 23 '17 at 17:15
  • did you try with context.sendBroadcast(intent)? I think the notification Service runs in another progress, so LocalBroadcastManager will not work – Leandro Borges Ferreira Jan 23 '17 at 17:20
  • for service i create another process . like android:process=":myNewProcess" can be its problem ? – Peter Jan 23 '17 at 17:55
  • Yep, that's your problem. `LocalBroadcastManager` doesn't work across separate processes. – Mike M. Jan 23 '17 at 20:08
  • how i can fix it ,? – Peter Jan 23 '17 at 23:38
  • As noted in answers on the duplicate linked at the top of your post, either run the `Service` in the same process by removing that attribute from the `` element, or use some sort of IPC, like sending the broadcasts on a `Context`, instead of using `LocalBroadcastManager`. – Mike M. Jan 24 '17 at 00:47
  • 1
    @MikeM. thank Mike , removing android:process=":myProcess" - fixed my problem. – Peter Jan 25 '17 at 23:00

0 Answers0