Widget not showing in widget list

2019-02-25 17:48发布

I have an app and have decided to add a widget to it, I've followed all the official documentation for it, but when I install the app on my Galaxy Nexus, the widget doesn't appear in the app/widget list.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dysign.livetubecountdown"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.dysign.livetubecountdown.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.dysign.livetubecountdown.Widget" android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <intent-filter>
            <action android:name="android.appwidget,action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
    </receiver>

</application>

widget_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
                    android:minHeight="80dp"
                    android:minWidth="80dp"
                    android:minResizeHeight="40dp"
                    android:minResizeWidth="40dp"
                    android:updatePeriodMillis="86400000"
                    android:initialLayout="@layout/widget_main"
                    android:previewImage="@drawable/ic_launcher"
                    android:widgetCategory="home_screen"
                    android:resizeMode="horizontal|vertical">
</appwidget-provider>

widget_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/widgetTextView"/>
</FrameLayout>

Widget.java

package com.dysign.livetubecountdown;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class Widget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerClass(context, appWidgetManager), 1, 1000);
    }

    private class TimerClass extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());

        public TimerClass(Context context, AppWidgetManager appWidgetManager) {
            this.appWidgetManager = appWidgetManager;
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
            thisWidget = new ComponentName(context, Widget.class);
        }

        @Override
        public void run() {
            remoteViews.setTextViewText(R.id.widgetTextView, "TIME = " +format.format(new Date()));
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }
    }
}

I've been looking all over the web to try to fix this, but every suggestion I try just doesn't work. Also there are no errors in LogCat or any mention of anything widget related.

Any help is much appreciated, thanks!

2条回答
神经病院院长
2楼-- · 2019-02-25 17:57

The problem turned out to be a typo in the manifest.

<action android:name="android.appwidget,action.APPWIDGET_UPDATE" />

Should of been:

<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

Notice the comma after android.appwidget.

查看更多
Bombasti
3楼-- · 2019-02-25 17:57

I have experienced the same problem a few times.

The thing that helped me was rebooting my phone, after that the widget started appearing in the widget list. Why is this happening i dont know i have googled and found out that many have similar problems, that it even happens that users install the app from the Market and the widget doesnt appear in the list.

Some solutions that were offered by others: re-installing the app(didn work for me), starting its main activity a few times(also nothing), rebooting phone(this one worked for me, since it even happened during development that the widget works fine for days and the suddenly it was missing in the list when re-installing from eclipse??? Rebooting the phone helped me.

Hope it helps!

查看更多
登录 后发表回答