RemoteViewsService不执行,

时间:2013-05-11 09:13:55

标签: android widget

我想用里面的列表做一个小部件,我必须实现一个扩展RemoteViewsService的类,但是这个类永远不会执行,我的代码如下:

MyWidgetProvider

public class MyWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);

    Intent updateIntent = new Intent(context, WidgetService.class);
    for (int appWidgetId : appWidgetIds) {
        updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME)));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
            views.setRemoteAdapter(appWidgetId, updateIntent);
        else
            views.setRemoteAdapter(appWidgetId, R.id.events_list, updateIntent);

        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.events_list);         
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}

WidgetService

public class WidgetService extends RemoteViewsService {
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new WidgetFactory(getApplicationContext(), intent);
    }
}

和清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.widget.example"
android:versionCode="1"
android:versionName="1.0" >

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

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

    <receiver android:name=".MyWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info" /> 
    </receiver>

    <service android:name=".WidgetService"
        android:permission="android.permission.BIND_REMOTEVIEWS"
        android:exported="false">
    </service>

</application>

编辑; 我发现了错误,我必须使用:

views.setRemoteAdapter(R.id.events_list, updateIntent);

而不是

views.setRemoteAdapter(appWidgetId, updateIntent);

2 个答案:

答案 0 :(得分:1)

请参阅RemoteViews.html#setRemoteAdapter

在API级别14+中不推荐使用带有3个参数的SetRemoteAdapter函数,即使您将3个参数调用为1,也会忽略widget id参数。因此该函数始终只接收2个有效参数,即view_id和intent。

这个函数基本上说我将使用intent作为视图的数据源(由view_id指示)。

答案 1 :(得分:0)

对我来说这是有效的

 @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    for (int widgetId : appWidgetIds) {
        RemoteViews mView = initViews(context, appWidgetManager, widgetId);
        appWidgetManager.updateAppWidget(widgetId, mView);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}



@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private RemoteViews initViews(Context context,
                              AppWidgetManager widgetManager, int widgetId) {

    RemoteViews mView = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);

    Intent intent = new Intent(context, WidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    mView.setRemoteAdapter(widgetId, R.id.widgetCollectionList, intent);

    return mView;
}