更新的Andr​​oid应用程序窗口小部件的多个实例 [英] Updating multiple instances of App Widget in Android

查看:128
本文介绍了更新的Andr​​oid应用程序窗口小部件的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建的应用程序窗口小部件的多个实例,并分别更新每个人,但找不到我们为什么它不能正常工作。这里是code,从这里收集并稍作修改

 包com.example.widgetagain;进口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){        对(INT appWidSingle = 0; appWidSingle&下; appWidgetIds.length; appWidSingle ++){
            //初始化窗口小部件布局
            RemoteViews remoteViews =新的RemoteViews(context.getPackageName()
                    R.layout.widget_main);            //注册按钮事件
            remoteViews.setOnClickPendingIntent(R.id.sync_button,
                    buildButtonPendingIntent(上下文,appWidSingle));            //初始数据更新视图
            remoteViews.setTextViewText(R.id.title函数,getTitle());
            remoteViews.setTextViewText(R.id.desc,getDesc());            //请求窗口更新
            pushWidgetUpdate(背景下,remoteViews,appWidSingle);
        }    }    公共静态的PendingIntent buildButtonPendingIntent(上下文的背景下,诠释appWidgetSingleId){
        ++ MyWidgetIntentReceiver.clickCount;        意图clickIntent =新意图(背景下,MyWidgetIntentReceiver.class);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetSingleId);
        clickIntent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);
        返回PendingIntent.getBroadcast(背景下,appWidgetSingleId,clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);        //初始化窗口更新请求
        / *意向意图=新的Intent();
        intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);
        返回PendingIntent.getBroadcast(背景下,0,意向,
                PendingIntent.FLAG_UPDATE_CURRENT); * /
    }    私有静态的CharSequence getDesc(){
        回到同步看到一些有趣的我们的笑话集合;
    }    私有静态的CharSequence的getTitle(){
        返回有趣的笑话;
    }    公共静态无效pushWidgetUpdate(上下文的背景下,RemoteViews remoteViews,诠释appWidgetSingleId){        AppWidgetManager经理= AppWidgetManager.getInstance(背景);
        RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.widget_main);        意图clickIntent =新意图(背景下,MyWidgetIntentReceiver.class);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetSingleId);
        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(背景下,appWidgetSingleId,clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);        views.setOnClickPendingIntent(R.id.sync_button,的PendingIntent);
        manager.updateAppWidget(appWidgetSingleId,意见);
    }
}


 包com.example.widgetagain;进口android.appwidget.AppWidgetManager;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.RemoteViews;公共类MyWidgetIntentReceiver扩展广播接收器{
    公共静态INT的clickCount = 0;
    私人弦乐味精[] = NULL;
    INT为widgetid;    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){    / *如果(intent.getAction()== NULL){
            捆绑额外= intent.getExtras();
            如果(临时演员!= NULL){
                为widgetid = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
                //为具有appWidgetId =为widgetid小部件做一些事情
            }
        }
        其他{
            //super.onReceive(context,意向);
        } * /        如果(intent.getAction()。等于(WidgetUtils.WIDGET_UPDATE_ACTION)){            捆绑额外= intent.getExtras();
            如果(临时演员!= NULL){
                为widgetid = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
                //为具有appWidgetId =为widgetid小部件做一些事情
            }            updateWidgetPictureAndButtonListener(背景下,为widgetid);
        }
    }    私人无效updateWidgetPictureAndButtonListener(上下文的背景下,诠释为widgetid){
        RemoteViews remoteViews =新的RemoteViews(context.getPackageName()
                R.layout.widget_main);        //更新视图
        remoteViews.setTextViewText(R.id.title函数,getTitle());
        remoteViews.setTextViewText(R.id.desc,getDesc(上下文));        //重新登记的点击监听器
        remoteViews.setOnClickPendingIntent(R.id.sync_button,
                MyWidgetProvider.buildButtonPendingIntent(上下文,为widgetid));        MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),remoteViews,为widgetid);
    }    私人字符串getDesc(上下文的背景下){
        //从XML一些静态的笑话
        味精= context.getResources()getStringArray(R.array.string_array_name)。
        如果(的clickCount> = msg.length){
            的clickCount = 0;
        }
        回报味精[的clickCount]
    }    私人字符串的getTitle(){
        返回有趣的笑话;
    }
}


 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.widgetagain
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =17/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <接收机器人:MyWidgetProviderNAME =>
            &所述;意图滤光器>
                <作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/>
            &所述; /意图滤光器>            &所述;元数据
                机器人:名字=android.appwidget.provider
                机器人:资源=@ XML / widget_info/>
        < /接收器>        <接收
            机器人:名字=。MyWidgetIntentReceiver
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=WidgetUtils.WIDGET_UPDATE_ACTION/>
            &所述; /意图滤光器>            &所述;元数据
                机器人:名字=android.appwidget.provider
                机器人:资源=@ XML / widget_info/>
        < /接收器>
    < /用途>< /清单>


 <?XML版本=1.0编码=UTF-8&GT?;
< appwidget提供商的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:initialLayout =@布局/ widget_main
    安卓了minHeight =146dp
    安卓了minWidth =292dp
    机器人:previewImage =@绘制/ ic_launcher
    机器人:updatePeriodMillis =1000000>
< / appwidget提供商>


 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5SP
    机器人:背景=@绘制/ ic_launcher
    机器人:方向=垂直>    < RelativeLayout的
        机器人:ID =@ + ID / buttonContainer
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真正的>        <按钮
            机器人:ID =@ + ID / sync_button
            机器人:layout_width =30dp
            机器人:layout_height =30dp
            机器人:layout_centerInParent =真
            机器人:背景=@绘制/ ic_launcher
            机器人:文字=/>
    < / RelativeLayout的>    <的LinearLayout
        机器人:ID =@ + ID / contentContainer
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ ID / buttonContainer
        机器人:layout_alignParentTop =真
        机器人:方向=垂直
        机器人:填充=8DP>        <的TextView
            机器人:ID =@ + ID /标题
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:MAXLINES =2
            机器人:paddingBottom会=5DP
            机器人:文字=
            机器人:文字颜色=#FCFCFC
            机器人:TEXTSIZE =16SP
            机器人:文字样式=大胆/>        <的TextView
            机器人:ID =@ + ID / DESC
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:MAXLINES =5
            机器人:文字=
            机器人:文字颜色=#FCFCFC
            机器人:TEXTSIZE =13SP
            机器人:TEXTSTYLE =正常/>
    < / LinearLayout中>< / RelativeLayout的>


解决方案

根据您发布它似乎这个:

  //为窗口更新请求
pushWidgetUpdate(背景下,remoteViews,appWidSingle);

或许应该是这​​样,而不是:

  //为窗口更新请求
pushWidgetUpdate(上下文,remoteViews,appWidgetIds [appWidSingle]);

同样的:

  //注册按钮事件
remoteViews.setOnClickPendingIntent(R.id.sync_button,
    buildButtonPendingIntent(上下文,appWidSingle));

应该是:

  //注册按钮事件
remoteViews.setOnClickPendingIntent(R.id.sync_button,
    buildButtonPendingIntent(上下文,appWidgetIds [appWidSingle]));

Trying to create multiple instances of app widget and update each of them separately, but can't find our why it's not working properly. Here is the code, collected from here and modified somewhat:

package com.example.widgetagain;

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) {

        for (int appWidSingle = 0; appWidSingle < appWidgetIds.length; appWidSingle++) {
            // initializing widget layout
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_main);

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

            // updating view with initial data
            remoteViews.setTextViewText(R.id.title, getTitle());
            remoteViews.setTextViewText(R.id.desc, getDesc());

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

    }

    public static PendingIntent buildButtonPendingIntent(Context context, int appWidgetSingleId) {
        ++MyWidgetIntentReceiver.clickCount;

        Intent clickIntent = new Intent(context, MyWidgetIntentReceiver.class);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetSingleId);
        clickIntent.setAction("WidgetUtils.WIDGET_UPDATE_ACTION");
        return PendingIntent.getBroadcast(context, appWidgetSingleId, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // initiate widget update request
        /*Intent intent = new Intent();
        intent.setAction("WidgetUtils.WIDGET_UPDATE_ACTION");
        return PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);*/
    }

    private static CharSequence getDesc() {
        return "Sync to see some of our funniest joke collections";
    }

    private static CharSequence getTitle() {
        return "Funny Jokes";
    }

    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews, int appWidgetSingleId) {

        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);

        Intent clickIntent = new Intent(context, MyWidgetIntentReceiver.class);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetSingleId);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetSingleId, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        views.setOnClickPendingIntent(R.id.sync_button, pendingIntent);
        manager.updateAppWidget(appWidgetSingleId, views);
    }
}  


package com.example.widgetagain;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;

public class MyWidgetIntentReceiver extends BroadcastReceiver {
    public static int clickCount = 0;
    private String msg[] = null;
    int widgetId;

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

    /*  if (intent.getAction()==null) {
            Bundle extras = intent.getExtras();
            if(extras!=null) {
                widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
                // do something for the widget that has appWidgetId = widgetId
            }
        }
        else {
            //super.onReceive(context, intent);
        }*/

        if (intent.getAction().equals("WidgetUtils.WIDGET_UPDATE_ACTION")) {

            Bundle extras = intent.getExtras();
            if(extras!=null) {
                widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
                // do something for the widget that has appWidgetId = widgetId
            }

            updateWidgetPictureAndButtonListener(context, widgetId);
        }
    }

    private void updateWidgetPictureAndButtonListener(Context context, int widgetId) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_main);

        // updating view
        remoteViews.setTextViewText(R.id.title, getTitle());
        remoteViews.setTextViewText(R.id.desc, getDesc(context));

        // re-registering for click listener
        remoteViews.setOnClickPendingIntent(R.id.sync_button,
                MyWidgetProvider.buildButtonPendingIntent(context, widgetId));

        MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),remoteViews, widgetId);
    }

    private String getDesc(Context context) {
        // some static jokes from xml
        msg = context.getResources().getStringArray(R.array.string_array_name);
        if (clickCount >= msg.length) {
            clickCount = 0;
        }
        return msg[clickCount];
    }

    private String getTitle() {
        return "Funny Jokes";
    }
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.widgetagain"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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_info" />
        </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_info" />
        </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:minHeight="146dp"
    android:minWidth="292dp"
    android:previewImage="@drawable/ic_launcher"
    android:updatePeriodMillis="1000000" >
</appwidget-provider>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5sp"
    android:background="@drawable/ic_launcher"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/buttonContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/sync_button"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerInParent="true"
            android:background="@drawable/ic_launcher"
            android:text="" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/contentContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/buttonContainer"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:padding="8dp" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:paddingBottom="5dp"
            android:text=""
            android:textColor="#fcfcfc"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:maxLines="5"
            android:text=""
            android:textColor="#fcfcfc"
            android:textSize="13sp"
            android:textStyle="normal" />
    </LinearLayout>

</RelativeLayout>

解决方案

Based on what you've posted it seems that this:

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

should probably look like this instead:

// request for widget update
pushWidgetUpdate(context, remoteViews, appWidgetIds[appWidSingle]);

Likewise:

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

should be:

// register for button event
remoteViews.setOnClickPendingIntent(R.id.sync_button,
    buildButtonPendingIntent(context, appWidgetIds[appWidSingle]));

这篇关于更新的Andr​​oid应用程序窗口小部件的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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