我试图让一个可调整大小的小部件工作,但它不会。我有一个与greenDAO一起使用的数据库。所以我的应用程序应该适用于Android 2.3+。这就是为什么我想得到以下小部件:
在preHoneycomb中,这些Widgets
应该只显示第一项,然后点击即可打开应用。
在Android 3+中,这些widgets
应该是可实现的和可滚动的。我的问题是我的Widget Service类从未被调用过。因此,小部件内部不会被填充: -
这是我的代码:
public class WidgetProvider extends AppWidgetProvider {
private static final String TAG = "WidgetProvider";
@TargetApi(14)
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.i(TAG, "Update!!");
for (int i = 0; i < appWidgetIds.length; ++i) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, TimetableActivity_.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[i]);
svcIntent.setData(Uri.parse(svcIntent
.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget);
views.setOnClickPendingIntent(R.id.img_widget_home, pendingIntent);
views.setRemoteAdapter(R.id.btn_widget_refresh, svcIntent);
Intent update = new Intent(context, WidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pend = PendingIntent.getBroadcast(context, 0, update,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.btn_widget_refresh, pend);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@TargetApi(11)
public class WidgetService extends RemoteViewsService {
private static final String TAG = "WidgetService";
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
Log.i(TAG, "WidgetService called");
return new ListRemoteViewsFactory(this.getApplicationContext(),
(TimetableApplication_) this.getApplication(), intent);
}
}
class ListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private static final String TAG = "ListRemoteViewsFactory";
private Context mContext;
private TimetableApplication_ application;
private List<Events> events = new ArrayList<Events>();
public ListRemoteViewsFactory(Context context,
TimetableApplication_ application, Intent intent) {
this.mContext = context;
this.application = application;
}
public int getCount() {
return events.size();
}
public RemoteViews getViewAt(int position) {
Log.i(TAG, ""+getCount());
String title = "";
String description = "";
Events e;
if ((e = events.get(position)) != null) {
title = e.getSubjects().getTitle();
description = e.getStart().toGMTString();
}
RemoteViews rv = new RemoteViews(mContext.getPackageName(),
R.layout.widget_item);
rv.setTextViewText(R.id.txt_widget_item_title, title);
rv.setTextViewText(R.id.txt_widget_item_description, description);
return rv;
}
public RemoteViews getLoadingView() {
return null;
}
public int getViewTypeCount() {
return 1;
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
if (events != null) {
events.clear();
}
events = application.getEvents();
}
@Override
public void onCreate() {
}
@Override
public void onDestroy() {
}
}
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mprengemann.hwr.timetabel"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".TimetableApplication_"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/TimetableTheme" >
<activity
android:name=".TimetableActivity_"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PreferenceActivity_"
android:label="@string/menu_settings" >
</activity>
<activity android:name=".SubjectDetailActivity_" >
</activity>
<receiver android:name=".widget.WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetprovider" />
</receiver>
<service android:name=".widget.WidgetService" />
<service android:name=".widget.ListWidgetService" />
</application>
</manifest>
感谢您的帮助! : - )
答案 0 :(得分:4)
抱歉,我发现这是一篇旧帖子,但可能是你的问题是你没有在清单文件中包含所需的小部件服务权限....
您可以尝试更改:
<service android:name=".widget.WidgetService" />
要
<service android:name=".widget.WidgetService" android:permission="android.permission.BIND_REMOTEVIEWS" />