AppWidgetProvider: not called onEnabled method - android

I have widget that display data from content provider. I want to know when data in content provider changes. As far as I know way to do it is
context.getContentResolver().registerContentObserver
But AppWidgetProvider.onEnabled method is not called when I add first instance of the widget.
That's why I can't make registerContentObserver.
The same with onDisabled.
How to solve this problem?
Thanks

You need to add android.appwidget.action.APPWIDGET_ENABLED as another action:
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
<action android:name="android.appwidget.action.APPWIDGET_DELETED" />
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
</intent-filter>
Without that, you will not receive the broadcast that triggers onEnabled().
note: APPWIDGET_DELETED for onDeleted(...),
APPWIDGET_DISABLED for onDisabled(...)

An AppWidgetProvider (or any other manifest-registered BroadcastReceiver) cannot call registerContentObserver(). The entity that is changing your content will need to update your app widget, or you will need to implement some sort of polling mechanism (e.g., check for new content based on android:updatePeriodMillis).

Related

Android Wear: Work around for "com.google.android.gms.wearable.BIND_LISTENER"

I used to use com.google.android.gms.wearable.BIND_LISTENER and WearableListenerService to communicate between my Mobile and Wear device.
After the intent com.google.android.gms.wearable.BIND_LISTENER getting deprecated I am supposed to use:
<service android:name=".MyListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
</intent-filter>
</service>
But how do I combine it with my WearableListenerService? where will the broadcast received?
Like you said the BIND_LISTENER is deprecated now, so according to this thread, the alternative for this is by using a fine-grained intent filter mechanism that allows developers to specify exactly what events they are interested in.
There are intent filters for DATA_CHANGED, MESSAGE_RECEIVED, CHANNEL_EVENT, and CAPABILITY_CHANGED. You can specify multiple elements, and if any of them match, it will call your service and filter out anything else. If you do not include an element, all events will be filtered out and your service will never be called, so make sure to include at least one.
Check also the Live listener part and Best practice to know more information about this issue, including the use of WearableListenerService.

Overriding push notification in BroadcastReceiver (PushBots)?

migrating from Parse I got stuck by not being able to override PushBots notification creation code like I used to do with getNotification function in ParsePushBroadcastReceiver. My project needs multiline and separate push notifications, but PushBots default behavior is to show single line and group notifications by application.
I tried canceling notification in the onReceive function of BroadcastReceiver (PBConstants.EVENT_MSG_RECEIVE event), so I could create & display my own push, but I don't know notification ID and CancelAll() didn't work (also would be a bad idea).
Pushbots.sharedInstance() does have a setNotificationBuilder function, which accepts PBGenerate object, but I have no idea how to construct it properly and if this actually would help my case.
Couldn't find any documentation & get a response from support yet, so asking here if maybe someone knows a solution or a way to workaround this issue.
Here's how receivers are defined in AndroidManifest.xml
<receiver
android:name="com.pushbots.google.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.my.app" />
</intent-filter>
</receiver>
<receiver android:name="com.my.app.MyPushReceiver" />
<service android:name="com.pushbots.push.GCMIntentService" />
Thank you in advance!

How to listen to a change in call log database?

I have a homescreen widget which has to be notified when a missed call has come. I did this with a ContentObserver, but it doesn't work always and I've read that the cause is that ContentObservers must not be registered in an AppWidgetProvider. So I want to try to use a BroadcastReceiver. I've made successfully a calendar changes listener that gets notified each time I add, delete or modify an event in the calendar. I did it in this way:
<receiver
android:name="...backend.observers.CalendarChangesReceiver"
>
<intent-filter>
<action android:name="android.intent.action.PROVIDER_CHANGED"/>
<data android:scheme="content"/>
<data android:host="com.android.calendar"/>
</intent-filter>
</receiver>
I tried to use the above approach to listen to changes in the call log database but it didn't worked. The question is, why? The code is shown below.
My question has not been answered yet in Stackoverflow.
I can not register a receiver to the action ACTION_PHONE_STATE_CHANGED because I do not only want to listen to a new missed call but also I want my application to get notified when the missed call is read by the user. PROVIDER_CHANGED should be useful to achieve this, but ACTION_PHONE_STATE_CHANGED is useless to do this.
<receiver
android:name="...backend.observers.CallsChangesReceiver"
>
<intent-filter>
<action android:name="android.intent.action.PROVIDER_CHANGED"/>
<data android:scheme="content"/>
<data android:host="com.android.phone"/>
</intent-filter>
</receiver>

How can I update a widget on receiving a broadcast?

I'm working on a widget that displays information from a completely separate app. The separate app sends a broadcast when its data is changed, and I want to refresh/update my widget upon receiving this broadcast.
I can't seem to work out how to update a widget from within a BroadcastReceiver however. Is there a way to do this? Or another method to get the same result?
If the separate app has special action for its broadcast, you can add the intent filter to the manifest of your widget. Something like this:
<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="SEPARATE_APP_ACTION" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/example_appwidget_info" />
</receiver>
And just process it in onRecive method of you AppWidgetProvider.
HTH.

How to declare a receiver for a widget to handle your own broadcast along with system broadcast

Suppose I want to define a receiver for my own widget and I want it to handle my own broadcast org.test.mywidget.MY_ACTION along with the APPWIDGET_xxx system broadcasts, what is the correct way to define it, if I want it to handle my own broadcast only if it is sent from the same app package? I tried the following XML code but in this way the APPWIDGET_DELETE action was no more delivered to the widget provider:
<receiver
android:name="MyWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/my_widget_info" />
</receiver>
<receiver
android:name="MyWidgetProvider"
android:exported="false">
<intent-filter>
<action android:name="org.test.mywidget.FORCE_SMALL_WIDGET_UPDATE" />
</intent-filter>
</receiver>
The XML above has the problem I've mentioned (no DELETED events delivered) and it also does not seem good to me, since the receiver is redefined.. So I compacted everything as follows:
<receiver
android:name="MyWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="org.test.mywidget.FORCE_SMALL_WIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/my_widget_info" />
</receiver>
This works, APPWIDGET_UPDATE, APPWIDGET_DELETE and my own broadcast are all delivered but now I have a question: are now other apps able to deliver a broadcast intent with the action org.test.mywidget.FORCE_SMALL_WIDGET_UPDATE to my widget, since the android:exported value is set to true by default? Maybe I'm missing some basic concept related to this exported value, so I would be glad if some of you can make me understand everything better :)
if I want it to handle my own broadcast only if it is sent from the same app package?
You do not need, or even want, an action string if it is all going to be within your own package. Just use the Intent constructor that takes a Java class object as the second parameter, and use that for sending broadcasts to be picked up by the receiver.
I tried the following XML code but in this way the APPWIDGET_DELETE action was no more delivered to the widget provider
That is because there is no <intent-filter> referencing that action string in your code.
are now other apps able to deliver a broadcast intent with the action org.test.mywidget.FORCE_SMALL_WIDGET_UPDATE to my widget, since the android:exported value is set to true by default?
Yes. Of course, third party apps can send APPWIDGET_UPDATE broadcasts, or even hack an Intent that identifies your component directly, and you will receive those as well.
I suspect that the right answer, in your case, is to simply implement a second BroadcastReceiver, one with no <intent-filter>, that handles operations that you solely want to be within your package. Or, if you do not need to use a PendingIntent for this BroadcastReceiver, consider LocalBroadcastManager from the Android Support package.

Categories

Resources