Android 小部件配置打开 Activity

Android Widget Configuration Open Activity

我想为应用程序创建一个小部件并且已经编写了 widget providerconfiguration classes。我在小部件布局中只有一张图片,单击它时,我希望它打开 MyActivity Activity class。在配置布局上有一个红色图像,当我单击该图像时,小部件图像会发生变化。 问题 是当我单击小部件时,它没有打开 MyActivity

我应该把views.setOnClickPendingIntent放在哪里?进入 appWidgetProvider ?进入 appWidgetConfiguration?进入两者?

问题 2 是:我是否必须将此代码放入 MyActivity 才能使用小部件点击打开它? : sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));

这是我的:

appwidget.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/imageWidget" />
</LinearLayout>

appWidgetProvider.java

public class OffAppWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;               
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
            views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

appWidgetConfiguration.java

public class AppWidgetConfiguration extends Activity{
    private int mAppWidgetId;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);
        setContentView(R.layout.activity_appwidget_configuration);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        image=(ImageView)findViewById(R.id.imageWidgetRed);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context= AppWidgetConfiguration.this;
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                RemoteViews views = new RemoteViews(getPackageName(),R.layout.appwidget);
                views.setImageViewResource(R.id.imageWidget,R.mipmap.widgetred);                        

                appWidgetManager.updateAppWidget(mAppWidgetId, views);

                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
            }
        });   

    }

1) 原来我必须把 views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent); 放到 appWidgetProviderappWidgetConfiguration 行之前 appWidgetManager.updateAppWidget(mAppWidgetId, views);.以下是我为点击侦听器编写的内容:

RemoteViews views = new RemoteViews(getPackageName(),R.layout.appwidget);
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

2) 我可以打开具有 xml 布局的 activity,而无需将此代码放入目标 activity:sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)).但是 MyActivity.java 没有 xml 布局,我不得不将此代码(放入 Oncreate)以便通过单击小部件打开它。第二个选项不是将它放在目标 activity 中,而是在打开 activity 时写入 intent.addCategory(Intent.CATEGORY_LAUNCHER);,如上所示。