具有分离工作可点击ImageView的多个小部件实例

时间:2015-05-11 17:52:42

标签: android android-widget

我正在尝试制作ImageView小部件,当用户点击小部件时,它会更改图像。

但问题是,当我有多个实例,并点击其中一个实例时,它会在所有实例中更改图像。

我在这里尝试了代码: Updating multiple instances of App Widget in 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>

1 个答案:

答案 0 :(得分:3)

方法updateAppWidget(ComponentName提供程序,RemoteViews视图)将RemoteView设置为用于所提供的AppWidget提供程序的所有AppWidget实例。

您想要使用此方法:updateAppWidget(int appWidgetId,RemoteViews视图),它将RemoteView设置为用于指定的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);

}