2015-10-17 55 views
1

通过Android应用程序的G +登录很容易,当我已经有一个电子邮件ID在我的手机,但当我重新启动它时GoogleApiClient不调用连接方法现在我的手机目前没有gmail id登录 那么如何让它重新连接后再次连接? 有没有想法的人? 这里的一些源代码,我使用Google+帐户登录: -如何通过Android应用程序连接到谷歌服务器后,我出厂重置我的手机

连接上的按钮点击

btnSignIn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       mGoogleApiClient.connect(); 
      } 
     }); 

当连接不成功

public void onConnectionFailed(ConnectionResult arg0) { 

     if (!mIntentInProgress && arg0.hasResolution()) { 
      try { 
       mIntentInProgress = true; 
       arg0.startResolutionForResult(this, RC_SIGN_IN); 
      } catch (IntentSender.SendIntentException e) { 

       mIntentInProgress = false; 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 

OnActivity结果

@Override 
    protected void onActivityResult(int requestCode, int responseCode, 
            Intent intent) { 
     if (requestCode == RC_SIGN_IN) { 
      mIntentInProgress = false; 
      if (!mGoogleApiClient.isConnecting() && responseCode==-1) { 
       Log.d("Flag",mGoogleApiClient.isConnecting()+"res"+responseCode); 
       mGoogleApiClient.connect(); 
      } 

     } 
    } 

获得当连接,获取登录人的图像和名称

@Override 
    public void onConnected(Bundle arg0) { 

     Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show(); 
     if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { 
      Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); 
      Log.d("Welcome : ", person.getDisplayName()); 
      try { 
       JSONObject jsonObject = new JSONObject(person.getImage() 
         .toString()); 
       String imageUrl = jsonObject.getString("url"); 
       try { 
        URL url = new URL(imageUrl); 
        Bitmap bmp; 
        bmp = BitmapFactory.decodeStream(url.openConnection() 
          .getInputStream()); 
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
        byte[] byteArray = stream.toByteArray(); 
        Intent i1 = new Intent(MainActivity.this, DetailActivity.class); 
        i1.putExtra("image", byteArray); 
        i1.putExtra("name", person.getDisplayName()); 
        i1.putExtra("count", "2"); 
        startActivity(i1); 
        finish(); 

       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
+0

您是否在询问有关提供用于使用您的应用登录的新电子邮件? –

+0

我想要的是作为应用程序没有找到任何帐户登录手机可能会重定向我添加帐户选项(SignUp)或一定间隔后弹出,如果没有得到connected.I要通知用户这基本上 –

回答

0

由于您的设备不必须与它集成任何Gmail帐户,现在,它会试图让连接

public void onConnectionFailed(ConnectionResult arg0) { 

    if (!mIntentInProgress && arg0.hasResolution()) { 
     try { 
      mIntentInProgress = true; 
      arg0.startResolutionForResult(this, RC_SIGN_IN); 
     } catch (IntentSender.SendIntentException e) { 

      mIntentInProgress = false; 
      mGoogleApiClient.connect(); 
     } 
    } 
    else { 
      //you can do whatever you want in this block,like adding a dialog, navigating to sign up etc.. 
     } 
} 

希望这正是你需要的东西后调用下面的方法。

+0

感谢您帮助。工作良好 –

+0

快乐编码(y) –

相关问题