Android的主屏幕小部件变得无法响应 [英] Android Homescreen Widget becomes Unresponsive

查看:842
本文介绍了Android的主屏幕小部件变得无法响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android主屏幕小部件我的工作一切顺利,但后约5〜6天的四个按钮恢复到默认状态,变得反应迟钝。我不明白为什么这些按钮变得反应迟钝。如果删除并重新添加小部件再次工作。如果我等待约24-48小时,将开始再次一段时间工作。

I have an Android home screen widget I am working on and everything works great, except after about 5 to 6 days the four buttons revert to their default state and become unresponsive. I do not understand why the buttons are becoming unresponsive. If I remove and add the widget again it works again. If I wait for about 24-48 hours, it will begin to work again for a period of time.

我试图删除的android:updatePeriodMillis 这样的小部件不会对自身的更新(这是自包含的,只有真正需要的用户交互更新) 。这也不奏效。

I have tried to remove the android:updatePeriodMillis so that the widget does not update on its own (it is self-contained and only really needs updated upon user interaction). This has not worked either.

这是我怎么设置我的 PendingIntents 的onUpdate (答案<一个仿照href=\"http://stackoverflow.com/questions/2471875/processing-more-than-one-button-click-at-android-widget\">this问题):

This is how I am setting up my PendingIntents withinonUpdate (modeled after the answer to this question):

Intent intent1 = new Intent(context, MyWidgetProvider.class);
intent1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent1.setAction(FIRST_INTENT);
intent1.setData(Uri.parse(first_band_intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, widgetId, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.first_button, pendingIntent1);

我那么的onReceive 方法内更新widget的视图中的项目:

I then update the widget's view items within the onReceive method:

@Override
public void onReceive(Context context, Intent intent) {
    int widgetID = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
    String preferences = PREFS + Integer.toString(widgetID);
    SharedPreferences values = context.getSharedPreferences(preferences, 0);
    int firstBandValue = values.getInt("first_band_value", 1);
    int secondBandValue = values.getInt("second_band_value", 0);
    int multiplierValue = values.getInt("multiplier_value", 2);
    int toleranceValue = values.getInt("tolerance_value", 1);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    if (intent.getAction().equals(FIRST_BAND)) {
        // nextBandColor() will set the proper band based on the next value and return an integer that is BandValue + 1
        firstBandValue = getNextBandColor(context, BandType.FIRST, firstBandValue);

        SharedPreferences.Editor editor = values.edit();
        editor.putInt("first_band_value", firstBandValue);
        editor.commit();
    }
    else if (intent.getAction().equals(SECOND_BAND)) {
        // nextBandColor() will return an integer that is BandValue + 1
        secondBandValue = getNextBandColor(context, BandType.SECOND, secondBandValue);

        SharedPreferences.Editor editor = values.edit();
        editor.putInt("second_band_value", secondBandValue);
        editor.commit();
    }
    else if (intent.getAction().equals(MULTIPLIER)) {
        // nextBandColor() will return an integer that is BandValue + 1
        multiplierValue = getNextBandColor(context, BandType.MULTIPLIER, multiplierValue);

        SharedPreferences.Editor editor = values.edit();
        editor.putInt("multiplier_value", multiplierValue);
        editor.commit();
    }
    else if (intent.getAction().equals(TOLERANCE)) {
        // nextBandColor() will return an integer that is BandValue + 1
        toleranceValue = getNextBandColor(context, BandType.TOLERANCE, toleranceValue);

        SharedPreferences.Editor editor = values.edit();
        editor.putInt("tolerance_value", toleranceValue);
        editor.commit();
    }
    else {
        super.onReceive(context, intent);
    }

    //Update the view items based on the new integer values; calls updateAppWidget()
    updateBands(context, widgetID, firstBandValue, secondBandValue, multiplierValue, toleranceValue);

    // Concatenate the integer value of the first band value with the integer value of the second band color
    String value = Integer.toString(firstBandValue) + Integer.toString(secondBandValue);

    // Turn the concatenation of the two integers into a double value
    baseValue = Double.parseDouble(value);

    multiplyBaseValue(multiplierValue);

    String units = getUnitsAndAdjustBaseValue();
    String tolerance = getTolerance(toleranceValue);

    DecimalFormat df = new DecimalFormat("#.#");
    resistance = df.format(baseValue) + units + tolerance;

    remoteViews.setTextViewText(R.id.resistance_value, resistance);


    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(widgetID, remoteViews);
}

这个小部件被设计成独立的,没有任何活动或服务。

This widget is designed to be stand-alone, without any Activity or Service.

更新1:如果我添加一个收益语句的else我的的onReceive super.onReceive(),然后将它恢复到原来的布局,但不会停止​​响应。

UPDATE 1: If I add a return statement to the else of my onReceive after the super.onReceive(), then it reverts to the original layout, but does not become unresponsive.

更新2:我看了这里了在的onReceive()方法必须在5秒内完成处理。可能的问题是,我的的onReceive()未在规定时间内完成,从而导致小部件冻结?

UPDATE 2: I read here that the onReceive() method must complete processing within 5 seconds. Could the issue be that my onReceive() is not completing within the allotted time and thus causing the widget to freeze?

这个问题也是与设备无关。它发生在Galaxy Nexus的,的Nexus 7 2012年,和Galaxy S4。

This issue is also device independent. It has happened on a Galaxy Nexus, Nexus 7 2012, and Galaxy S4.

什么引起这种反应迟钝的问题?

What could be causing the issue of this unresponsiveness?

推荐答案

测试你的的onReceive() code首先,看看错误出在那里或其他地方 - 它始终是最好的检验假设寻找随机或怪异的错误时

Test your onReceive() code first to see if the error lies in there or elsewhere - it is always best to test assumptions when looking for "random" or "weird" bugs.

因此​​,为了测试它,简化它绝对,使其100%,微不足道。删除所有有趣code,并只让文字变成简单的东西。东西,可以没有可能有一个错误在里面。

So to test it, simplify it absolutely, to make it 100% trivial. Remove all the interesting code, and just make it change the text to something simple. Something that could no possibly have a bug in it.

@Override
public void onReceive(Context context, Intent intent) {
    int widgetID = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    remoteViews.setTextViewText(R.id.resistance_value, "1.2 ohms");

    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(widgetID, remoteViews);
}

如果这仍然打破了,那么你知道错误是不是你的的onReceive() code之内。所以,你可以再看看其他地方。

If this still breaks, then you know the bug is not within your onReceive() code. So you can then look elsewhere.

如果这能解决问题,那么你知道的那部分你的的onReceive()打破了小部件。

If this fixes the issue, then you know that part of your onReceive() has broken the widget.

点点添加code回位,加记录的手( Log.d())。

Add the code back bit by bit, add lots of logging (Log.d()).

您可能想看看你的共享preferences 的访问,因为这是文件访问。也看看这个帖子,并考虑使用共享preferences.Editor.apply()

You may want to look at your SharedPreferences access, since this is file access. Also have a look at this post, and consider using SharedPreferences.Editor.apply():

  • What's the difference between commit() and apply() in Shared Preference

通常使用这种方法,你就能找到bug,或者至少缩小它要真正具体的事情,这将使它更容易得到帮助这里。

Typically with this approach, you will be able to find the bug, or at least narrow it down to something really specific, which will make it easier to get help here.

这篇关于Android的主屏幕小部件变得无法响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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