如何保存在帐户选择器对话框中选择的帐户的详细信息?

时间:2016-10-03 17:46:31

标签: android xml google-api google-account

下面的代码首次显示帐户选择器对话框,当我登录我的应用程序然后从中选择一个帐户时。但无论我选择哪个帐户,我想显示其详细信息( email_id,user_name,导航抽屉标题上的user_profile pic 。如何显示帐户的详细信息?

MainActivity.java

  public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    RecyclerView rvHome;
    RVAdapter rvAdapter;
    LinearLayoutManager llm;
    ArrayList<Doc> docs = new ArrayList<>();
    private boolean loading = true;
    private ProgressBar progressBar;
    private long numFound = 0;
    private boolean isFilter = false;
    private String category = "ad_category:3";
    private GoogleApiClient mGoogleApiClient;
    private ConnectionResult mConnectionResult;
    private static final int RC_SIGN_IN = 0;
    private boolean mIntentInProgress;
    private static final int PROFILE_PIC_SIZE = 400;
    CircleImageView profileImage;
    TextView userName;
    TextView userEmail;
    CallbackManager callbackManager;
    ProgressDialog progress;
    SharedPreferences sharedPreferences;
    private boolean isConnected;
    String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile";
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long startTime=System.currentTimeMillis();
        requestWindowFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("Popular Design");
        setColorIndex();
        setColors(this, colorIndex);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        rvHome = (RecyclerView) findViewById(R.id.rv_home);
        llm = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
        rvHome.setLayoutManager(llm);
        rvAdapter = new RVAdapter(MainActivity.this);
        rvHome.setAdapter(rvAdapter);
        rvHome.addOnScrollListener(scrollListener);
        progressBar = (ProgressBar) findViewById(R.id.loading_progress);
        progress = new ProgressDialog(this);
        progress.setCancelable(false);
        MyShopApplication application = (MyShopApplication) getApplication();
        Tracker mTracker = application.getDefaultTracker();
        sharedPreferences = getSharedPreferences(AppConstants.USER_PREFERENCES, Context.MODE_PRIVATE);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        View hView = navigationView.getHeaderView(0);
        navigationView.setCheckedItem(R.id.home);
        profileImage = (CircleImageView) hView.findViewById(R.id.imageView);
        userName = (TextView) hView.findViewById(R.id.user_name);
        userEmail = (TextView) hView.findViewById(R.id.user_email);
        userName.setText(sharedPreferences.getString(AppConstants.USER_NAME, ""));
        userEmail.setText(sharedPreferences.getString(AppConstants.USER_EMAIL, ""));
        String image_url = sharedPreferences.getString(AppConstants.USER_EMAIL, "");
        if (!image_url.trim().matches("")) {
            Picasso.with(this).load(image_url).into(profileImage);
        }
        checkPermissions();
        callbackManager = CallbackManager.Factory.create();
        mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .build();
        Utils.TrackTiming(MyShopApplication.getInstance().getDefaultTracker(),AppConstants.ACTIVITY_LOAD,"MAIN_ACTIVITY",System.currentTimeMillis()-startTime,AppConstants.LOADED);

    }
}

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

Navigation_drawer

1 个答案:

答案 0 :(得分:1)

接下来,您必须使用Intent开始登录流程。

public void startSignIn(){
    Intent signInIntent =     Auth.GoogleSignInApi.getSignInIntent(.   mGoogleApiClient);
            startActivityForResult(signInIntent,     GOOGLE_SIGN_IN);
}

现在实现onActivityResult()以获得结果。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent.
        if (requestCode == GOOGLE_SIGN_IN && resultCode == RESULT_OK) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

现在您可以从结果对象中获取帐户详细信息。

private void     handleSignInResult(GoogleSignInResult result) {
    //Check if signin result is successful
    if (result.isSuccess()) {
        // Signed in successfully.
        GoogleSignInAccount acct =     result.getSignInAccount();
                testView.setText(acct.getDisplayName());
Uri profilePictureUri = acct.getPhotoUrl();
//You can get other details like email,Id etc.
      
    } else {
        // Signed out.
       
    }
}