Created
April 25, 2014 12:55
-
-
Save pi-chan/11288689 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends ActionBarActivity { | |
public Tasks service; | |
private static final int REQUEST_ACCOUNT_PICKER = 2; | |
GoogleAccountCredential mCredential; | |
private static String PREF_ACCOUNT_NAME = "accountName"; | |
static final String TAG = "TasksSample"; | |
final HttpTransport mHttpTransport = AndroidHttp.newCompatibleTransport(); | |
final JsonFactory mJsonFactory = GsonFactory.getDefaultInstance(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mCredential = GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS)); | |
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE); | |
mCredential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null)); | |
service = new Tasks.Builder(mHttpTransport, mJsonFactory, mCredential) | |
.setApplicationName("Google-TasksAndroidSample/1.0").build(); | |
Button b = (Button)findViewById(R.id.button_user_info); | |
b.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
chooseAccount(); | |
} | |
}); | |
} | |
private void chooseAccount(){ | |
startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data){ | |
super.onActivityResult(requestCode, resultCode, data); | |
if(requestCode == REQUEST_ACCOUNT_PICKER && resultCode == RESULT_OK && | |
data != null && data.getExtras() != null ){ | |
String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME); | |
if(accountName != null){ | |
mCredential.setSelectedAccountName(accountName); | |
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putString(PREF_ACCOUNT_NAME, accountName); | |
Log.d(TAG, accountName); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment