Debugging Widget causes ANR

你说的曾经没有我的故事 提交于 2019-12-05 11:02:57

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. Separate logic from presentation and debug your logic in Activity, then incorporate it into widget.
  2. Resort to "old good" logging.

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.

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>

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

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.

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 :)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!