2013-05-11 22 views
6

tôi muốn làm một widget với một danh sách bên trong, tôi phải thực hiện một lớp mở rộng của RemoteViewsService, nhưng lớp này không bao giờ thực hiện, mã của tôi là như sau:RemoteViewsService không thực hiện,

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

Và Manifest

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

Chỉnh sửa; tôi đã tìm thấy lỗi, tôi phải sử dụng:

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

thay vì

views.setRemoteAdapter(appWidgetId, updateIntent); 

Trả lời

1

Xem RemoteViews.html#setRemoteAdapter

Chức năng SetRemoteAdapter với 3 thông số bị phản đối ở mức API 14+, và thậm chí nếu bạn gọi 3 tham số, tham số id tiện ích sẽ bị bỏ qua. Vì vậy, hàm luôn nhận được chỉ 2 tham số hợp lệ, tức là view_id và intent.

Chức năng này về cơ bản nói rằng tôi sẽ sử dụng mục đích làm nguồn dữ liệu cho chế độ xem (được chỉ ra bởi view_id).

0

cho tôi công trình này

@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; 
} 
Các vấn đề liên quan