Widget debugging causes ANR

I'm trying to debug an AppWidget, but I rushed into a problem: D If you do not set a breakpoint, the widget works without ANR, and the Log.v commands run flawlessly. Then I set a breakpoint at 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"); } 

The breakpoint stops execution as expected, but then the process dies. The problem is that the breakpoint (I think xD) calls ANR, and the ActivityManager kills the process. What is 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. 

This will stop debugging. So the question is, is there a way to debug a widget not related to ANR? in advance for replies

+5
source share
1 answer

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

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

  • Separate the logic from the view and debug your logic in the Activity, then include it in the widget.
  • To resort to the "old good" protocol.

OR you can try the following. I'm not sure how this will work, but it might be worth a try on this approach.

Use your own activity as a host for the RemoteViews widget. I did 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 the widget node. The operation code itself is as follows:

 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 the widget update logic in the updateWidget method (or just name the correct onUpdate method of the BroadcastReceiver widget with fake parameters), and you can debug it with the click of a button in your activity.

Again, I never tried this on real widgets, I just came up with an idea and tried to write code for it. Use it at your own risk :)

I set up my quick and dirty github project if you want to try it. This is an Idea project, but it is easy to import into Eclipse.

+2
source

All Articles