简体   繁体   English

Android-验证过程结束时调用函数

[英]Android - Call a function when authenticate process end

I'm trying to integrate dropbox upload in my app. 我正在尝试将Dropbox上传集成到我的应用中。 I've read the official examples and I was able to write a client to upload the files. 我已经阅读了官方示例,并且能够编写一个客户端来上传文件。 Now there is just one thing that I'd like to fix.. the example first run this line of code: 现在只有一件事我想修复..该示例首先运行以下代码行:

mDBApi.getSession().startOAuth2Authentication(MainActivity.this);

and then use OnResume to complete the auth: 然后使用OnResume完成身份验证:

protected void onResume() {
    super.onResume();
    if (mDBApi.getSession().authenticationSuccessful()) {
        try {
            // Required to complete auth, sets the access token on the session
            mDBApi.getSession().finishAuthentication();

            String accessToken = mDBApi.getSession().getOAuth2AccessToken();
        } catch (IllegalStateException e) {
            Log.i("DbAuthLog", "Error authenticating", e);
        }
    }
    new myOperation().execute("");
}

This works, but I was looking for a way to replace the onResume (that is also called when the app start and cause some exceptions). 这行得通,但是我一直在寻找一种替换onResume的方法(当应用程序启动并引起某些异常时也被调用)。 Is there a way to call a function when the mDBApi.getSession().startOAuth2Authentication(MainActivity.this); 当mDBApi.getSession()。startOAuth2Authentication(MainActivity.this);时,是否可以调用函数? ends ? 结束了吗?

Thanks for the help, and as usual, if you need further infos please ask 感谢您的帮助,如往常一样,如果您需要更多信息,请询问

if this is the only way available by the API, then you have to use it if you have a control over what startOAuth2Authentication do, you can use startActivityForResult() , but its inside API you can't change it. 如果这是API可用的唯一方法,那么如果您可以控制startOAuth2Authentication的操作,则必须使用它,可以使用startActivityForResult() ,但是无法在其内部API中进行更改。

so i would suggest you handle whatever exceptions you get at onResume() by using some validations, like wrapping whole code inside onResume() with this 所以我建议您通过使用一些验证来处理在onResume()上遇到的任何异常,例如,将整个代码包装在onResume()中

if(mDBApi!=null){...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM