Java tutorial
//package com.java2s; /* * This file is part of GPSLogger for Android. * * GPSLogger for Android is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * GPSLogger for Android is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GPSLogger for Android. If not, see <http://www.gnu.org/licenses/>. */ import android.accounts.*; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; public class Main { /** * OAuth 2 scope to use */ //https://docs.google.com/feeds/ gives full access to the user's documents private static final String SCOPE = "oauth2:https://docs.google.com/feeds/"; /** * This version show the user an access request screen for the user to authorize the app * * @param accountManager * @param account * @param ota * @param activity */ public static void GetAuthTokenFromAccountManager(AccountManager accountManager, Account account, AccountManagerCallback<Bundle> ota, Activity activity) { Bundle bundle = new Bundle(); accountManager.getAuthToken(account, SCOPE, bundle, activity, ota, null); } /** * This version puts a message in the notification area asking for authorization * * @param accountManager * @param account * @param ota */ public static void GetAuthTokenFromAccountManager(AccountManager accountManager, Account account, AccountManagerCallback<Bundle> ota) { accountManager.getAuthToken(account, // Account retrieved using getAccountsByType() SCOPE, // Auth scope true, ota, // Callback called when a token is successfully acquired null); // Callback called if an error occurs } /** * Gets the stored authToken, which may be expired */ public static String GetAuthToken(Context applicationContext) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext); return prefs.getString("GDOCS_AUTH_TOKEN", ""); } }