2

我对 android 开发很陌生,我想为我的应用程序创建一个小部件。不幸的是,我无法让它工作。我希望我的小部件启动服务。该服务应包含所有逻辑,例如根据内部存储文件的数据读取更新文本字段。我必须每分钟更新一次我的小部件,因为我想显示剩余时间。为此,我想到了一个 AlarmManager。

我以此作为参考: http ://www.helloandroid.com/tutorials/mastering-android-widget-development-part4

这是我到目前为止所尝试的:AppWidgetProvider:

public class WidgetCtrl extends AppWidgetProvider {
public static String WIDGET_UPDATE = "WIDGET_UPDATE";
public static int UPDATE_RATE = 60 * 1000;
private SessionData sessionData = SessionData.getInstance();
private BaseCtrl baseCtrl = BaseCtrl.getInstance();

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

    // Service Intent
    Intent serviceIntent = new Intent(context, UpdateWidgetService.class);
    serviceIntent.setAction(UpdateWidgetService.UPDATE);
    PendingIntent servicePendingIntent = PendingIntent.getService(context,
            0, serviceIntent, 0);
    // send Service intent
    try {
        servicePendingIntent.send();
    } catch (CanceledException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // start alarm manager for all widget instances
    for (int appWidgetId : appWidgetIds) {
        setAlarm(context, appWidgetId, UPDATE_RATE);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onReceive(Context context, Intent intent) {

    if (WIDGET_UPDATE.equals(intent.getAction())) {
        Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
    }

    super.onReceive(context, intent);

}

@Override
public void onDisabled(Context context) {
    context.stopService(new Intent(context, UpdateWidgetService.class));
    super.onDisabled(context);
}


public static void setAlarm(Context context, int appWidgetId, int updateRate) {
    Intent serviceIntent = new Intent(context, UpdateWidgetService.class);
    serviceIntent.setAction(UpdateWidgetService.UPDATE);
    PendingIntent servicePendingIntent = PendingIntent.getService(context,
            0, serviceIntent, 0);

    AlarmManager alarms = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    if (updateRate >= 0) {
        alarms.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime(), updateRate, servicePendingIntent);
    } else {
        // on a negative updateRate stop the refreshing
        alarms.cancel(servicePendingIntent);
    }
}

}

服务:

public class UpdateWidgetService extends Service {
public static String UPDATE = "update";
private SessionData sessionData = SessionData.getInstance();
private BaseCtrl baseCtrl = BaseCtrl.getInstance();
private Context ctx;


@Override
public void onStart(Intent intent, int startId) {
    ctx = getApplicationContext();
    int appWidgetId = intent.getExtras().getInt(
            AppWidgetManager.EXTRA_APPWIDGET_ID);

    // TODO update information and display      

    AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(ctx);
    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    ComponentName currWidget = new ComponentName(ctx, WidgetCtrl.class);

    RemoteViews remoteViews = new RemoteViews(ctx.getPackageName(), R.layout.widget);
    remoteViews.setTextViewText(R.id.widget_text, "test");

    final Intent updateIntent = new Intent(ctx, UpdateWidgetService.class);
    final PendingIntent pending = PendingIntent.getService(ctx, 0, updateIntent, 0);
    final AlarmManager alarm = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pending);
    long interval = 60;
    alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);

    appWidgetManger.updateAppWidget(appWidgetId, remoteViews);
    super.onStart(intent, startId);
}


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

}

我遇到的问题是服务似乎永远不会被调用。我在服务的 onStart 方法中设置了断点,但我从来没有到达那里。有人可以帮忙吗?

如果您为我的程序建议不同的结构,也请告诉我。

编辑:我忘了添加清单代码。在应用程序标签之间,我在小部件的接收器对象后面有这段代码:

<service android:name=".UpdateWidgetService"></service>

这个对吗?

我真的希望有人能给我一个解决方案或提示。

4

2 回答 2

3

OK,终于找到问题了:

在清单中,我必须添加服务的完整路径。

所以我不得不将.UpdateWidgetService 更改为com.example.UpdateWidgetService。

我希望这可以帮助其他人节省几个小时的搜索时间;)

于 2012-12-12T14:14:32.820 回答
1

我忘了在清单中添加服务。经过几个小时处理我的 appwidget 代码。最后,我发现了我的小错误。以下清单添加对我在远程视图中列出数据有很大帮助。

    <service
        android:name="np.com.bpb.capstoneroomtest2.widget.JobWidgetRemoteViewsService"
        android:exported="false"
        android:permission="android.permission.BIND_REMOTEVIEWS" >
    </service>
</application><!--Just before closing this tag, add your service-->

希望,我的经验对你有帮助。

于 2018-10-19T20:20:40.577 回答