gpt4 book ai didi

android - 从哪里调用 Google 登录 Activity 中的异步任务?

转载 作者:搜寻专家 更新时间:2023-11-01 09:51:23 27 4
gpt4 key购买 nike

这是我的谷歌登录页面:

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();

最佳答案

您可以使用 SharedPreferences 记录您的登录状态。你也可以使用 MySQlite 来记录它。当你打开你的应用程序时,你可以判断你的状态,然后你可以选择你应该做什么。

关于android - 从哪里调用 Google 登录 Activity 中的异步任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36351131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com