Android的小部件显示更新过程中奇怪的图像 [英] Android Widget shows strange image during update

查看:86
本文介绍了Android的小部件显示更新过程中奇怪的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的人在这里...... 我有一个摩托罗拉的i1与Nextel的运行Android 1.5

我有一个Android窗口小部件。 当我开始运行更新该控件我看到的图像从另一个插件的服务(启动DC接触)

这表明这个形象在我的窗口小部件的位置,但是当我的窗口小部件的更新完成它被替换的布局我有我的小工具。

这是因为如果有部件图像的缓存,它显示的东西在那里,直到我的窗口小部件的更新完成。

如果我的更新需要很长的话,用户可以看到这个奇怪的图像2-3秒。

 公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
            INT [] appWidgetIds){
        Log.d(TAG,输入的onupdate);
        最终诠释N = appWidgetIds.length;

        的for(int i = 0; I&n种;我++){
            INT为widgetid = appWidgetIds [I]
            RemoteViews意见=新RemoteViews(context.getPackageName()
                    R.layout.widget_initial_layout);
            意向意图=新的意图(背景下,MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(上下文,0,
                    意图,0);
            views.setOnClickPendingIntent(R.id.WidgetLayout,pendingIntent);
            views.setTextViewText(R.id.NEWCountTextView,Integer.toString(NEWC));
            views.setTextViewText(R.id.ACKCountTextView,Integer.toString(ACKC));
            views.setTextViewText(R.id.PKUCountTextView,Integer.toString(PKUC));
            views.setTextViewText(R.id.PODCountTextView,Integer.toString(PODC));
            如果(errorMessage.contentEquals()){
                的errorMessage =(新日期())的toString()。
                views.setTextColor(R.id.LastUpdatedTextView,Color.BLACK);
            } 其他 {
                views.setTextColor(R.id.LastUpdatedTextView,Color.RED);
            }
            views.setTextViewText(R.id.LastUpdatedTextView,的errorMessage);
            appWidgetManager.updateAppWidget(为widgetid,意见);
        }
        Log.d(TAG,退出的onupdate);
    }

公共无效的onReceive(上下文的背景下,意图意图){
    Log.d(TAG,输入的onReceive);
    串动= intent.getAction();
    如果(action.contentEquals(android.appwidget.action.APPWIDGET_UPDATE)){
        捆绑额外= intent.getExtras();
        如果(临时演员!= NULL){
            NEWC = extras.getInt(NEWCOUNT);
            ACKC = extras.getInt(ACKCOUNT);
            PKUC = extras.getInt(PKUCOUNT);
            PODC = extras.getInt(PODCOUNT);
            的errorMessage = extras.getString(errormessage的);
        }
    }
 

super.onReceive(背景下,意图);         Log.d(TAG,退出的onReceive); } 这里是布局

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / WidgetLayout
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT
    机器人:重力=顶
    机器人:方向=垂直
    机器人:背景=@可绘制/ BG
    >

    <的TextView
        机器人:文本=订单
        机器人:ID =@ + ID / TextView01
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =25dip
        机器人:重力=顶|中心
        机器人:文字颜色=#FFFF00
        机器人:TEXTSTYLE =黑体
        />

    <的TextView
        机器人:文本=
        机器人:ID =@ + ID / TextView01
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =6dip
        机器人:重力=顶|中心
        机器人:后台=#0000FF
        />

    <的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / LinearLayout02
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:重力=顶|中心
        机器人:方向=横向
        >
        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / CountsLinearLayout
            机器人:layout_height =FILL_PARENT
            机器人:layout_width =WRAP_CONTENT
            机器人:重力=顶
            机器人:方向=垂直
            >
            <的TextView
                机器人:文本=NEW
                机器人:ID =@ + ID / TextView11
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体

                />
            <的TextView
                机器人:文本=
                机器人:ID =@ + ID / NEWCountTextView
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体
                />
        < / LinearLayout中>

        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / LinearLayout04
            机器人:layout_height =FILL_PARENT
            机器人:layout_width =WRAP_CONTENT
            机器人:重力=顶
            机器人:方向=垂直
            >
            <的TextView
                机器人:文本=ACK
                机器人:ID =@ + ID / TextView21
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体

                />
            <的TextView
                机器人:文本=
                机器人:ID =@ + ID / ACKCountTextView
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体
                />
        < / LinearLayout中>

        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / LinearLayout05
            机器人:layout_height =FILL_PARENT
            机器人:layout_width =WRAP_CONTENT
            机器人:重力=顶
            机器人:方向=垂直
            >
            <的TextView
                机器人:文本=北大
                机器人:ID =@ + ID / TextView31
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体
                />
            <的TextView
                机器人:文本=
                机器人:ID =@ + ID / PKUCountTextView
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体
                />
        < / LinearLayout中>

        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / LinearLayout06
            机器人:layout_height =FILL_PARENT
            机器人:layout_width =WRAP_CONTENT
            机器人:重力=顶
            机器人:方向=垂直
            >
            <的TextView
                机器人:文本=POD
                机器人:ID =@ + ID / TextView41
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体
                />
            <的TextView
                机器人:文本=
                机器人:ID =@ + ID / PODCountTextView
                机器人:layout_width =75dip
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =25dip
                机器人:重力=中心
                机器人:文字颜色=#000000
                机器人:TEXTSTYLE =黑体
                />
        < / LinearLayout中>


    < / LinearLayout中>

    <的TextView
        机器人:文本=
        机器人:ID =@ + ID / TextView01
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =6dip
        机器人:重力=底部|中心
        机器人:后台=#0000FF
        />

    <的TextView
        机器人:文本=
        机器人:ID =@ + ID / LastUpdatedTextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =15dip
        机器人:重力=中心
        机器人:文字颜色=#000000
        机器人:TEXTSTYLE =黑体
        />
< / LinearLayout中>
 

你见过这样的事?

感谢

解决方案

我像你描述并搜查了很多的解决方案,但没有找到任何在我的窗口小部件有相同的问题

。我最后做的是一些解决办法,这似乎在我的情况下正常工作。我所做的是下面的,而不是直接从的OnUpdate()方法更新窗口小部件,我开始处理该更新,然后杀死自己的服务。这解决了它在模拟器和设备有更新时粘在小部件上。 下面是一个示例code:

在AppWidgetProvider:

  @覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
    context.startService(新意图(背景下,WidgetService.class));
}
 

该服务:

  @覆盖
公共无效ONSTART(意向意图,诠释startId){
    启动(意向);
}

@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
    启动(意向);
    返回START_STICKY;
}


私人无效启动(意向意图){
    //这里更新部件
    上下文的背景下= getApplicationContext();
    updateWidgets(上下文);
    stopSelf(); //查杀服务
}

私人无效updateWidgets(上下文的背景下){
    AppWidgetManager AppManager的= AppWidgetManager.getInstance(上下文);
    组件名cmpName =新的组件名(背景下,widgetClass);
    INT [] widgetIds = appmanager.getAppWidgetIds(cmpName);
    RemoteViews rView =新RemoteViews(context.getPackageName(),layoutId);
    对于(INT WID:widgetIds){
        //都在这里更新
        rView.setTextViewText(TVID,DESC);
        appmanager.updateAppWidget(WID,rView);
    }
}
 

请注意确定为什么这种解决方法解决了这个问题,但好处是,它确实 希望这有助于

I have a strange one here... I have a Motorola i1 with Nextel running android 1.5

I have an android widget. When I start running the Service that updates this Widget I see an image from another widget (Launch DC Contact)

It shows this image in my widget's location but when "update" of my widget is done it is replaced with the layout I have for my widget.

It is as if there is a cache of widget images and it shows stuff from there until my widget's update is done.

If my update takes long then user can see this strange image for 2-3 secs.

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        Log.d(TAG, "Enter onUpdate");
        final int N = appWidgetIds.length;

        for (int i = 0; i < N; i++) {
            int widgetId = appWidgetIds[i];
            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.widget_initial_layout);
            Intent intent = new Intent(context, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, 0);
            views.setOnClickPendingIntent(R.id.WidgetLayout, pendingIntent);
            views.setTextViewText(R.id.NEWCountTextView, Integer.toString(NEWC));
            views.setTextViewText(R.id.ACKCountTextView, Integer.toString(ACKC));
            views.setTextViewText(R.id.PKUCountTextView, Integer.toString(PKUC));
            views.setTextViewText(R.id.PODCountTextView, Integer.toString(PODC));
            if (errorMessage.contentEquals("")) {
                errorMessage = (new Date()).toString();
                views.setTextColor(R.id.LastUpdatedTextView, Color.BLACK);
            } else {
                views.setTextColor(R.id.LastUpdatedTextView, Color.RED);
            }
            views.setTextViewText(R.id.LastUpdatedTextView, errorMessage);
            appWidgetManager.updateAppWidget(widgetId, views);
        }
        Log.d(TAG, "Exit onUpdate");
    }

public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Enter onReceive");
    String action = intent.getAction();
    if (action.contentEquals("android.appwidget.action.APPWIDGET_UPDATE")) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            NEWC = extras.getInt("NEWCOUNT");
            ACKC = extras.getInt("ACKCOUNT");
            PKUC = extras.getInt("PKUCOUNT");
            PODC = extras.getInt("PODCOUNT");
            errorMessage = extras.getString("ERRORMESSAGE");
        }
    }

super.onReceive(context, intent); Log.d(TAG, "Exit onReceive"); } here is the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/WidgetLayout" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:gravity="top"
    android:orientation="vertical"
    android:background="@drawable/bg"
    >

    <TextView 
        android:text="ORDERS" 
        android:id="@+id/TextView01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textSize="25dip"
        android:gravity="top|center"
        android:textColor="#ffff00"
        android:textStyle="bold"
        />

    <TextView 
        android:text="" 
        android:id="@+id/TextView01" 
        android:layout_width="fill_parent" 
        android:layout_height="6dip"
        android:gravity="top|center"
        android:background="#0000ff"
        />

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout02" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:gravity="top|center"
        android:orientation="horizontal"        
        >
        <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/CountsLinearLayout" 
            android:layout_height="fill_parent" 
            android:layout_width="wrap_content"
            android:gravity="top"
            android:orientation="vertical"
            >
            <TextView 
                android:text="NEW" 
                android:id="@+id/TextView11" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"

                />
            <TextView 
                android:text="" 
                android:id="@+id/NEWCountTextView" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"
                />
        </LinearLayout>

        <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/LinearLayout04" 
            android:layout_height="fill_parent" 
            android:layout_width="wrap_content"
            android:gravity="top"
            android:orientation="vertical"
            >
            <TextView 
                android:text="ACK" 
                android:id="@+id/TextView21" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"

                />
            <TextView 
                android:text="" 
                android:id="@+id/ACKCountTextView" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"
                />
        </LinearLayout>

        <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/LinearLayout05" 
            android:layout_height="fill_parent" 
            android:layout_width="wrap_content"
            android:gravity="top"
            android:orientation="vertical"
            >
            <TextView 
                android:text="PKU" 
                android:id="@+id/TextView31" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"
                />
            <TextView 
                android:text="" 
                android:id="@+id/PKUCountTextView" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"
                />
        </LinearLayout>

        <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/LinearLayout06" 
            android:layout_height="fill_parent" 
            android:layout_width="wrap_content"
            android:gravity="top"
            android:orientation="vertical"
            >
            <TextView 
                android:text="POD" 
                android:id="@+id/TextView41" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"
                />
            <TextView 
                android:text="" 
                android:id="@+id/PODCountTextView" 
                android:layout_width="75dip" 
                android:layout_height="wrap_content"
                android:textSize="25dip"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"
                />
        </LinearLayout>


    </LinearLayout>

    <TextView 
        android:text="" 
        android:id="@+id/TextView01" 
        android:layout_width="fill_parent" 
        android:layout_height="6dip"
        android:gravity="bottom|center"
        android:background="#0000ff"
        />

    <TextView 
        android:text="" 
        android:id="@+id/LastUpdatedTextView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textSize="15dip"
        android:gravity="center"
        android:textColor="#000000"
        android:textStyle="bold"
        />                  
</LinearLayout>

Have you seen anything like this?

thanks

解决方案

I had the exact same issue in my widget as you described and searched a lot for a solution and couldn't find any. What I ended up doing is some kind of workaround that seems to work fine in my case. What i did is the following, instead of updating the widgets directly from the onUpdate() method, I started a service that handled the update then killed itself. This solved it on the emulator and on a device that had the widget stuck during update. Here's a sample code:

The AppWidgetProvider:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    context.startService(new Intent(context, WidgetService.class));
}

The Service:

@Override
public void onStart(Intent intent, int startId) {
    started(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    started(intent);
    return START_STICKY;
}


private void started(Intent intent) {
    //update here the widgets
    Context context = getApplicationContext();
    updateWidgets(context);
    stopSelf();//killing the service
}

private void updateWidgets(Context context) {
    AppWidgetManager appmanager = AppWidgetManager.getInstance(context);
    ComponentName cmpName = new ComponentName(context, widgetClass);
    int[] widgetIds = appmanager.getAppWidgetIds(cmpName);
    RemoteViews rView = new RemoteViews(context.getPackageName(), layoutId);
    for (int wid : widgetIds) {
        //all updates here
        rView.setTextViewText(tvId, desc);
        appmanager.updateAppWidget(wid, rView);
    }
}

Note sure why this workaround solves the issue, but the good thing is that it does Hope this helps

这篇关于Android的小部件显示更新过程中奇怪的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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