Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用端点定期将userpositon更新到服务器_Android_Android Intent_Android Service_Google Cloud Endpoints - Fatal编程技术网

Android 使用端点定期将userpositon更新到服务器

Android 使用端点定期将userpositon更新到服务器,android,android-intent,android-service,google-cloud-endpoints,Android,Android Intent,Android Service,Google Cloud Endpoints,我想定期将我的用户位置更新到Google App Engine中的Google端点。 我使用端点这一事实很重要,因为这就是为什么我不能简单地使用IntentService和AlarmManger来安排任务的原因 问题如下: 要将位置更新到服务器,我需要一些在服务中无法访问的对象。 让我试着描绘一下: public class MyService extends IntentService { [...] @Override public int onStartComman

我想定期将我的用户位置更新到Google App Engine中的Google端点。
我使用端点这一事实很重要,因为这就是为什么我不能简单地使用
IntentService
AlarmManger
来安排任务的原因

问题如下:
要将位置更新到服务器,我需要一些在服务中无法访问的对象。
让我试着描绘一下:

public class MyService extends IntentService {
    [...]
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        endpointsHandler.updatePosition()
        return super.onStartCommand(intent, flags, startId);
    }
}
这里我需要我的
EndpointsHandler
的一个对象。我不能简单地创建一个新对象,因为它依赖于一些其他对象,如
GoogleAppClient

但是我无法将对象添加到服务器,因为当我初始化
AlarmManager
时,无法传递对象

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent updaterIntent = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, updaterIntent, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SOME_PERIOD, SOME_PERIOD, pendingIntent);
我可以按原意传递对象,但除了所有类(即使是
GoogleAppClient
)都必须实现
Parsable
,我认为这不是一个很好的方法。我确信有一条干净的路我就是找不到。

我自己解决了。
但由于我没有找到任何类似这样的解决方案,所以我将在这里发布:

我使用了描述的方式。 我创建了一个中央控制器类,它保存我应用程序的所有重要对象。此类是android.app.Application的子类 在我添加的清单文件中

<application
    [...]
    android:name=".controllers.AppController">

声明使用我的自定义应用程序类。

现在,在每个活动和服务中,我都可以使用
getApplication()
来获取我的应用程序类的对象。

序列化这些对象不是一个好主意,因为它们可能包含不可序列化的内容。为什么不创建一个新的GoogleAppClient?创建一个新的GoogleAppClient是可能的,但这是一个非常糟糕的方法,因为创建需要很长时间,并且需要很多资源才能在很长的时间内创建一个新对象。