Android 如何在后台服务中使用oAuth获取的凭据?
我遵循这个()在我的Android应用程序中使用Google日历API 它可以工作,但是我需要能够在后台服务中执行同样的操作Android 如何在后台服务中使用oAuth获取的凭据?,android,oauth-2.0,google-calendar-api,google-oauth,Android,Oauth 2.0,Google Calendar Api,Google Oauth,我遵循这个()在我的Android应用程序中使用Google日历API 它可以工作,但是我需要能够在后台服务中执行同样的操作 com.google.api.services.calendar.Calendar mService = new com.google.api.services.calendar.Calendar.Builder( transport, jsonFactory, credential) .setApplica
com.google.api.services.calendar.Calendar mService = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Google Calendar API Android Quickstart")
.build();
帮助我实例化日历对象
我实例化credential如下:
credential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
如何在后台服务中同时使用它们
我使用IntentService(WakefulIntentService)作为后台服务
com.google.api.services.calendar.Calendar mService = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Google Calendar API Android Quickstart")
.build();
意向服务由如下报警触发:
public class Receiver extends BroadcastReceiver {
private static final int PERIOD = 5000;
private static final int INITIAL_DELAY = 5000;
@Override
public void onReceive(Context ctxt, Intent i) {
if (i.getAction() == null) {
WakefulIntentService.sendWakefulWork(ctxt, ScheduledService.class);
} else {
scheduleAlarms(ctxt);
}
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr =
(AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctxt, Receiver.class);
PendingIntent pi = PendingIntent.getBroadcast(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + INITIAL_DELAY,
PERIOD, pi);
}
}
我的主要活动是调用Receiver.ScheduleArms