2013-05-11 54 views
6

我想要做一個列表控件裏面,我要實現的是RemoteViewsService的擴展一個類,但這個類從來沒有執行,我的代碼如下: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); 

回答

1

RemoteViews.html#setRemoteAdapter

三個參數SetRemoteAdapter函數已不再API等級14+,甚至如果你調用了3個參數之一,那麼widget id參數將被忽略。所以該函數總是隻接收2個有效參數,即view_id和intent。

這個函數基本上說我將使用意圖作爲視圖的數據源(由view_id表示)。

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; 
}