2

我试图让一个可调整大小的小部件工作,但它不会。我有一个与 greenDAO 一起使用的数据库。所以我的应用程序应该可以在 Android 2.3+ 上运行。这就是为什么我想拥有以下小部件:

  • 4x2 有 2 件物品
  • 4x3 有 3 个项目
  • 4x4 有 4 个项目
  • 4x5 有 5 件物品

在 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>

谢谢你的帮助!:-)

4

1 回答 1

4

抱歉,我意识到这是一篇旧帖子,但可能是您的问题是您没有在清单文件中包含小部件服务所需的权限....

您可以尝试更改:

<service android:name=".widget.WidgetService" />

<service android:name=".widget.WidgetService" android:permission="android.permission.BIND_REMOTEVIEWS" />
于 2012-11-23T13:11:12.663 回答