Android 安卓appwidget服务赢得';开始

Android 安卓appwidget服务赢得';开始,android,android-widget,android-appwidget,android-service,Android,Android Widget,Android Appwidget,Android Service,当我在调试模式下运行时,我似乎无法到达服务内部的任何断点,这是为什么 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { context.startService(new Intent(context, UpdateService.class)); } public static class UpdateS

当我在调试模式下运行时,我似乎无法到达服务内部的任何断点,这是为什么

    @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.getPackageName(), R.id.widget_main_layout);
    }


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

您的
服务
可能未在清单中注册。或者您的
AppWidgetProvider
可能未在清单中注册

您可能想不使用服务来完成您正在做的事情。如果只运行一次UpDeVIEWSW()一次,那么只需在链接到AppWIDWET的XML文件中设置安卓:UpDeaVixMyLILIS为86400000。您的XML文件如下所示:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="72dp"
  android:maxWidth="72dp"
  android:updatePeriodMillis="86400000" >
</appwidget-provider>
@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);
}

这将让android每天更新一次你的appwidget,而不会让服务在后台运行,这可能会被用户正在运行的任务杀手杀死,然后阻止你的widget更新。请注意,如果您需要它的更新速度超过每30分钟一次,那么android:updatePeriodMillis将无法工作(它的最小值为30分钟),此时我建议使用AlarmManager,因为它将比服务消耗更少的电池,也不会被任务杀手杀死。

onUpdate-方法仅在小部件初始化(例如,放在主屏幕上)或updatePeriodMillis过期时执行。如果您想执行服务,例如通过点击小部件,您必须“附加”一个待定的意图,如下所示:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="72dp"
  android:maxWidth="72dp"
  android:updatePeriodMillis="86400000" >
</appwidget-provider>
@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);
}
@覆盖
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
最终意图=新意图(上下文,UpdateService.class);
pendingent pendingent=pendingent.getService(上下文,0,意图,0);
//获取应用程序小部件的布局,并将单击侦听器附加到
//按钮
RemoteView视图=新的RemoteView(context.getPackageName(),R.layout….);
views.SetOnClickPendingEvent(R.id.按钮,PendingEvent);
对于(int i=0,n=appWidgetIds.length;i