这是我的google登录页面:
public class SignInActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private TextView mStatusTextView;
private ProgressDialog mProgressDialog;
ConnectivityManager cm;
NetworkInfo netInfo;
Context context;
ProgressDialog pd;
GoogleSignInAccount acct;
@Override
protected void onResume() {
super.onResume();
android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.hide();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin_page);
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
netInfo = cm.getActiveNetworkInfo();
context=this;
// Button listeners
findViewById(R.id.sign_in_button).setOnClickListener(this);
pd = new ProgressDialog(SignInActivity.this);
pd.setMessage("loading");
// [START configure_signin]
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// [END configure_signin]
// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// [END build_client]
// [START customize_button]
// Customize sign-in button. The sign-in button can be displayed in
// multiple sizes and color schemes. It can also be contextually
// rendered based on the requested scopes. For example. a red button may
// be displayed when Google+ scopes are requested, but a white button
// may be displayed when only basic profile is requested. Try adding the
// Scopes.PLUS_LOGIN scope to the GoogleSignInOptions to see the
// difference.
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
// [END customize_button]
}
@Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
// [START onActivityResult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
// [END onActivityResult]
// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
acct = result.getSignInAccount();
pd.show();
fetchUserdataServerAsync mfetchUserdataServerAsync=
new fetchUserdataServerAsync(acct.getEmail());
mfetchUserdataServerAsync.execute();
Intent intentreg = new Intent(this, RegistrationIntentService.class);
intentreg.putExtra("email", "" + acct.getEmail());
startService(intentreg);
// mStatusTextView.setText(getString("SignIN", acct.getDisplayName()));
updateUI(true);
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
// [END handleSignInResult]
// [START signIn]
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signIn]
// [START revokeAccess]
private void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
// [START_EXCLUDE]
updateUI(false);
// [END_EXCLUDE]
}
});
}
// [END revokeAccess]
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.hide();
}
}
private void updateUI(boolean signedIn) {
if (signedIn) {
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
} else {
// mStatusTextView.setText("Sign Out");
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
if (netInfo != null && netInfo.isConnected()) {
signIn();}else {
Toast.makeText(context, "Check Your Internet Connection",
Toast.LENGTH_LONG).show();
}
break;
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
private static boolean doesDatabaseExist(Context context) {
DataBaseHelper mydb = new DataBaseHelper(context);
File dbFile = context.getDatabasePath(mydb.getDatabaseName());
return dbFile.exists();
}
private class fetchUserdataServerAsync extends AsyncTask<Void, Void, String> {
String username;
public fetchUserdataServerAsync(String _username) {
username = _username;
}
@Override
protected String doInBackground(Void... voids) {
// JSONObject jsonObject= null;
JSONArray jsonArr = new JSONArray();
jsonArr.put(username);
return Http.httpPost(jsonArr, "http://xxxxx.com/fetchdata.jsp", null);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String data) {
super.onPostExecute(data);
Log.v("data received", "" + data);
JSONArray arr = null;
DataBaseHelper mydb = new DataBaseHelper(SignInActivity.this);
mydb.getWritableDatabase();
try {
arr = new JSONArray(data);
for (int i=0; i< arr.length(); i++) {
JSONObject jObj = arr.getJSONObject(i);
String id_To_Search = jObj.getString("id_To_Search");
.....
mydb.Add_Account(
recid, matdate, bank, userName);
}
}catch(JSONException e){
e.printStackTrace();
}
pd.dismiss();
Intent intent = new Intent(SignInActivity.this,
ShowAllAccounts.class);
startActivity(intent);
}
}
}
我很困惑应该拨打我的AsynTask
?得到acct.getEmail()
我需要拨打handleSigninResult()
,但如果我在每次打开应用时调用AsynTask
来调用。实际上我只需要在第一次登录时打电话给AsynTask
......怎么做任何想法...... ????
fetchUserdataServerAsync mfetchUserdataServerAsync= new fetchUserdataServerAsync(acct.getEmail());
mfetchUserdataServerAsync.execute();
答案 0 :(得分:2)
您可以使用SharedPreferences记录您的登录状态。您还可以使用MySQlite进行录制。当您打开应用程序时,您可以判断您的状态,然后您可以选择应该做什么。