4

I understand services and such are being clamped down on, so my receiver has stopped working in Android Oreo.

I have this code starting the service -

Intent intent = new Intent(this, MyService.class);
intent.putExtra("Time", locationUpdatesTime);
intent.putExtra("Dist", locationUpdatesDistance);
startService(intent);

I have this in my service -

Intent intent = new Intent();
intent.setAction("com.androidandyuk.laptimerbuddy");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.putExtra("Lat", thisLat);
intent.putExtra("Lon", thisLon);

sendBroadcast(intent);

But my receiver is never called. Having searched around I think I have to register my receiver, but I can't figure how I code this with the correct syntax. Can anyone help please?

If you wish to downvote me, I'd appreciate if you would comment why, so I can learn as I've looked for an answer, couldn't find/understand it and I think I've laid the question out as I should :-)

Many thanks!

UPDATE Trying to use a LocalBroadcastManager.

In MainActivity I have -

BroadcastReceiver mMessageReceiver;

onCreate I have -

mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            String message = intent.getStringExtra("message");
            Log.i("receiver", "Got message: " + message);
        }
    };
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("custom-event-name"));

In my service, onLocationChanged

    Log.i("sender", "Broadcasting message");
                Intent intent = new Intent();
                intent.setAction("custom-event-name");
                intent.putExtra("message", "This is my message!");

 LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

This works as I see the sender message.

What am I doing wrong?

AndyCr15
  • 465
  • 5
  • 17
  • 1
    Please give a [mcve]. – Code-Apprentice Sep 02 '17 at 15:30
  • 1
    Manifest-registered receivers cannot receive broadcasts of implicit `Intents`. If your sender and receiver are in the same app and same process, stop using system broadcasts (`sendBroadcast()` and `registerReceiver()` on `Context`). Instead, use `LocalBroadcastManager`. Not only does this get you past the changes in Android 8.0, but it improves speed and security. But, as Code-Apprentice suggests, please show how you are registering this receiver. – CommonsWare Sep 02 '17 at 15:32
  • I haven't registered the receiver before... I don't think? But it worked before Oreo. I'm trying to learn by following others code, so only gradually understanding what I'm doing you see. I've tried to follow this - https://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager but my service has no context, which I assume is why it doesn't work? – AndyCr15 Sep 02 '17 at 16:02
  • A `Service` is a `Context`. Do note that `LocalBroadcastManager` does not work across processes. If your `Service` is running in a separate process - i.e., if it has a `process` attribute on its `` element in the manifest - that's not going to work. CommonsWare alludes to that above, but you might've missed it. – Mike M. Sep 02 '17 at 16:51
  • I've done this example - http://sohailaziz05.blogspot.co.uk/2012/04/localbroadcastmanager-intra-application.html on it's own on Oreo and it works, so I'll try and replicate that into my app. Thanks. – AndyCr15 Sep 02 '17 at 17:53

2 Answers2

7

Since Android Oreo, receivers must be registered in runtime using context.registerReceiver(receiver, intentFilter); to receive implicit intents

You can still receive explicit intents and some special implicit actions, as boot_completed or locale_changed for example

More information at https://developer.android.com/about/versions/oreo/background.html#broadcasts

Simone S.
  • 152
  • 2
  • 3
2

create your intent than pass in method

 sendImplicitBroadcast(this,new Intent(IntentActions.ACTION_APP_CREATE));

Static Method

public static void sendImplicitBroadcast(Context ctxt, Intent i) {
    PackageManager pm=ctxt.getPackageManager();
    List<ResolveInfo> matches=pm.queryBroadcastReceivers(i, 0);

    for (ResolveInfo resolveInfo : matches) {
        Intent explicit=new Intent(i);
        ComponentName cn=
                new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
                        resolveInfo.activityInfo.name);

        explicit.setComponent(cn);
        ctxt.sendBroadcast(explicit);
    }
}
Sanjay Hadiya
  • 864
  • 9
  • 21