窗口小部件不更新活动 [英] Widget not updating activity

查看:101
本文介绍了窗口小部件不更新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行与集合的小部件。我有部件工作正常,但是,当用户点击集合中的一个项目,我想开我的应用程序的通行证和身份证,从部件到 活动,其中,然后,将用户重定向到另一个活动。我这样做的原因是,我需要先刷新活动(我显示未读邮件数)。

我有我第一次在小部件集合单击一个项目工作。不过,如果我preSS home键并单击另一个项目,我的应用程序只显示从浏览器中的最后一个屏幕活动(第二个活动用户将被引导到)。如果我退出程序出来,或单击后退,然后去窗口小部件,它按预期工作。此问题只发生在我最小化的应用程序,而其余2号活动

我不知道是否有事可做与 PendingIntent 在窗件服务。

的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,Main.class);

        最后包包子=新包();
        bun.putInt(ID,mIds.get(位置));
        mi.putExtras(BUN);

        最后PendingIntent piMain = PendingIntent.getActivity(mContext,位置,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(); //返回的ID从SQLite数据库
    }

    公共无效的onDestroy(){
        // TODO自动生成方法存根

    }
}
 

Main.class

 公共类主要扩展SherlockFragmentActivity
{
    @覆盖
    公共无效的onCreate(最终捆绑冰柱)
    {
        super.onCreate(冰柱);

        的setContentView(R.layout.main);
    }

    公共无效onNewIntent(意向意图){

        最终捆绑额外= intent.getExtras();

        最终意图的浏览器=新的意图(这一点,Browser.class);

        最后包包子=新包();
        bun.putInt(ID,extras.getInt(ID));
        browser.putExtras(BUN);

        startActivity(浏览器);
    }
}
 

Browser.class

 公共类BrowserPager扩展SherlockFragmentActivity
{
    私人诠释MID = 0;

    @覆盖
    公共无效的onCreate(最终捆绑冰柱)
    {
        super.onCreate(冰柱);

        的setContentView(R.layout.browser_pager);

        最终捆绑额外= getIntent()getExtras()。

        如果(临时演员!= NULL)
        {
            MID = extras.getInt(ID);
        }
        ...
    }
}
 

解决方案

请重读有关Widget收藏的部分: http://developer.android.com/guide/topics/appwidgets/index html的#集合

<一个href="http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent(int" rel="nofollow">http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent(int, android.app.PendingIntent)规定:

  

在设置集合中的项目上单击操作(如:   ListView控件,StackView等),这种方法是行不通的。相反,使用   {@link RemoteViews#setPendingIntentTemplate(INT,PendingIntent)在   与RemoteViews#setOnClickFillInIntent(INT,意图)。

结合

要使用一个小部件集合中创建一个Intent打电话给你的活动。这种意图被添加到被添加到使用setPendingIntentTemplate您的收藏视图(列表,堆栈,网格),一个PendingIntent。这一切都发生在你的AppWidgetProvider类的OnUpdate方法。 要添加个人行为到一个单一的项目将创建一个包含有关单个项目额外的另一个意图并使用setOnClickFillInIntent添加到RemoteViews在getViewAt。接收活动可以读取额外的参数通常的方式

全PendingIntent code进入AppWidgetProvider如像这样的:

 最终意图MI =新的意图(mContext,Main.class)
    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
最后PendingIntent piMain = PendingIntent.getActivity(mContext,位置,MI,PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.MyCollection,piMain);
 

您getViewAt code看起来更像是这样的:

 最终意图fillInIntent =新意图();
最后包包子=新包();
bun.putInt(ID,mIds.get(位置));
fillInIntent.putExtras(BUN);
rv.setOnClickFillInIntent(R.id.widget_text,fillInIntent);
 

I am implementing a widget with a collection. I have the widget working fine, however, when a user clicks on an item in the collection, I want to open my app an pass and id, from the widget, to the Main activity, which, then, redirects the user to another activity. The reason I do this is I need to refresh the Main activity first (I show the number of unread items).

What I have works the first time I click on an item in the widget collection. However, if I press the home button and click on another item, my app just displays the last screen from the Browser activity (the second activity the user is directed to). If I exit out of the app or click back, then go to the widget, it works as expected. This issue only occurs when I "minimize" the app, while remaining on the 2nd activity.

I'm not sure if it has something to do with the PendingIntent in the widget service.

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, Main.class);

        final Bundle bun = new Bundle();
        bun.putInt("id", mIds.get(position));
        mi.putExtras(bun);

        final PendingIntent piMain = PendingIntent.getActivity(mContext, position, 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(); //returns id's from a sqlite database
    }

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

    }
}

Main.class

public class Main extends SherlockFragmentActivity
{
    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);
    }

    public void onNewIntent(Intent intent){

        final Bundle extras = intent.getExtras();

        final Intent browser = new Intent(this, Browser.class);

        final Bundle bun = new Bundle();
        bun.putInt("id", extras.getInt("id"));
        browser.putExtras(bun);

        startActivity(browser);
    }
}

Browser.class

public class BrowserPager extends SherlockFragmentActivity
{
    private int mId = 0;

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

        setContentView(R.layout.browser_pager);

        final Bundle extras = getIntent().getExtras();

        if (extras != null)
        {
            mId = extras.getInt("id");
        }
        ...
    }
}

解决方案

Please reread the section about widget collections: http://developer.android.com/guide/topics/appwidgets/index.html#collections

http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent(int, android.app.PendingIntent) states:

When setting the on-click action of items within collections (eg. ListView, StackView etc.), this method will not work. Instead, use {@link RemoteViews#setPendingIntentTemplate(int, PendingIntent) in conjunction with RemoteViews#setOnClickFillInIntent(int, Intent).

To use a collection in a widget create an Intent to call your activity. That intent is added to a PendingIntent that is added to your collection view (list, stack, grid) using setPendingIntentTemplate. This all happens in the onUpdate method of your AppWidgetProvider class. To add individual behavior to a single item you create another Intent containing extras about that single item and add it to the RemoteViews in getViewAt using setOnClickFillInIntent. The receiving Activity can read the extra parameters the usual way.

The whole PendingIntent code goes into the AppWidgetProvider e.g. like this:

final Intent mi = new Intent(mContext, Main.class)
    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent piMain = PendingIntent.getActivity(mContext, position, mi, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.MyCollection, piMain);

Your getViewAt code would look more like this:

final Intent fillInIntent = new Intent();
final Bundle bun = new Bundle();
bun.putInt("id", mIds.get(position));
fillInIntent.putExtras(bun);
rv.setOnClickFillInIntent(R.id.widget_text, fillInIntent);

这篇关于窗口小部件不更新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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