Long-term Android to update the application

I have a widget that should perform a potentially long-running operation in onUpdate (). just doing the operation directly led to ANR. To solve this problem, my first attempt was to create a thread in it. I noticed that in some cases the widget will not be updated. my guess is that as soon as onUpdate () comes out, android can kill the process along with the incomplete thread.

my next attempt was to create an intent service. the onUpdate () widget is just starting the intent service, which directly does the work and updates the widget when it is done. this works, but, to my great surprise, it seems that onHandleIntent () is single-threaded. if I have two widgets and then both updates and the launch of the intent service, they are updated sequentially ...

the two cases of the widget are not very important, but I'm just curious about the best practice for this type of template.

In order to solve two cases of the widget, I ended up updating all instances of the widget with the same data whenever any of them clicked. for example, I run a lengthy process once and apply the results to all instances of the widget. in my scenario, this does not matter, but for many widgets this may be important not to do.

thoughts?

+5
source share
2 answers

but, to my great surprise, it looks like onHandleIntent () is single-threaded

Yes.

If I have two widgets, and then both updates and the launch of the intent service, they are updated sequentially ...

Yes.

but I'm just curious about the best practice for this type of template.

Yours IntentServicewas a great decision, IMHO. Remember that Android runs on slow processors with devices with small RAM. Running a large number of threads in parallel is not a good idea.

then I start a topic in onHandleIntent (), which requires locking the trace, and it seems like this is getting too complicated.

Try to complete WakefulIntentService.

+3

onUpdate , . . : , , IntentService, , . , .

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    updateWidget(context, appWidgetManager, appWidgetIds);
}

private void updateWidget(Context context){
    ComponentName widget = new ComponentName(context, MyWidget.class);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widget);
    updateWidget(context, appWidgetManager, appWidgetIds);
}

private void updateWidget(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    final boolean isEnabled = true; //took out code didn't want you to see
    // start intent service here
    for(int i = 0; i< appWidgetIds.length; i++){
        int appWidgetId = appWidgetIds[i];
        Intent intent = new Intent(isEnabled ? ACTION_TOGGLE_OFF : ACTION_TOGGLE_ON);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.widget , pi);
        views.setImageViewResource(R.id.widget_image, isEnabled? R.drawable.widget_on : R.drawable.widget_off);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
0

All Articles