"developers.android.com" at this section of this page has pointed:

The onUpdate() method will not be called when the App Widget is created (the system will not send the ACTION_APPWIDGET_UPDATE broadcast when a configuration Activity is launched). It is the responsibility of the configuration Activity to request an update from the AppWidgetManager when the App Widget is first created.

So I created a simple Android Studio "Hello World!" Project and then made the minimal changes to it (see the end).

Now when I build my App and then try to insert its widget on home screen, the configuration Activity will be launched and immediately these messages will appear in logcat window:

12-23 01:20:54.429 19010-19010/com.example.widgettest I/XXX: Received action: android.appwidget.action.APPWIDGET_ENABLED
12-23 01:20:54.459 19010-19010/com.example.widgettest I/XXX: Received action: android.appwidget.action.APPWIDGET_UPDATE

While according to the mentioned reference the second log must not appear at all!


The minimal changes I made to Android Studio "Hello World!" Project:

  1. Added into AndroidManifest.xml file and between <application></application> tags:

    <receiver android:name=".ExampleAppWidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/example_appwidget_info" />
    </receiver>
    
    <activity android:name=".ExampleAppWidgetConfigure">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
    
  2. Added the file res/xml/example_appwidget_info.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:configure="com.example.widgettest.ExampleAppWidgetConfigure"
        android:initialLayout="@layout/example_appwidget"
        android:minHeight="40dp"
        android:minWidth="40dp"
        android:previewImage="@mipmap/ic_launcher"
        android:updatePeriodMillis="86400000">
    </appwidget-provider>
    
  3. Added the file res/layout/example_appwidget.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Click Me"/>
    
    </RelativeLayout>
    
  1. Added the file ExampleAppWidgetConfigure.java (empty Activity class):

    package com.example.widgettest;
    
    public class ExampleAppWidgetConfigure extends android.app.Activity {
    }
    
  2. Added the file ExampleAppWidgetProvider.java:

    package com.example.widgettest;
    
    import android.appwidget.AppWidgetProvider;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class ExampleAppWidgetProvider extends AppWidgetProvider {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("XXX", "Received action: " + intent.getAction());
        }
    }
    
0

There are 0 answers