错误的项目传递给活动部件从 [英] Wrong item being passed to activity from widget

查看:127
本文介绍了错误的项目传递给活动部件从的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用集合的部件。我需要的是当小部件的行之一被选中的ListView 来传递一个id,从窗口小部件,给活动在我的应用程序。

由于某些原因,当我调试活动,错误的ID被发送。我已经把范围缩小到一些与位置。如果我通过了位置,我的活动,当我调试,这是错误的位置这可能只是我的一个集合的部件是如何工作的误解,因为这是我的第一个。

的Widget服务

 公共类WidgetService扩展RemoteViewsService {
    @覆盖
    公共RemoteViewsFactory onGetViewFactory(意向意图){
        返回新StackRemoteViewsFactory(this.getApplicationContext(),意向);
    }
}类StackRemoteViewsFactory实现RemoteViewsService.RemoteViewsFactory {        私人列表<整数GT; MIDS =新的ArrayList<整数GT;();
        私人语境mContext;
        私人诠释mAppWidgetId;        公共StackRemoteViewsFactory(上下文的背景下,意图意图){
            mContext =背景;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
        }        公共无效的onCreate(){
        }        公众诠释的getCount(){
            // TODO自动生成方法存根
            返回mIds.size();
        }        众长getItemId(INT位置){
            // TODO自动生成方法存根
            返回的位置;
        }        公共RemoteViews getLoadingView(){
            // TODO自动生成方法存根
            返回null;
        }        公共RemoteViews getViewAt(INT位置){
            RemoteViews RV =新的RemoteViews(mContext.getPackageName(),R.layout.widget_item);
            最终意图MI =新意图(mContext,MyActivity.class);
            最终捆绑馍=新包();
            bun.putInt(ID,mIds.get(位置));
            mi.putExtras(BUN);            最终的PendingIntent piMain = PendingIntent.getActivity(mContext,mAppWidgetId,MI,PendingIntent.FLAG_UPDATE_CURRENT);            rv.setOnClickPendingIntent(R.id.widget_text,piMain);
            rv.setTextViewText(R.id.widget_text,mIds.get(位置));            返回rv;
        }        公众诠释getViewTypeCount(){
            // TODO自动生成方法存根
            返回1;
        }        公共布尔hasStableIds(){
            // TODO自动生成方法存根
            返回true;
        }        公共无效onDataSetChanged(){
            MIDS = Utilities.GetIds();
        }        公共无效的onDestroy(){
            // TODO自动生成方法存根        }
}

我的活动

 公共类活动扩展SherlockFragmentActivity {    @覆盖
    公共无效的onCreate(最终捆绑冰柱)
    {
        super.onCreate(冰柱);        的setContentView(R.layout.browser_pager);        如果(getIntent()。getExtras()!= NULL)
        {
            最终捆绑额外= getIntent()getExtras()。            INT ID = extras.getInt(ID); //这个ID是错误的
        }
    }
}


解决方案

 最后的PendingIntent piMain = PendingIntent.getActivity(mContext,mAppWidgetId,MI,PendingIntent.FLAG_UPDATE_CURRENT);


getActivity()不使用 mAppWidgetId 作为请求code:部件ID是不变一个小窗口的一个实例。因此该平台无法识别新的意图的,并会提供相同的的PendingIntent 所有的时间。从报价文档


  

...重要的是要知道,当两个目的被认为是用于检索的PendingIntent的目的是相同的。一个常见的​​错误的人提出的是,创建一个意图,只有在他们的额外的内容各不相同的多个对象的PendingIntent,期望每次获得不同的PendingIntent。这不会发生。了用于匹配的意图的部分是由<定义的相同href=\"http://developer.android.com/reference/android/content/Intent.html#filterEquals%28android.content.Intent%29\"相对=nofollow> Intent.filterEquals 。如果您使用的是等效按照Intent.filterEquals 2意向对象,那么你会得到相同的PendingIntent对他们俩的。


  
  

有两种典型的方式来处理这个问题。


  
  

如果你真正需要多个不同的PendingIntent在同一时间对象有效(如为,它们都在同一时间显示的两个通知使用),那么你将需要确保有一些东西是不同的关于他们的关联他们用不同的PendingIntents。这可以是任何由Intent.filterEquals,或的不同要求code整数提供给<一个考虑的意向属性href=\"http://developer.android.com/reference/android/app/PendingIntent.html#getActivity%28android.content.Context,%20int,%20android.content.Intent,%20int%29\"相对=nofollow> getActivity(背景下,INT,意向,INT),<一个href=\"http://developer.android.com/reference/android/app/PendingIntent.html#getActivities%28android.content.Context,%20int,%20android.content.Intent%5B%5D,%20int%29\"相对=nofollow> getActivities(背景下,INT,意图[],INT),<一个href=\"http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast%28android.content.Context,%20int,%20android.content.Intent,%20int%29\"相对=nofollow> getBroadcast(背景下,INT,意向,INT),或<一个href=\"http://developer.android.com/reference/android/app/PendingIntent.html#getService%28android.content.Context,%20int,%20android.content.Intent,%20int%29\"相对=nofollow>的getService(背景下,INT,意向,INT)。


所以,相反,使用一个ID这对每一行唯一的:位置是一个不错的人选。

I have a widget that is using a collection. What I need is to pass an id, from the widget, to an activity in my app when one of the rows of the widget ListView is selected.

For some reason, when I debug activity, the wrong id is being sent. I've narrowed it down to something with the position. If I pass the position to my activity when I debug, it is the wrong position It may just be my misunderstanding of how a widget with a collection works, since it's my first one.

Widget Service

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory  {

        private List<Integer> mIds = new ArrayList<Integer>();
        private Context mContext;
        private int mAppWidgetId;

        public StackRemoteViewsFactory(Context context, Intent intent) {
            mContext = context;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        public void onCreate() {
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return mIds.size();
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public RemoteViews getLoadingView() {
            // TODO Auto-generated method stub
            return null;
        }

        public RemoteViews getViewAt(int position) {
            RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
            final Intent mi = new Intent(mContext, MyActivity.class);
            final Bundle bun = new Bundle();
            bun.putInt("id", mIds.get(position));
            mi.putExtras(bun);

            final PendingIntent piMain = PendingIntent.getActivity(mContext, mAppWidgetId, mi, PendingIntent.FLAG_UPDATE_CURRENT);

            rv.setOnClickPendingIntent(R.id.widget_text, piMain);
            rv.setTextViewText(R.id.widget_text, mIds.get(position));

            return rv;
        }

        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 1;
        }

        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        public void onDataSetChanged() {
            mIds = Utilities.GetIds();
        }

        public void onDestroy() {
            // TODO Auto-generated method stub

        }
}

My Activity

public class Activity extends SherlockFragmentActivity {

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.browser_pager);

        if (getIntent().getExtras() != null)
        {
            final Bundle extras = getIntent().getExtras();

            int id = extras.getInt("id"); //this id is wrong
        }
    }
}

解决方案

final PendingIntent piMain = PendingIntent.getActivity(mContext, mAppWidgetId, mi, PendingIntent.FLAG_UPDATE_CURRENT);

At getActivity() do not use mAppWidgetId as a request code: widget ID is constant for an instance of a widget. As a result the platform fails to recognize new Intent as a "new" and will deliver the same PendingIntent all the time. Quoting from docs:

... it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

There are two typical ways to deal with this.

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

So, instead, use an ID that's unique for each row: position is a good candidate.

这篇关于错误的项目传递给活动部件从的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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