Android的appWidget多个实例 [英] Android appWidget multiple instances

查看:249
本文介绍了Android的appWidget多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是pretty的的<一个太大的重复href="http://stackoverflow.com/questions/4225197/android-multiple-appwidgets-playing-diferent-sounds">Android - 多appWidgets打diferent听起来的,但是我的code不工作。我加入了一个额外的意图与小部件ID,使用日志报表来证明这一点,但每当我点击我的部件之一,当有它的多个实例,我只得到了最后appWidgetId。它看起来像第一小部件的意图,第二个加入后正在改变。如果code在其他岗位应工作,那么我一定是忽视的东西,因为我相信我的设置pretty的大致相同的方式。

This question is pretty much a duplicate of Android - Multiple appWidgets playing diferent sounds, but my code isn't working. I am adding an intent extra with the widget id, with log statements to prove it, but whenever I click on one of my widgets when there are multiple instances of it, I only get the last appWidgetId. It looks like the intent of the first widget is changing after the second one is added. If the code in that other post should work, then I must be overlooking something because I believe mine is set up pretty much the same way.

 public class PhcaAppWidgetProvider extends AppWidgetProvider {
     private static final String ACTION_CLICK = "com.skipmorrow.phca.PhcaAppWidgetProvider.WIDGET_CLICKED";
     private final String widgetPageName = "_widget";
     private static final String PREFS_NAME = "PHCA";
     private static final String PREF_PREFIX_KEY = "prefix_";
     private static String MY_DEBUG = "PhcaAppWidget";

     @Override
     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
             int[] appWidgetIds) {
        Log.d(MY_DEBUG, "Provider.onUpdate");
     }

     @Override
     public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        Log.d(MY_DEBUG, "Provider.onReceive(); action = " + intentAction);
        Bundle extras = intent.getExtras();

        // make a list of all extras, just so I can see what is in there...
        if (extras!=null) {
            Set<String> ks = extras.keySet();
            Iterator<String> iterator = ks.iterator();
            while (iterator.hasNext()) {
                String s = iterator.next();
                Log.d(MY_DEBUG, "Provider.Key found: " + s);
            }
        }
        else {
          Log.d(MY_DEBUG, "Provider.extras was null");
        }

            int[] appWidgetIds1 = intent.getIntArrayExtra("appWidgetIds");

            if (extras!=null && extras.containsKey("appWidgetIds")){
                // sometimes the intent comes back with an array of appWidgetIds, and sometimes the appWidgetId is stored
                // in the one extra "appWidgetId" Clicks never come with the array of appWidgetIds. They always come by
                // themselves
                Log.d(MY_DEBUG, "Provider.Intent extras does contain a key appWidgetIds");
                if (appWidgetIds1!=null) Log.d(MY_DEBUG, "Provider.Intent int array appWidgetIds1 size is " + appWidgetIds1.length);
                for (int i = 0; i<appWidgetIds1.length; i++) {
                    int appWidgetId = appWidgetIds1[i];
                    Log.d(MY_DEBUG, "Provider.Intent appWidgetIds1[" + i + "] = " + appWidgetId);
                    if (intentAction.equals("android.appwidget.action.APPWIDGET_UPDATE")) {
                        Log.d(MY_DEBUG, "Provider.APPWIDGET UPDATE");
                        updateWidgetState(context, intentAction, appWidgetId);
                    } else if (intentAction.equals("android.appwidget.action.APPWIDGET_DELETED")) {
                        // do nothing
                    }
                    else {
                        super.onReceive(context, intent);
                    }
                }
            } else {

            String intentData = intent.getDataString();
            Log.d(MY_DEBUG, "Provider.intent data = " + intentData);


            Integer appWidgetId = intent.getIntExtra("appWidgetId", -1);
            Log.d(MY_DEBUG, "Provider.appWidgetId = " + appWidgetId);

            if (intentAction.equals(ACTION_CLICK)) {
                Log.d(MY_DEBUG, "Provider.Clicked");
                SharedPreferences myPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_WORLD_WRITEABLE);
                Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1);
                Log.d(MY_DEBUG, "Provider.ObjNum = " + objNum);
                if (objNum > -1) {
                    Log.d(MY_DEBUG, "Provider.Executing...");
                    PageAction pa = (PageAction) CommonActivity.GetPageObjectAtIndex(context, widgetPageName, objNum);
                    pa.ExecuteActionFromWidgetClick(context);
                }
            }
            else {
                super.onReceive(context, intent);
            } 
            }
     }

     public static void updateWidgetState(Context paramContext, String paramString, Integer appWidgetId)
     {
        Log.d(MY_DEBUG, "Provider.updateWidgetState; appWidgetId = " + appWidgetId + "; paramString = " + paramString);
        RemoteViews localRemoteViews = buildUpdate(paramContext, paramString, appWidgetId);
        ComponentName localComponentName = new ComponentName(paramContext, PhcaAppWidgetProvider.class);
        AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
     }



     private static RemoteViews buildUpdate(Context ctx, String paramString, Integer appWidgetId)
     {
        Log.d(MY_DEBUG, "Provider.buildUpdate(); appWidgetId = " + appWidgetId + "; paramString = " + paramString);
        RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.phca_appwidget);
        if (paramString.equals("android.appwidget.action.APPWIDGET_UPDATE")) {
            Log.d(MY_DEBUG, "Provider.buildUpdate().. APPWIDGET_UPDATE");
            Intent intent = new Intent(ctx, PhcaAppWidgetProvider.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(ACTION_CLICK);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            Uri data = Uri.withAppendedPath(
                Uri.parse("ABCD" + "://widget/id/")
                ,String.valueOf(appWidgetId));
            intent.setData(data);
            Log.d(MY_DEBUG, "Provider.Added intent extra \"" +  AppWidgetManager.EXTRA_APPWIDGET_ID + "\" = " + appWidgetId);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, appWidgetId, intent , PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.phca_appwidget_layout, pendingIntent);
        }
        if(paramString.equals(ACTION_CLICK))
        {
            Log.d(MY_DEBUG, "Provider.buildUpdate().. CLICKED");
            Toast.makeText(ctx, "ACTION_CLICK", Toast.LENGTH_LONG).show();
        }
        return views; 
    }
 }

我有一个配置活动(一个ListActivity)这是我的onListItemClick:

I do have a configuration activity (a ListActivity) Here's my onListItemClick:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Toast.makeText(getApplicationContext(), "onListItemClick", Toast.LENGTH_SHORT);
    Log.d(MY_DEBUG, "Configurator.onListItemClick");
    SharedPreferences.Editor prefs = getSharedPreferences(PREFS_NAME, 0).edit();
    prefs.putInt(PREF_PREFIX_KEY + mAppWidgetId, position);
    prefs.commit();
    Log.d(MY_DEBUG, "Configurator.Prefs.commit()");

    // Push widget update to surface with newly set prefix
    Log.d(MY_DEBUG, "Configurator.calling updateWidgetState with appWidgetId = " + mAppWidgetId);
    PhcaAppWidgetProvider.updateWidgetState(getApplicationContext(), "android.appwidget.action.APPWIDGET_UPDATE", mAppWidgetId);
    Log.d(MY_DEBUG, "Configurator.updateWidgetState complete");
    Intent resultValue = new Intent();
    Uri data = Uri.withAppendedPath(
            Uri.parse("ABCD" + "://widget/id/")
            ,String.valueOf(mAppWidgetId));
    resultValue.setData(data);
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    Log.d(MY_DEBUG, "Configurator.Creating widget with id = " + mAppWidgetId);
    setResult(RESULT_OK, resultValue);
    finish();
}

我一直在这方面的工作了两天,我也希望有人能指出我的方式犯错。虚拟电子啤酒等待的人谁可以解决这个问题:)

I've been working on this for two days and I do hope that someone can point out the err of my ways. A virtual e-beer awaits the person who can solve this :)

推荐答案

我想了解一下您code,但没有找到为什么它不为你工作,除非我发现在$ C $一些其他错误C。无论如何,我已经建立了一个快速的工作code,你可以尝试一下。

I tried to look over your code but did not find why it doesn't work for you except I found some other bugs in your code. Anyway I have built a quick working code that you can try out.

WidgetProvider.java

package com.example.helloclickablewidget;

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

public class WidgetProvider extends AppWidgetProvider {

    public static final String WIDGET_CLICKED = "com.example.helloclickablewidget.WIDGET_CLICKED";

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action != null && action.equals(WIDGET_CLICKED)) {
            int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
            Toast.makeText(context, "Widget clicked. ID: " + widgetId, Toast.LENGTH_LONG).show();
        } else {
            super.onReceive(context, intent);
        }
    }

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

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, WidgetProvider.class);
            intent.setAction(WIDGET_CLICKED);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.text, pendingIntent);
            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

}

布局/ widget.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/app_name" >
</TextView>

XML / widget.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget"
    android:minHeight="50dp"
    android:minWidth="50dp"
    android:updatePeriodMillis="86400000" >
</appwidget-provider>

AndroidManifest.xml中

<manifest package="com.example.helloclickablewidget"
    android:versionCode="1"
    android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="WidgetProvider" android:exported="false">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.example.helloclickablewidget.WIDGET_CLICKED"/>
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget" />
        </receiver>
    </application>

</manifest>

这篇关于Android的appWidget多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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