Android上的Google Identity Toolkit,检查用户是否已登录

时间:2016-02-09 21:42:40

标签: android google-identity-toolkit

我已按照此文档创建了一个使用Google / Facebook身份验证的Android应用。

https://developers.google.com/identity/toolkit/android/quickstart

登录工作正常,但问题是我每次关闭应用程序时都必须登录(按登录按钮)。

有没有办法检查用户是否已登录并且令牌未过期?

我应该将令牌值保存在共享首选项,数据库还是Web服务中?

登录活动:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private GitkitClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        client = GitkitClient.newBuilder(this, new GitkitClient.SignInCallbacks() {
            @Override
            public void onSignIn(IdToken idToken, GitkitUser user) {
                //showProfilePage(idToken, user);
                Intent mapsIntent = new Intent(LoginActivity.this, PhotosMapActivity.class);
                startActivity(mapsIntent);
            }

            @Override
            public void onSignInFailed() {
                Toast.makeText(LoginActivity.this, "Sign in failed", Toast.LENGTH_LONG).show();
            }
        }).build();

        showSignInPage();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (!client.handleActivityResult(requestCode, resultCode, intent)) {
            super.onActivityResult(requestCode, resultCode, intent);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (!client.handleIntent(intent)) {
            super.onNewIntent(intent);
        }
    }

    private void showSignInPage() {
        setContentView(R.layout.welcome);
        Button button = (Button) findViewById(R.id.sign_in);
        button.setOnClickListener(this);
    }

    private void showProfilePage(IdToken idToken, GitkitUser user) {
        setContentView(R.layout.profile);
        showAccount(user);
        findViewById(R.id.sign_out).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in) {
            client.startSignIn();
        } else if (v.getId() == R.id.sign_out) {
            showSignInPage();
        }
    }

    private void showAccount(GitkitUser user) {
        ((TextView) findViewById(R.id.account_email)).setText(user.getEmail());

        if (user.getDisplayName() != null) {
            ((TextView) findViewById(R.id.account_name)).setText(user.getDisplayName());
        }

        if (user.getPhotoUrl() != null) {
            final ImageView pictureView = (ImageView) findViewById(R.id.account_picture);
            new AsyncTask<String, Void, Bitmap>() {

                @Override
                protected Bitmap doInBackground(String... arg) {
                    try {
                        byte[] result = HttpUtils.get(arg[0]);
                        return BitmapFactory.decodeByteArray(result, 0, result.length);
                    } catch (IOException e) {
                        return null;
                    }
                }

                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    if (bitmap != null) {
                        pictureView.setImageBitmap(bitmap);
                    }
                }
            }.execute(user.getPhotoUrl());
        }
    }
}

0 个答案:

没有答案