1

I have an app with a home widget. I use PendingIntent for clicked buttons on it, like so:

Intent intent = new Intent("MY_STRING_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 123, intent, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.my_btn, pendingIntent);

And I receive this intent action in a BroadcastReceiver where I manage it. This works perfectly ONLY till Android 7.1. Due to Android 8 changes about BroadcastReceiver I don't know how to correctly register this broadcast receiver (can't context register it because if activity is destroyed, is's automatically unregistred).

Right now my received is declared in manifest like so:

     <receiver android:name="view.widget.WidgetActionsReceiver">
        <intent-filter>
            <action android:name="MY_STRING_ACTION" />
        </intent-filter>
    </receiver>
Choletski
  • 7,074
  • 6
  • 43
  • 64
  • 1
    Change the `Intent` to an explicit `Intent`, which you really should've been doing all along: `Intent intent = new Intent(appContext, WidgetActionsReceiver.class); intent.setAction("MY_STRING_ACTION");`. If you don't need the action to distinguish anything, you can then it. In either case, the `` is then unnecessary. – Mike M. Feb 13 '18 at 09:52
  • @MikeM. oh yeah, that's right, they even mentioned in docs about explicit intents ;) – Choletski Feb 13 '18 at 09:57
  • Yep. Btw, that should say "If you don't need the action to distinguish anything, you can _omit_ it." Darn autocorrect. – Mike M. Feb 13 '18 at 09:59

0 Answers0