是的,您可以访问accountManager中的信息,并使用注册的Google帐户(例如)对用户进行身份验证,而无需输入密码。
下面的代码是从Nick Johnson和Tencent的示例中混搭出来的。
从您的主Activity开始:
package com.tg.auth;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ZAuth02aActivity extends ListActivity {
protected AccountManager accountManager;
protected Intent intent;
String TAG = "TGtracker";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accountManager = AccountManager.get(getApplicationContext());
Account[] accounts = accountManager.getAccountsByType("com.google");
this.setListAdapter(new ArrayAdapter<Account>(this, R.layout.list_item, accounts));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Account account = (Account)getListView().getItemAtPosition(position);
Intent intent = new Intent(this, AppInfo.class);
intent.putExtra("account", account);
startActivity(intent);
}
}
创建一个AppInfo类:
package com.tg.auth;
import org.apache.http.impl.client.DefaultHttpClient;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AppInfo extends Activity {
DefaultHttpClient http_client = new DefaultHttpClient();
Activity activity;
String TAG = "TGtracker";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_info);
activity = this;
}
@Override
protected void onResume() {
super.onResume();
AccountManager accountManager = AccountManager.get(getApplicationContext());
Log.i(TAG, "got token, yipee: "+updateToken(accountManager, true));
}
private String updateToken(AccountManager am, boolean invalidateToken) {
String authToken = "null";
try {
Account[] accounts = am.getAccountsByType("com.google");
AccountManagerFuture<Bundle> accountManagerFuture;
if(activity == null){
accountManagerFuture = am.getAuthToken(accounts[0], "android", false, null, null);
} else {
accountManagerFuture = am.getAuthToken(accounts[0], "android", null, activity, null, null);
}
Bundle authTokenBundle = accountManagerFuture.getResult();
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString();
Log.v(TAG, "newToken preinvalidate: "+authToken);
if(invalidateToken) {
am.invalidateAuthToken("com.google", authToken);
authToken = updateToken(am, false);
}
} catch (IOException e) {
Log.e(TAG, "the exception was: "+e.toString());
e.printStackTrace();
}
return authToken;
}
}
使用类似以下的清单文件(必须声明活动和权限):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tg.auth"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ZAuth02aActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AppInfo"></activity>
</application>
</manifest>
使用Nick Johnson提供的布局文件,您可以在此处下载:
https://github.com/Arachnid/AEAuth。