Pavel
Pavel

Reputation: 1

Android - receiving implicit broadcast in a widget

I am trying to create a widget, which content is updated based on some implicit system broadcast messages (e.g. android.media.VOLUME_CHANGED_ACTION). I want to use a newer api than 26, so I cannot register the receiver in the manifest. Also it cannot be registered using the context passed to onUpdate method in my AppWidgetProvider implementation.

I've found that calling following code from the onUpdate method almost works:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
Context app_context = context.getApplicationContext();
app_context.registerReceiver(new AudioChangeReceiver(appWidgetId), intentFilter);

The problem is that it stops working after a while. I suppose that the reason is that the app context that was used to register the receiver doesn't exist anymore (killed by the system?). What is the proper way of doing this? Thanks Pavel

Upvotes: 0

Views: 30

Answers (0)

Related Questions