Android Widget 在更新期间显示奇怪的图像

发布于 2024-10-20 09:43:38 字数 8990 浏览 1 评论 0原文

我这里有一个奇怪的... 我有一台摩托罗拉 i1 和 Nextel,运行 android 1.5

我有一个 android 小部件。 当我开始运行更新此小部件的服务时,我看到来自另一个小部件的图像(启动 DC Contact)

它在我的小部件的位置中显示此图像,但是当我的小部件的“更新”完成时,它会被替换为我的布局小部件。

就好像有一个小部件图像的缓存,它显示从那里开始的内容,直到我的小部件更新完成。

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

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(上下文,意图); Log.d(TAG, "接收时退出"); } 这是布局

<?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>

你见过这样的东西吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2024-10-27 09:43:38

我的小部件中遇到了与您描述的完全相同的问题,并搜索了很多解决方案,但找不到任何解决方案。我最终所做的是某种在我的情况下似乎效果很好的解决方法。我所做的如下,我启动了一个处理更新的服务,然后自行终止,而不是直接从 onUpdate() 方法更新小部件。这在模拟器和更新期间小部件卡住的设备上解决了这个问题。
以下是示例代码:

AppWidgetProvider:

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

服务:

@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);
    }
}

请注意为什么此解决方法可以解决问题,但好处是它确实解决了问题
希望这有帮助

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

妖妓 2024-10-27 09:43:38

我在 4.0 和 4.0 上遇到了这个问题4.2 当手动发送android.appwidget.action.APPWIDGET_UPDATE广播来更新小部件时。
通过使用自定义操作修复,例如

<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
    <action android:name="my.app.action.APPWIDGET_REFRESH" />
</intent-filter>

I had this issue on 4.0 & 4.2 when manually sending android.appwidget.action.APPWIDGET_UPDATE broadcast to update the widget.
Fixed by using a custom action, e.g.

<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
    <action android:name="my.app.action.APPWIDGET_REFRESH" />
</intent-filter>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文