Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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 如何在后台服务中使用oAuth获取的凭据?_Android_Oauth 2.0_Google Calendar Api_Google Oauth - Fatal编程技术网

Android 如何在后台服务中使用oAuth获取的凭据?

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

我遵循这个()在我的Android应用程序中使用Google日历API

它可以工作,但是我需要能够在后台服务中执行同样的操作

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