appwidget永远不会进入onUpdate方法

3

我的应用程序小部件从未进入 onUpdate 函数,我添加了 3000 毫秒进行测试,但除了第一次添加之外,它永远不会进入 onUpdate 函数。

我做错了什么?

我的清单文件:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="layout.ClockWidgetClass">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/clock_widget_class_info" />
    </receiver>
</application>

我的时钟小部件:

/**
 * Implementation of App Widget functionality.
 */
public class ClockWidgetClass extends AppWidgetProvider {

    static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
                                final int appWidgetId) {

        //getting shered prefrences
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        String name = preferences.getString("randomString", "");
        if(!name.equalsIgnoreCase(""))
        {
            name = name;  /* Edit the value here*/
        }

        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget_class);

        //set chosen design visible
        //set all visibility to 0
        views.setViewVisibility(R.id.AnalogClock0, View.INVISIBLE);
        views.setViewVisibility(R.id.AnalogClock1, View.INVISIBLE);
        views.setViewVisibility(R.id.AnalogClock2, View.INVISIBLE);
        views.setViewVisibility(R.id.AnalogClock3, View.INVISIBLE);

        //turning on the correct clock
        if (name.equals("1")) {
            views.setViewVisibility(R.id.AnalogClock0, View.VISIBLE);
        } else if (name.equals("2")) {
            views.setViewVisibility(R.id.AnalogClock1, View.VISIBLE);
        } else if (name.equals("3")) {
            views.setViewVisibility(R.id.AnalogClock2, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.AnalogClock3, View.VISIBLE);
        }

//        views.setTextViewText(R.id.appwidget_text, name);

        //updateting the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);

    }


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // Chain up to the super class so the onEnabled, etc callbacks get dispatched
        super.onReceive(context, intent);
        // Handle a different Intent
        Log.d("reciving", "onReceive()" + intent.getAction());

    }


}

我的小部件XML文件:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/clock_widget_class"
    android:initialLayout="@layout/clock_widget_class"
    android:minHeight="110dp"
    android:minWidth="250dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="3000"
    android:widgetCategory="home_screen"></appwidget-provider>
1个回答

4
从时间上来讲,“永远”并不准确。在你期望的时间内可能不会发生。在updatePeriodMillis文档中,你会发现一个重要的注释:

注意:使用updatePeriodMillis请求更新将不会超过30分钟以上。

因此,在下一次调用onUpdate()之前,你可能需要等待这30分钟。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接