与分离工作可点击的ImageView小部件的多个实例 [英] multiple instances of widget with separated working clickable ImageView

查看:135
本文介绍了与分离工作可点击的ImageView小部件的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我曾尝试code在这里:
<一href=\"http://stackoverflow.com/questions/17763871/updating-multiple-instances-of-app-widget-in-android\">Updating在Android的应用控件的多个实例
但没有工作。

这是我的code:

 包com.appwidget.test;进口android.app.PendingIntent;
进口android.appwidget.AppWidgetManager;
进口android.appwidget.AppWidgetProvider;
进口android.content.ComponentName;
进口android.content.Context;
进口android.content.Intent;
进口android.widget.RemoteViews;
公共类MyWidgetProvider扩展AppWidgetProvider {    @覆盖
    公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
            INT [] appWidgetIds){
        // super.onUpdate(背景下,appWidgetManager,appWidgetIds);        //初始化窗口小部件布局
        RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget_main);        //注册按钮事件
        remoteViews.setOnClickPendingIntent(R.id.widgetImageView,buildButtonPendingIntent(上下文));        //请求窗口更新
        pushWidgetUpdate(背景下,remoteViews);    }    公共静态无效pushWidgetUpdate(上下文的背景下,RemoteViews remoteViews){
        组件名进myWidget =新的组件名(背景下,MyWidgetProvider.class);
        AppWidgetManager经理= AppWidgetManager.getInstance(背景);
        manager.updateAppWidget(进myWidget,remoteViews);    }    公共静态的PendingIntent buildButtonPendingIntent(上下文的背景下){
        意向意图=新的Intent();
        intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);        返回PendingIntent.getBroadcast(背景下,0,意向,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }}


 包com.appwidget.test;进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.widget.RemoteViews;公共类MyWidgetIntentReceiver扩展广播接收器{
    公共静态INT的clickCount = 1;    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        如果(intent.getAction()。等于(WidgetUtils.WIDGET_UPDATE_ACTION)){
            updateWidgetPictureAndButtonListener(上下文);
        }    }    私人无效updateWidgetPictureAndButtonListener(上下文的背景下){        RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget_main);
        remoteViews.setImageViewResource(R.id.widgetImageView,getImageToSet());        remoteViews.setOnClickPendingIntent(R.id.widgetImageView,MyWidgetProvider.buildButtonPendingIntent(上下文));
        MyWidgetProvider.pushWidgetUpdate(背景下,remoteViews);    }    私人诠释getImageToSet(){        如果(==的clickCount 5)
        {
            的clickCount = 0;
        }        INT可绘制= R.drawable.ui1 +的clickCount;        的clickCount ++;        返回绘制资源;
    }}


 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.appwidget.test&GT;    &lt;应用
        机器人:allowBackup =真
        机器人:图标=@的mipmap / ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme&GT;
        &LT;活动
            机器人:名字=泼水节
            机器人:标签=@字符串/ APP_NAME&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.intent.action.MAIN/&GT;                &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;        &lt;接收机器人:名字=MyWidgetProvider&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/&GT;
            &所述; /意图滤光器&gt;            &所述;元数据
                机器人:名字=android.appwidget.provider
                机器人:资源=@ XML / widget_provider/&GT;
        &LT; /接收器&GT;        &lt;接收
            机器人:名字=MyWidgetIntentReceiver
            机器人:标签=@字符串/ APP_NAME&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=WidgetUtils.WIDGET_UPDATE_ACTION/&GT;
            &所述; /意图滤光器&gt;            &所述;元数据
                机器人:名字=android.appwidget.provider
                机器人:资源=@ XML / widget_provider/&GT;
        &LT; /接收器&GT;    &LT; /用途&gt;&LT; /清单&GT;


 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; appwidget提供商的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:initialLayout =@布局/ widget_main
    安卓了minWidth =110dp
    安卓了minHeight =40dp
    机器人:updatePeriodMillis =0
    机器人:resizeMode =横|纵&GT;
    &LT; - 机器人:previewImage =@绘制/ - &GT;
    &LT;! - N =细胞数 - &GT;
    &LT; - !70×N - 30 - &GT;&LT; / appwidget提供商&GT;


 &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@机器人:彩色/透明&GT;       &LT; ImageView的        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ID =@ + ID / widgetImageView
        机器人:SRC =@绘制/ UI1
        机器人:layout_centerVertical =真
        机器人:layout_centerHorizo​​ntal =真/&GT;&LT; / RelativeLayout的&GT;


解决方案

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

您想要使用此方法:updateAppWidget(INT appWidgetId,RemoteViews意见),它设置RemoteViews用于指定appWidgetId

要做到这一点,你需要跟踪小部件的id的

  @覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
        INT [] appWidgetIds){
    最终诠释N = appWidgetIds.length;    的for(int i = 0; I&LT; N;我++){
        //为插件的个人ID
        INT appWidgetId = appWidgetIds [I]        RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget_main);        //传递appWidgetId到单击处理程序
        remoteViews.setOnClickPendingIntent(R.id.widgetImageView,buildButtonPendingIntent(背景下,appWidgetIds [I]));        //传递appWidgetId到更新方法
        pushWidgetUpdate(上下文,remoteViews,appWidgetIds [I]);    }
}

然后,更新的两个方法为使用appWidgetId

 公共静态无效pushWidgetUpdate(上下文的背景下,RemoteViews remoteViews,诠释appWidgetId){
    AppWidgetManager经理= AppWidgetManager.getInstance(背景);
    manager.updateAppWidget(appWidgetId,remoteViews);}公共静态的PendingIntent buildButtonPendingIntent(上下文的背景下,诠释appWidgetId){
    意向意图=新的Intent();
    //把appWidgetId作为额外的意图更新
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,为widgetid);
    intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);    返回PendingIntent.getBroadcast(背景下,appWidgetId,意向,
            PendingIntent.FLAG_UPDATE_CURRENT);
}

最后,更新接收器采取额外appWidgetId并用它来更新呼叫

  @覆盖
公共无效的onReceive(上下文的背景下,意图意图){    如果(intent.getAction()。等于(WidgetUtils.WIDGET_UPDATE_ACTION)){
        INT appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
        updateWidgetPictureAndButtonListener(背景下,INT appWidgetId);
    }}私人无效updateWidgetPictureAndButtonListener(上下文的背景下,诠释appWidgetId){    RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget_main);
    remoteViews.setImageViewResource(R.id.widgetImageView,getImageToSet());    remoteViews.setOnClickPendingIntent(R.id.widgetImageView,MyWidgetProvider.buildButtonPendingIntent(上下文));
    MyWidgetProvider.pushWidgetUpdate(背景下,remoteViews,appWidgetId);}

I am trying to make ImageView widget, when the user click on the widget, it changes the image.

But the problem is when i have multiple instances, and click on one of them, it changes the image in all instances.

I have tried the code in here: Updating multiple instances of App Widget in Android but didnt work.

This is my code:

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>

解决方案

The method updateAppWidget(ComponentName provider, RemoteViews views) sets the RemoteViews to use for all AppWidget instances for the supplied AppWidget provider.

You want to use this method: updateAppWidget(int appWidgetId, RemoteViews views) which set the RemoteViews to use for the specified appWidgetId.

To do this you will need to keep track of the widget's 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]);

    }
}

Then update the two methods to use the 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);
}

Finally, update the receiver to take the appWidgetId extra and use it for the update call

@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);

}

这篇关于与分离工作可点击的ImageView小部件的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆