Java-如何在其他活动中登录Google之后保存Google个人资料

Java-如何在其他活动中登录Google之后保存Google个人资料,第1张

概述我是Android编程新手.我成功集成了Google登录并成功将数据传递给另一个活动.但是,当我通过导航抽屉返回活动时,我在其中传递了数据,因此播出了空白屏幕.我想在该活动中保存用户个人资料.我想通过永久单击应用程序中的“保存”按钮以显示为用户个人资料来保存用户详细信息.请帮助我

我是Android编程新手.我成功集成了Google登录并成功将数据传递给另一个活动.但是,当我通过导航抽屉返回活动时,我在其中传递了数据,因此播出了空白屏幕.我想在该活动中保存用户个人资料.我想通过永久单击应用程序中的“保存”按钮以显示为用户个人资料来保存用户详细信息.请帮助我解决这个问题.谢谢,提前提供解决方案.

这是我的Java文件和XML文件.

activity_main3.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:app="http://schemas.androID.com/apk/res-auto"xmlns:tools="http://schemas.androID.com/tools"androID:ID="@+ID/activity_main3"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical"tools:context=".Main3Activity"><linearLayout    androID:ID="@+ID/prof_section"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_marginleft="20dp"    androID:layout_margintop="50dp"    androID:orIEntation="horizontal">    <ImageVIEw        androID:ID="@+ID/prof_pic"        androID:layout_wIDth="90dp"        androID:layout_height="125dp"        androID:src="@drawable/profilep" />    <linearLayout        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginleft="28dp"        androID:layout_margintop="20dp"        androID:orIEntation="vertical">        <TextVIEw            androID:ID="@+ID/name"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:gravity="center"            androID:text="disply name Here"            androID:textSize="18dp"            androID:textStyle="bold" />        <TextVIEw            androID:ID="@+ID/email"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:layout_margintop="10dp"            androID:gravity="center"            androID:text="disply Email Here"            androID:textSize="18dp"            androID:textStyle="bold" />        <button            androID:ID="@+ID/butn_logout"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:text="logout"            />    </linearLayout></linearLayout><com.Google.androID.gms.common.SignInbutton    androID:ID="@+ID/butn_login"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_marginleft="50dp"    androID:layout_marginRight="50dp"    androID:layout_margintop="60dp"  ></com.Google.androID.gms.common.SignInbutton>

activity_detail.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical"androID:paddingBottom="@dimen/activity_vertical_margin"androID:paddingleft="@dimen/activity_horizontal_margin"androID:paddingRight="@dimen/activity_horizontal_margin"androID:paddingtop="@dimen/activity_vertical_margin"tools:context=".Detail"><ImageVIEw    androID:ID="@+ID/dp"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_margin="10dp" /><TextVIEw    androID:ID="@+ID/name"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_margin="10dp" /><TextVIEw    androID:ID="@+ID/email"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_margin="10dp" /><button    androID:ID="@+ID/button_save"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:text="SAVE" />

细节Java

    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_detail);    dp = (ImageVIEw) findVIEwByID(R.ID.dp);    name = (TextVIEw) findVIEwByID(R.ID.name);    email = (TextVIEw) findVIEwByID(R.ID.email);    Intent i = getIntent();    final String i_name, i_email, i_url;    i_name = i.getStringExtra("p_name");    i_email = i.getStringExtra("p_email");    i_url = i.getStringExtra("p_url");    name.setText(i_name);    email.setText(i_email);    new Thread(new Runnable() {        @OverrIDe        public voID run() {            try {                URL url = new URL(i_url);                inputStream is = url.openConnection().getinputStream();                final Bitmap bmp = BitmapFactory.decodeStream(is);                runOnUiThread(new Runnable() {                    @OverrIDe                    public voID run() {                        dp.setimageBitmap(bmp);                    }                });            } catch (MalformedURLException e) {                e.printstacktrace();            } catch (IOException e) {                e.printstacktrace();            }        }    }).start();}    

Main3Activity.Java

@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main3);    preferenceConfig = new SharedPreferenceConfig(getApplicationContext());    if (preferenceConfig.readLoginStatus()) {        startActivity(new Intent(this, Main2Activity.class));        finish();    }    Prof_Section = (linearLayout) findVIEwByID(R.ID.prof_section);    SignOut = (button) findVIEwByID(R.ID.butn_logout);    SignIn = (SignInbutton) findVIEwByID(R.ID.butn_login);    name = (TextVIEw) findVIEwByID(R.ID.name);    Email = (TextVIEw) findVIEwByID(R.ID.email);    Prof_Pic = (ImageVIEw) findVIEwByID(R.ID.prof_pic);    SignIn.setonClickListener(this);    SignOut.setonClickListener(this);    Prof_Section.setVisibility(VIEw.GONE);    GoogleSignInoptions signInoptions = new GoogleSignInoptions.Builder(GoogleSignInoptions.DEFAulT_SIGN_IN).requestemail().requestProfile().build();    Googleapiclient = new Googleapiclient.Builder(this).enableautoManage(this, this).addAPI(Auth.Google_SIGN_IN_API, signInoptions).build();}@OverrIDepublic voID onClick(VIEw v) {    switch (v.getID()) {        case R.ID.butn_login:            signIn();            break;        case R.ID.butn_logout:            signOut();            break;    }}@OverrIDepublic voID onConnectionFailed(@NonNull ConnectionResult connectionResult) {}private voID signIn() {    Intent intent = Auth.GoogleSignInAPI.getSignInIntent(Googleapiclient);    startActivityForResult(intent, REQ_CODE);}private voID signOut() {    Auth.GoogleSignInAPI.signOut(Googleapiclient).setResultCallback(new ResultCallback<Status>() {        @OverrIDe        public voID onResult(@NonNull Status status) {            updateUI(false);        }    });}private voID updateUI(boolean isLogin) {    if (isLogin) {        Prof_Section.setVisibility(VIEw.VISIBLE);        SignIn.setVisibility(VIEw.GONE);    } else {        Prof_Section.setVisibility(VIEw.GONE);        SignIn.setVisibility(VIEw.VISIBLE);    }}@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (requestCode == REQ_CODE) {        GoogleSignInResult GoogleSignInResult = Auth.GoogleSignInAPI.getSignInResultFromIntent(data);        GoogleSignInAccount account = GoogleSignInResult.getSignInAccount();        String name = account.getdisplayname();        String email = account.getEmail();        String img_url = account.getPhotoUrl().toString();        name.setText(name);        Email.setText(email);        GlIDe.with(this).load(img_url).into(Prof_Pic);        updateUI(true);        preferenceConfig.writeLoginStatus(true);        try {            Intent sendData = new Intent(Main3Activity.this, Detail.class);            name = account.getdisplayname();            email = account.getEmail();            img_url = account.getPhotoUrl().toString();            sendData.putExtra("p_name", name);            sendData.putExtra("p_email", email);            sendData.putExtra("p_url", img_url);            startActivity(sendData);        } catch (Exception e) {            Toast.makeText(Main3Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();        }    } else {        Toast.makeText(Main3Activity.this, "Login Failed", Toast.LENGTH_SHORT).show();    }}

解决方法:

为您的目的使用共享的首选项.您可以阅读有关here的更多信息.

此处提供示例sharedPreferences

总结

以上是内存溢出为你收集整理的Java-如何在其他活动中登录Google之后保存Google个人资料全部内容,希望文章能够帮你解决Java-如何在其他活动中登录Google之后保存Google个人资料所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081342.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存