与分离的工作可点击的ImageView插件的多个实例(multiple instances of w

2019-10-23 02:12发布

我试图让ImageView的小工具,当上了插件的用户点击,它改变了形象。

但问题是,当我有多个实例,并点击其中的一个,它在所有情况下改变图像。

我试图在这里的代码: 更新应用程序窗口小部件的多个实例,在Android中 ,但没有工作。

这是我的代码:

package com.appwidget.test;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;


public class MyWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // super.onUpdate(context, appWidgetManager, appWidgetIds);

        // initializing widget layout
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);

        // register for button event
        remoteViews.setOnClickPendingIntent(R.id.widgetImageView, buildButtonPendingIntent(context));

        // request for widget update
        pushWidgetUpdate(context, remoteViews);

    }

    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
        ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, remoteViews);

    }

    public static PendingIntent buildButtonPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setAction("WidgetUtils.WIDGET_UPDATE_ACTION");

        return PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

}


package com.appwidget.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class MyWidgetIntentReceiver extends BroadcastReceiver {


    public static int clickCount = 1;

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals("WidgetUtils.WIDGET_UPDATE_ACTION")){
            updateWidgetPictureAndButtonListener(context);
        }

    }

    private void updateWidgetPictureAndButtonListener(Context context) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
        remoteViews.setImageViewResource(R.id.widgetImageView, getImageToSet());

        remoteViews.setOnClickPendingIntent(R.id.widgetImageView, MyWidgetProvider.buildButtonPendingIntent(context));
        MyWidgetProvider.pushWidgetUpdate(context, remoteViews);

    }

    private int getImageToSet() {

        if (clickCount == 5)
        {
            clickCount = 0;
        }

        int drawables = R.drawable.ui1 + clickCount;

        clickCount++;

        return drawables;
    }

}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.appwidget.test" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <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/widget_provider" />
        </receiver>

        <receiver
            android:name="MyWidgetIntentReceiver"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="WidgetUtils.WIDGET_UPDATE_ACTION" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider" />
        </receiver>

    </application>

</manifest>


<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialLayout="@layout/widget_main"
    android:minWidth="110dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="0"
    android:resizeMode="horizontal|vertical">
    <!--android:previewImage="@drawable/"-->
    <!-- n = Number of cells -->
    <!--70 × n − 30-->

</appwidget-provider>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

       <ImageView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/widgetImageView"
        android:src="@drawable/ui1"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Answer 1:

该方法updateAppWidget(组件名称提供商,RemoteViews视图)设置RemoteViews要用于所提供的AppWidget提供商所有AppWidget实例。

你想用这个方法:updateAppWidget(INT appWidgetId,RemoteViews意见),该设置RemoteViews使用指定的appWidgetId。

要做到这一点,你需要跟踪控件的ID的

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    for (int i = 0; i < N; i++) {
        // the individual id for the widget
        int appWidgetId = appWidgetIds[i];

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);

        // pass appWidgetId to the click handler
        remoteViews.setOnClickPendingIntent(R.id.widgetImageView, buildButtonPendingIntent(context, appWidgetIds[i]));

        // pass appWidgetId to the update method
        pushWidgetUpdate(context, remoteViews, appWidgetIds[i]);

    }
}

然后更新两种方法使用appWidgetId

public static void pushWidgetUpdate(Context context, RemoteViews remoteViews, int appWidgetId) {
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(appWidgetId, remoteViews);

}

public static PendingIntent buildButtonPendingIntent(Context context, int appWidgetId) {
    Intent intent = new Intent();
    // put the appWidgetId as an extra to the update intent
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    intent.setAction("WidgetUtils.WIDGET_UPDATE_ACTION");

    return PendingIntent.getBroadcast(context, appWidgetId, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
}

最后,更新接收器采取appWidgetId额外并将其用于更新调用

@Override
public void onReceive(Context context, Intent intent) {

    if(intent.getAction().equals("WidgetUtils.WIDGET_UPDATE_ACTION")){
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        updateWidgetPictureAndButtonListener(context, int appWidgetId);
    }

} 

private void updateWidgetPictureAndButtonListener(Context context, int appWidgetId) {

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
    remoteViews.setImageViewResource(R.id.widgetImageView, getImageToSet());

    remoteViews.setOnClickPendingIntent(R.id.widgetImageView, MyWidgetProvider.buildButtonPendingIntent(context));
    MyWidgetProvider.pushWidgetUpdate(context, remoteViews, appWidgetId);

}


文章来源: multiple instances of widget with separated working clickable ImageView