2016-10-03 6 views
0

以下のコードは、自分のアプリにログインしてアカウントを選択したときに、初めてアカウント選択ダイアログボックスを表示していますが、どちらのアカウントを選択しても詳細を表示したいです。 )をクリックします。アカウントの詳細を表示する方法は? 。Account Chooserダイアログボックスで選択したアカウントの詳細を保存する方法は?

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

次はあなたがテントを使って、流れの中で記号を開始する必要があります。

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. 
        
    } 
} 
+0

私はこれらのメソッドとインテントを 'onCreate'メソッドで呼び出す必要がありますか? –

+0

Googleのサインインにカスタムボタンを使用している場合は、ボタンのクリックイベントでstartActivityReault()を呼び出すことができます。私は答えを更新しました。 – Darshan

+0

いいえ、私は自動的にアプリケーションを開くと、ダイアログボックスが表示され、サインインを要求します。そして、私がアカウントを選択すると、詳細を保存してナビゲーション・ドロワーに表示する必要があります。 –

関連する問題