1

我正在尝试使用 CommonsWare 示例实现 WakefulIntentService 。我需要将 Location 传递给 WakefulIntentService 的子类,但我无法做到这一点。

我使用以下方法调用 sendWakefulWork:

Intent i = new Intent(context, WakefulIntentService.class);
i.putExtra(Constants.LOCATION, location);
WakefulIntentService.sendWakefulWork(context, i);

在 WakefulIntentService 类上,我有以下方法:

public static void sendWakefulWork(Context context, Intent i) {
    getLock(context).acquire();
    i.setClass(context, ProtocolController.class);
    context.startService(i);
}

最后,ProtocolController 类:

public class ProtocolController extends WakefulIntentService {

    public ProtocolController() {
        super(ProtocolController.class.getSimpleName());
        Log.d(Constants.TAG, "starting Protocol");
    }

    @Override
    public void onCreate(){
        super.onCreate();
    }

    @Override
    protected void doWakefulWork(Intent intent) {
        Log.d(Constants.TAG, "Doing wakeful work");
    }
}
4

1 回答 1

0

我认为您不能以您尝试的方式传递 Location 对象。我会使用 putExtra 传递您需要的各个值,例如纬度和经度、时间戳等...使用 putExtra 传递您的基本值,例如字符串、浮点数、整数、布尔值等...

于 2011-06-20T21:39:46.017 回答