android - Android Widget不更新RemoteViewsService

android - Android Widget不更新RemoteViewsService,第1张

调用Widget onUpdate()但列表视图中的数据不会更改

当窗口小部件第一次放置在屏幕上时,它会加载正确的数据 1小时后调用onUpdate(),但内部数据未更新

AppWidget_4X3.java

public class AppWidget_4X3 extends AppWidgetProvider {

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int i = 0; i < appWidgetIds.length;   i) {
        Intent intent = new Intent(context, widget_Update_Service.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_4_x3);
        rv.setRemoteAdapter(appWidgetIds[i], R.id.widget_list_view_4_x3, intent);
        rv.setEmptyView(R.id.widget_list_view_4_x3, R.id.empty_view);
        appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
    }

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

widget_Update_Service.java

public class widget_Update_Service extends RemoteViewsService {
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new ListRemoteViewsFactory(this.getApplicationContext(), intent);
}}

class ListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
private Context mContext;
private int mAppWidgetId;
int no_of_col;
SharedPreferences sp_no_of_col;
String column_stored = "No_column_stored";
String table_file_name = "table_data";

public ListRemoteViewsFactory(Context context, Intent intent) {
    mContext = context;
    mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
}

public void onCreate() {

    L.msg("onCreate widget_Update_Service");
    sp_no_of_col= mContext.getSharedPreferences(table_file_name, 0);
    no_of_col = sp_no_of_col.getInt(column_stored, 2);

    Calendar c = Calendar.getInstance();
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

    for (int i = 0; i < no_of_col; i  ) {
        mWidgetItems.add(new WidgetItem(i,mContext,dayOfWeek-1));

    }

}

public void onDestroy() {

    try {
        mWidgetItems.clear();
    }catch (Exception e){

    }
}

public int getCount() {
    return no_of_col;
}

public RemoteViews getViewAt(int position) {

    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
    rv.setTextViewText(R.id.widget_item_time_from, mWidgetItems.get(position).time_from);
    rv.setTextViewText(R.id.widget_item_subject,mWidgetItems.get(position).subject);
    rv.setTextViewText(R.id.widget_item_time_to, mWidgetItems.get(position).time_to);

    Bundle extras = new Bundle();

    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);

        rv.setOnClickFillInIntent(R.id.widget_item_time_to, fillInIntent);


    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() {

}

最佳答案:

1 个答案:

答案 0 :(得分:5)

添加appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.widget_list_view_4_x3); 已修复更新问题

AppWidget_4X3.java#

public class AppWidget_4X3 extends AppWidgetProvider {


public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);}


public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

for (int i = 0; i < appWidgetIds.length;   i) {



        Intent intent = new Intent(context, widget_Update_Service.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_4_x3);
        rv.setRemoteAdapter(appWidgetIds[i], R.id.widget_list_view_4_x3, intent);


        rv.setEmptyView(R.id.widget_list_view_4_x3, R.id.empty_view);


 appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.widget_list_view_4_x3);
        appWidgetManager.updateAppWidget(appWidgetIds[i], rv);

    }

super.onUpdate(context, appWidgetManager, appWidgetIds);
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
教程弟学习网站 » android - Android Widget不更新RemoteViewsService

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情