如何确认时,用户不希望连接到谷歌播放服务? [英] How to acknowledge when user doesn't want to connect to Google Play Services?

查看:115
本文介绍了如何确认时,用户不希望连接到谷歌播放服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果系统提示让我的应用程序访问自己的谷歌驱动器帐户命中谁用户取消,有没有办法承认他们点击取消?现在我的应用程序不断要求他们选择谷歌驱动器帐户才能使用。

If a user who is prompted to let my app access their Google Drive account hits "cancel," is there a way to acknowledge that they clicked cancel? Right now my application continuously asks them to choose a Google Drive account to use.

这是在code:

    @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // This is the XML for this activity
    setContentView(R.layout.view_all_timelines_activity);

    // This is used for authentication and uploading to Google Drive
    googleApiClient = new GoogleApiClient.Builder(this).addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();

    googleApiClient.connect();

我怎么能承认,如果他们说取消,不希望连接?我不希望不断纠缠登录的用户。

How can I acknowledge if they say "Cancel" and don't want to connect? I don't want to constantly pester the user to log in.

推荐答案

我会努力制定一个更深一点,所以答案包括参与获取谷歌推动Android API(GDAA)连接和工作全过程。假设你在控制台注册您的应用程序,你的应用程序已经打通3障碍。所有这些都涉及您的行为放弃控制到另一个活动(对话)和追赶,结果在回调中。我试图写一个小,精益和平均的演示可以是这里发现。由于通常情况下,我悲惨地失败了,在code结束了没有接近贫放大器;平均(更像臃肿,丑陋:)。

I will try to elaborate a little deeper, so the answer covers the full process involved in getting the Google Drive Android API (GDAA) connected and working. Assuming you registered your app in the console, your app has to get through 3 hurdles. All of them involve your activity relinquishing control to another activity (dialog) and catching the result in callbacks. I attempted to write a small, lean and mean demo which can be found here. As usually, I failed miserably, the code ended up nothing close to lean&mean (more like bloated and ugly :).

我注意到,很多开发者栽倒在授权/认证。它需要强调指出的控制台必须知道应用程序的包名称,它的SHA1。您的应用程序的SHA1载你*的.apk文件里。你有2个版本的APK文件,调试和发行版本,他们每个人都有不同的SHA1。 <一href="http://stackoverflow.com/questions/28532206/google-oauth-2-0-and-debug-key-for-google-drive-api">It在这里讨论。

I noticed that a lot of developers stumble on authorization/authentication. It needs to be stressed out that the 'console' has to know the app's package name and it's SHA1. The SHA1 of your app is contained inside of your *.APK file. You have 2 versions of APK files, the debug and the release version, each of them has a different SHA1. It was discussed here.

所以,障碍是:

1 /检查播放服务

某处的OnCreate(),你可以将初步核实,如果您有谷歌播放提供服务,如果您的版本就可以了。

somewhere in OnCreate(), you may put an initial check if you have Google Play Services available and if your version is OK.

@Override
protected void onCreate(Bundle bundle) {   super.onCreate(bundle);
  ...
  if (checkPlayServices() && checkUserAccount()) {
    GooDrive.init(this, UT.AM.getActiveEmil());
    GooDrive.connect(true);
  }
  ...
}

这是checkPlayServices()以上,是这样的。

it is the 'checkPlayServices()' above, and goes like this

private boolean checkPlayServices() { 
  int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  if (status != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
      errorDialog(status, REQ_RECOVER);
    } else {
      finish();
    }
    return false;
  }
  return true;
}

如果不成功(但可恢复),则控制被释放到GooPlaySvcs对话,最终导致该'onActivityResult'回调REQ_RECOVER:

If unsuccessful (but recoverable), the control is relinquished to GooPlaySvcs dialog, eventually resulting in the 'onActivityResult' callback REQ_RECOVER:

@Override
protected void onActivityResult(int request, int result, Intent data) {
  switch (request) {
    case REQ_RECOVER: {
      mIsInAuth = false;
      if (result == Activity.RESULT_OK) {
        GooDrive.connect(true);
      } else if (result == RESULT_CANCELED) {
        finish();
      }
      break;
    }
  }
  super.onActivityResult(request, result, data);
}

如果成功恢复和用户没有退出,该应用程序可以欢快地继续连接尝试这可能(也可能不会)成功(稍后我们将会看到);

if the recovery is successful and user did not quit, the app can merrily continue to a connect attempt which may (or may not) be successful (we'll see later);

2 /获取的用户帐户。

2/ Get the user account.

您需要的用户账户(Gmail的),谷歌驱动器不会跟你离不开它。有几种方法可以得到它,你可能很难℃下将其myown@gmail.com$ C $,或者你可以从账户通过账户选择对话框设备的列表中得到它:

You will need user account (gmail), Google Drive will not talk to you without it. There are few ways to get it, you may hardcode it "myown@gmail.com", or you can go a get it from your device's list of accounts via Account Picker Dialog:

private boolean checkUserAccount() {  
  String email = UT.AM.getActiveEmil();
  Account accnt = UT.AM.getPrimaryAccnt(true);
  if (email == null) {
    if (accnt == null) {         // multiple or no accounts available, go pick/create one
      accnt = UT.AM.getPrimaryAccnt(false);  // pre-select primary account if present
      Intent it = AccountPicker.newChooseAccountIntent(accnt, null,
       new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null
      );
      startActivityForResult(it, REQ_ACCPICK);
      return false;  //--------------------->>>
    } else {  // there's only one goo account registered with the device, skip the picker
      UT.AM.setEmil(accnt.name);
    }
    return true;  //------------------>>>>
  }

  // UNLIKELY BUT POSSIBLE,
  // emil's OK, but the account have been removed (through settings), re-select
  accnt = UT.AM.getActiveAccnt();
  if (accnt == null) {
    accnt = UT.AM.getPrimaryAccnt(false);
    Intent it = AccountPicker.newChooseAccountIntent(accnt, null,
     new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null
    );
    startActivityForResult(it, REQ_ACCPICK);
    return false;  //------------------>>>
  }
  return true;
}

在code以上会从缓存或帐户的设备的清单Gmail帐户,同时在考虑该应用程序正在运行,当用户删除的设置的帐户的可能性。它的工作原理在缓存pviously选择账户$ P $与AM(的AccountManager)合作。再次,它可能最终结束了在账户选择对话框导致回调(同上):

the code above gets gmail account from cache or the device's list of accounts, taking in account the possibility that the user removed the account in settings when the app was running. It works in cooperation with the AM(AccountManager) that caches previously selected account. Again, it may eventually end up in Account Picker Dialog resulting in a callback (same as above):

@Override
protected void onActivityResult(int request, int result, Intent data) {
  switch (request) {
    case REQ_ACCPICK: {  // return from account picker
      if (result == Activity.RESULT_OK && data != null) { 
        String email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        if (UT.AM.setEmil(email) == UT.AM.CHANGED) {
          GooDrive.init(this, UT.AM.getActiveEmil());
          GooDrive.connect(true);
        }
      } else if (UT.AM.getActiveEmil() == null) { 
        finish();
      }
      break;
    }
  }
  super.onActivityResult(request, result, data);
}

如果用户没有退出,应用程序可以乐呵呵地初始化GoogleApiClient并进行其他连接尝试这可能(也可能不会)成功;

If the user did not quit, the app can cheerfully initialize GoogleApiClient and make another connect attempt which may (or may not) be successful;

3 /的连接尝试可能导致onConnectionFailed(),其可具有恢复溶液

3/ The connect attempt may result in onConnectionFailed(), which may have recovery solution

@Override
public void onConnectionFailed(ConnectionResult result) {
  if (!mIsInAuth) {
    if (result.hasResolution()) {
      try {
        mIsInAuth = true;
        result.startResolutionForResult(this, REQ_AUTH);
      } catch (IntentSender.SendIntentException e) {
        finish();
      }
    } else {
      finish();
    }
  }
}

,如果它有一个解决方案,则很可能需要用户授权。基本上,用户在确认该应用程序可以与谷歌驱动器的一方。再次,结果回来的onActivityResult回调。

if it has a solution, it is likely the need for user authorization. Basically user confirming that this app can have a party with Google Drive. And again, the result comes back in onActivityResult callback.

@Override
protected void onActivityResult(int request, int result, Intent data) {
  switch (request) {
    case REQ_AUTH: {
      mIsInAuth = false;
      if (result == RESULT_OK) {
        GooDrive.connect(true);
      } else if (result == RESULT_CANCELED) {
        finish();
      }
      break;
    }
  }
  super.onActivityResult(request, result, data);
}

在code这里是相同的步骤1 /,即连接成功,退出O故障/用户取消。谷歌驱动器记住这个授权,所以你通常会看到它只有一次。如果您需要重置它,去到Web驱动器,设置>管理应用程序>从驱动器断开

The code here is the same as in step 1/, i.e. connect on success, quit o failure / user CANCEL. Google Drive remembers this authorization, so you usually see it only once. If you need to reset it, go to the web Drive, Settings > Manage Apps > Disconnect from Drive

回顾:

  • 第1步:如果你不通过关一送呢,你是DOA
  • 第2步:如果你只有一个帐户或用户选择了一个已经和你缓存它,把它交给GooDrive初始化没有窃听用户
  • 第三步:如果用户授权的应用程序,它永远不会再错误他,除非他/她弄乱在网络上的从驱动器断开连接。

步骤2和3可能不会在这里描述的顺序,这取决于你的应用程序逻辑。

The steps 2 and 3 may not come in the order described here, it depends on your app logic.

好运。

这篇关于如何确认时,用户不希望连接到谷歌播放服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆