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");
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.
uaraven
source share