将widget配置活动的意图传递给AppWidgetProviderClass

时间:2016-08-23 09:28:39

标签: android android-intent widget android-widget

我创建了应用程序窗口小部件和配置活动来配置应用程序窗口小部件。在我的配置活动中有多个图像视图,其中必须为我的1 * 1窗口小部件仅选择一个图像视图。我在窗口小部件的配置活动中实现它。

下面的

是配置活动的图像 configuration activity

用于在小部件中设置图像的代码

views.setImageViewResource(R.id.img_widget,R.drawable.feed_detail_share);
            Intent defineIntent = new Intent(ConfigurationActivity.this,EditActivity.class);
            defineIntent.putExtra("from_widget",true);
            PendingIntent pendingIntent1 = PendingIntent.getActivity(ConfigurationActivity.this,0 /* no requestCode */, defineIntent, 0 /* no flags */);
            views.setOnClickPendingIntent(R.id.img_widget, pendingIntent1);

单击确定按钮后 widget

下面是设备开启时的图像(当打开窗口小部件的设备为空时)   empty widget

public class WidgetProviderSmall extends AppWidgetProvider {
Context context;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    if (appWidgetIds != null) {
        int N = appWidgetIds.length;        
        for (int mAppWidgetId : appWidgetIds) { 
            Intent intent = new Intent(context, UpdateSmallWidgetService.class);
            intent.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId);
            intent.setAction("FROM WIDGET PROVIDER");
            context.startService(intent);
        }
    }
}

public static class UpdateSmallWidgetService extends IntentService {
    public UpdateSmallWidgetService() {         
        super("UpdateWidgetService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(UpdateSmallWidgetService.this);

        int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,
                INVALID_APPWIDGET_ID);

        if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {
            try {
                updateNewsAppWidget(appWidgetManager, incomingAppWidgetId,
                        intent);
            } catch (NullPointerException e) {
            }

        }

    }

    public void updateNewsAppWidget(AppWidgetManager appWidgetManager,
            int appWidgetId, Intent intent) {
        Log.v("String package name", this.getPackageName());
        RemoteViews views = new RemoteViews(this.getPackageName(),
                R.layout.layout_appwidget_small);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
}

我没有更新AppWidgetProvider类中的AppWidget。我希望通过将widget配置活动中的intent传递给AppWidgetProviderClass来实现,我不知道它

有什么建议吗?

0 个答案:

没有答案