我正在学习这篇http://www.vogella.com/articles/AndroidWidgets/article.html教程。
下面是我的Widetprovider:
import java.util.Random;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import com.asco.countdown.R;
public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
@Override
public void onEnabled(Context context) {
Log.d("WID", "enabled");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d("WID", "update");
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
} 这是我的widgetinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="5000000" >
</appwidget-provider> 下面是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name="MyWidgetProvider" >
<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>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetinfo" />
</receiver>
</application>
将显示小部件,但既没有调用onEnabled()方法,也没有调用onUpdate()方法,onClickListener也不起作用。我尝试了在清单中给出AppWidgetProvider的完整路径,将AppWidgetProvider放在另一个包中(现在它在默认包中),等等,但什么都没有发生,现在我没有想法了。有人能帮帮我吗?
发布于 2012-08-24 20:25:46
尝试在maifest中提供全名
<receiver android:name="MyWidgetProvider" >至
<receiver android:name="com.asco.countdown.MyWidgetProvider" > 或者叫什么全名
发布于 2012-08-24 23:23:46
好的,在4.0.3模拟器和2.3设备上测试了它,都工作得很好。似乎是我的自定义4.1 JB的问题。
https://stackoverflow.com/questions/12109287
复制相似问题