如何在没有共享首选项的情况下重新启动后维护app 1 * 1小部件

时间:2016-09-02 13:22:24

标签: android android-widget android-appwidget

我创建了1 * 1小部件。小部件的布局包含ImageView,图像视图的图像设置如下

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout_appwidget_small);
    Intent defineIntent = new Intent(context, MainActivity.class);
    defineIntent.putExtra("from_widget", true);
    PendingIntent pendingIntent1;   
    views.setImageViewResource(R.id.img_widget,R.drawable.ic_launcher);
    defineIntent.putExtra("type_from_widget", 1);
    pendingIntent1 = PendingIntent.getActivity(context, 0, defineIntent, 0);
    views.setOnClickPendingIntent(R.id.img_widget, pendingIntent1);
    appWidgetManager.updateAppWidget(intExtra, views);

但我通过检查ConfigurationActivity中的某些条件为ImageView选择图像。 我将小部件的详细信息存储在AppWidgetId的共享首选项中,并在重新启动后进行维护,需要几秒钟才能加载。 在清除应用程序数据后重新启动设备时,窗口小部件变为空白的另一个主要原因

以共享首选项存储数据。

SharedPreferences prefs = context.getSharedPreferences("prefs", 0);
SharedPreferences.Editor edit = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(MainActivity.widgetBeanArrayList);
edit.putString("widget_arraylist", json);
edit.commit();

在AppWidgetProvider类的onReceive方法中检索它并维护我的小部件

    @Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if(intent.getBooleanExtra("from_config_activity",false)){
        for (int mAppWidgetId : intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)) {
            update(mAppWidgetId,intent.getIntExtra("image_id",0));
        }
    }
    if(intent.getBooleanExtra("switch_on",false)){

        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
        SharedPreferences settings = context.getSharedPreferences("prefs", 0);
        if(settings != null) {
            Gson gson = new Gson();
            String json = settings.getString("widget_arraylist", null);
            Type type = new TypeToken<ArrayList<WidgetBean>>() {}.getType();
            MainActivity.widgetBeanArrayList = gson.fromJson(json, type);

            if(MainActivity.widgetBeanArrayList!=null) {
                if (MainActivity.widgetBeanArrayList.size() > 0){
                    for(int i = 0;i < MainActivity.widgetBeanArrayList.size();i++){
                        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout_appwidget_small);
                        if(MainActivity.widgetBeanArrayList.get(i).icon == 1) {
                            views.setImageViewResource(R.id.img_widget, R.drawable.feed_detail_share);
                        }else if(MainActivity.widgetBeanArrayList.get(i).icon == 2){
                            views.setImageViewResource(R.id.img_widget, R.drawable.feed_detail_edit);
                        }else if(MainActivity.widgetBeanArrayList.get(i).icon == 3){
                            views.setImageViewResource(R.id.img_widget, R.drawable.feed_list_audio);
                        }
                        Intent defineIntent = new Intent(context, MainActivity.class);
                        defineIntent.putExtra("from_widget", true);
                        PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, defineIntent, 0);
                        views.setOnClickPendingIntent(R.id.img_widget, pendingIntent1);
                        appWidgetManager.updateAppWidget(MainActivity.widgetBeanArrayList.get(i).id, views);
                    }

                }
            }

        }else{
            Log.d("tag", "=============settingd=====null===");
        }
    }

我必须维护窗口小部件图像,而不将详细信息存储在共享首选项中。 有什么建议吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

在onEnabled()中重启设备后制作逻辑会更好。

   @Override
      public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created(this callback gonna be first after rebooting device)
        final AppWidgetManager appWidgetManager = 
       AppWidgetManager.getInstance(context);
        int widgets_IDs[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetProvider.class));
        for (int widget_id : widgets_IDs) {
         //...get data from shared preference and fill ur widgets by ids
        }
    }

删除app数据后,没有正确的方法可以返回小部件的当前状态。因此,只需移动用户配置活动,让他为小部件选择新设置