调试窗口小部件引起ANR [英] Debugging Widget causes ANR

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

问题描述

我试图调试AppWidget,但我发生了问题:D 如果不设置断点小部件的工作原理没有ANR和命令Log.v的完美执行。 然后,我把一个断点在方法的顶部:

I'm trying to debug an AppWidget but I incurred in a problem :D If not setting the breakpoint the widget works without ANR and the commands Log.v are executed flawlessly. Then I placed a breakpoint on the top of the method:

    public void onReceive(Context context, Intent intent) {
    Log.v(TAG, "onReceive 1"); // BP on this line
    super.onReceive(context, intent);
    String action = intent.getAction();

    // Checks on action and computations ...

    Log.v(TAG, "onReceive 2");
    updateWidget(context);
    Log.v(TAG, "onReceive 3");
}

断点停止执行的预期,但随后的过程中死亡。 问题是,该断点(我想的xD)引起ANR和ActivityManager终止进程。这是日志:

The breakpoint stops the execution as expected but then the process dies. The problem is that the breakpoint ( I guess xD ) cause an ANR and the ActivityManager kills the process. That's the Log:

01-07 14:32:38.886: ERROR/ActivityManager(72): ANR in com.salvo.wifiwidget
01-07 14:32:38.886: INFO/Process(72): Sending signal. PID: 475 SIG: 9
......
......
01-07 14:32:38.906: INFO/ActivityManager(72): Process com.salvo.wifiwidget (pid   475) has died.

这导致调试停止。 所以,问题是:有一种方法,而不会产生在ANR调试窗口小部件? 在此先感谢您的答案

This cause the debug to stop. So the question is: there's a way to debug the widget without incurring in the ANR?? thanks in advance for the answers

推荐答案

您说的对。据与Widget的的BroadcastReceiver将由系统,如果它需要被杀死时间超过10秒,以处理其的onReceive方法

You're right. According to this your Widget's BroadcastReceiver will be killed by the system if it takes longer than 10 seconds to process its onReceive method.

从我的头顶,我可以看到两种解决方案:

From the top of my head I can see two solutions:

  1. 从presentation和调试在活动你的逻辑独立的逻辑,然后将其合并成小部件。
  2. 度假村以老好的日志。

或者你可以尝试下面的事情。我不知道它是如何工作的,但也许这是值得给这个方法一试。

OR you can try following thing. I am not sure how it will work, but maybe it is worth to give this approach a try.

使用您自己的活动为主机部件的RemoteViews。我做了这样的事情:

Use your own activity as host for widget's RemoteViews. I've done something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
            android:id="@+id/widget_view"
            android:layout_width="fill_parent"
            android:layout_height="200dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Inflate widget"
        android:onClick="onInflateClick"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Update widget"
            android:onClick="onUpdateClick"/>
</LinearLayout>

在此布局的LinearLayout id为widget_view将发挥控件主机。活动$ C C本身$看起来是这样的:

In this layout LinearLayout with id = widget_view will play widget host. Activity code itself looks like this:

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RemoteViews;

public class MyActivity extends Activity {
    private LinearLayout widgetHolder;
    private View widgetView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        widgetHolder = (LinearLayout) findViewById(R.id.widget_view);
    }

    public void onInflateClick(View v) {
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
        views.setImageViewResource(R.id.image, R.drawable.img1);
        views.setTextViewText(R.id.text, "Widget created");
        widgetView = views.apply(this, null);
        widgetHolder.addView(widgetView);
    }

    public void onUpdateClick(View v) {
        onUpdateWidget(0);
    }

    public void onUpdateWidget(int widgetId) {
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
        views.setImageViewResource(R.id.image, R.drawable.img2);
        views.setTextViewText(R.id.text, "Widget updated");

        // Tell the AppWidgetManager to perform an update on the current app widget
        updateWidget(widgetId, views);
    }

    private void updateWidget(int widgetId, RemoteViews views) {
        views.reapply(this, widgetView);
        // appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

把你的小部件更新逻辑到updateWidget方法(或只是调用控件的的BroadcastReceiver与假参数的适当的onupdate法),你可以用一个按钮,您的活动点击调试。

Put your widget update logic into updateWidget method (or just call proper onUpdate method of the widget's BroadcastReceiver with fake parameters) and you can debug it with a click of a button on your activity.

此外,我从来没有尝试过的真实的小部件,我只是想出的主意,并试着写code吧。使用它在您自己的风险:)

Again, I never tried it on real widget, I just come up with idea and tried to write code for it. Use it on your own risk :)

我把我的快速和肮脏的项目 github上如果你想尝试一下。它是和Idea项目,但它应该很容易将其导入到Eclipse中。

I put my quick and dirty project to github in case you want to try it. It is and Idea project, but it should be easy to import it into Eclipse.

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

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