我已经为用户实施了一种使用Google+帐户登录该应用程序的方法。 在Google Developers网页上的示例中,登录过程连接到Activity生命周期:用户在Activity启动时登录,并在Activity关闭时注销。
该应用程序有一个Activity,可以执行登录和注销功能。 但是,我可能需要在其他活动中访问Google+服务。 为此,我需要在每个Activity和其他方法上使用connect方法。 我认为这很糟糕,重复代码。 我该如何以良好的方式实现这一点?
完整的登录系统看起来如此。
public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener { private GoogleApiClient googleApiClient; private boolean intentInProcess = false; private int PLUS_SIGN_IN = 63640; private boolean signInClicked = false; private ConnectionResult connectionResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); googleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_LOGIN) .build(); } @Override protected void onStart() { super.onStart(); googleApiClient.connect(); } @Override protected void onStop() { super.onStop(); if(googleApiClient.isConnected()) { googleApiClient.disconnect(); } } private void resolveSignInErrors() { if(connectionResult.hasResolution()) { try { intentInProcess = true; connectionResult.startResolutionForResult(this, PLUS_SIGN_IN); } catch(SendIntentException e) { intentInProcess = false; googleApiClient.connect(); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == PLUS_SIGN_IN) { if(requestCode != RESULT_OK) { signInClicked = false; } intentInProcess = false; if(!googleApiClient.isConnecting()) { googleApiClient.connect(); } } } @Override public void onConnectionFailed(ConnectionResult result) { if(!intentInProcess) { connectionResult = result; if(signInClicked) { resolveSignInErrors(); } } } @Override public void onConnected(Bundle connectionHint) { signInClicked = false; } @Override public void onConnectionSuspended(int cause) { googleApiClient.connect(); } public void signOutPlus() { if(googleApiClient.isConnected()) { Plus.AccountApi.clearDefaultAccount(googleApiClient); googleApiClient.disconnect(); } } public void disconnectPlus() { Plus.AccountApi.clearDefaultAccount(googleApiClient); Plus.AccountApi.revokeAccessAndDisconnect(googleApiClient).setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status result) { } }); } }I have implemented a way for the users to sign in to the application with their Google+ account. In the examples on Google Developers webpage, the login process is connected to the Activity lifecycle: the user is logged in when the Activity starts and logged out when the Activity closes.
The application has one Activity where the login and sign out functionalities can be executed. Though, I might need access to the Google+ services in other activities. To achieve this, I'd need to use the connect method on every Activity and the other methods too. I think this is bad, repeating code. How do I implement this in a good way?
The complete sign in system looks this.
public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener { private GoogleApiClient googleApiClient; private boolean intentInProcess = false; private int PLUS_SIGN_IN = 63640; private boolean signInClicked = false; private ConnectionResult connectionResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); googleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_LOGIN) .build(); } @Override protected void onStart() { super.onStart(); googleApiClient.connect(); } @Override protected void onStop() { super.onStop(); if(googleApiClient.isConnected()) { googleApiClient.disconnect(); } } private void resolveSignInErrors() { if(connectionResult.hasResolution()) { try { intentInProcess = true; connectionResult.startResolutionForResult(this, PLUS_SIGN_IN); } catch(SendIntentException e) { intentInProcess = false; googleApiClient.connect(); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == PLUS_SIGN_IN) { if(requestCode != RESULT_OK) { signInClicked = false; } intentInProcess = false; if(!googleApiClient.isConnecting()) { googleApiClient.connect(); } } } @Override public void onConnectionFailed(ConnectionResult result) { if(!intentInProcess) { connectionResult = result; if(signInClicked) { resolveSignInErrors(); } } } @Override public void onConnected(Bundle connectionHint) { signInClicked = false; } @Override public void onConnectionSuspended(int cause) { googleApiClient.connect(); } public void signOutPlus() { if(googleApiClient.isConnected()) { Plus.AccountApi.clearDefaultAccount(googleApiClient); googleApiClient.disconnect(); } } public void disconnectPlus() { Plus.AccountApi.clearDefaultAccount(googleApiClient); Plus.AccountApi.revokeAccessAndDisconnect(googleApiClient).setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status result) { } }); } }最满意答案
创建一个实现Google Play Services的基本活动,然后在您需要使用googe play服务时扩展该活动
恩。
public class GoogleActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener然后延伸
public class MainActivity extends GoogleActivityCreate a base Activity that implements Google Play Services then extend that activity any time you need to use googe play services
ex.
public class GoogleActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListenerthen extend that
public class MainActivity extends GoogleActivity更多推荐
Activity,void,public,googleApiClient,Google,电脑培训,计算机培训,IT培训"/> <m
发布评论