我有一个应用程序并决定向它添加一个小部件,我已经遵循了它的所有官方文档,但是当我在我的 Galaxy Nexus 上安装该应用程序时,该小部件没有出现在应用程序/小部件列表中。
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>
小部件.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);
}
}
}
我一直在网上寻找解决这个问题的方法,但我尝试的每一个建议都不起作用。LogCat 中也没有任何错误,也没有提及任何与小部件相关的内容。
非常感谢任何帮助,谢谢!