开发者

Android appwidget service won't start

开发者 https://www.devze.com 2023-02-13 08:51 出处:网络
When I\'m running in debugging mode I can\'t seem to reach any breakpoints that are inside of the service, why is that?

When I'm running in debugging mode I can't seem to reach any breakpoints that are inside of the service, why is that?

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    context.startService(new Intent(context, UpdateService.class));
}

public static class UpdateService extends Service {

    @Override
    public void onStart(Intent intent, int startId) {
        // Build the widget update for today
        RemoteViews updateViews = buildUpdate(this);

        // Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this, WidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
    }

    public RemoteViews buildUpdate(Context context) {
        return new RemoteViews(context.g开发者_JAVA百科etPackageName(), R.id.widget_main_layout);
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}


The "onUpdate"-method is only executed if the widget is initalized (e.g. put on the homescreen) or the updatePeriodMillis are expired. If you want to execute the service e.g. by a click on the widget, you have to "attach" a pending intent like this:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final Intent intent = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

// Get the layout for the App Widget and attach an on-click listener to
// the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout....);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
for(int i=0,n=appWidgetIds.length;i<n;i++){
    int appWidgetId = appWidgetIds[i];
    appWidgetManager.updateAppWidget(appWidgetId , views);
}

(cleaned up version of a working widget).

The point is, that the onUpdate() method is really very seldom executed. The real interaction with a widget is specified through pending intents.


Your Service might not be registered in the manifest. Or your AppWidgetProvider might not be registered in the manifest.


You might want to think of not using a service for what you're doing. If it's just running the updateViews() once a day then consider just setting android:updatePeriodMillis to 86400000 in the XML file that's linked to your appwidget. Your XML file would look something like this:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="72dp"
  android:maxWidth="72dp"
  android:updatePeriodMillis="86400000" >
</appwidget-provider>

This will have android update your appwidget once a day without having a service run in the background that might get killed by a task killer that the user is running which then stops your widget from updating. Just a note, if you need it to update faster than every 30 minutes then android:updatePeriodMillis won't work (it's minimum value is 30 minutes) at that point I'd recommend using an AlarmManager since that'll use up less battery than a Service and also won't be killed by task killers.

0

精彩评论

暂无评论...
验证码 换一张
取 消