java - 带有回调的方法中的操作顺序不正确

标签 java android callback google-drive-api

我有下一个方法,返回 ArrayList<String> ,来自 Google 云端硬盘帐户的数据。

public int getMetodsList()
{   final ArrayList<String> result = new ArrayList<String>();
     System.out.println("1");
    Drive.DriveApi.getFolder(MainActivity.mGoogleApiClient, getIDOfFolder("SomeFolder"))
    .listChildren(MainActivity.mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>()
            {
            @Override
            public void onResult(MetadataBufferResult mdbr) {
                 System.out.println("2");
                for (int i = 0; i < mdbr.getMetadataBuffer().getCount(); i++)
                {  result.add(mdbr.getMetadataBuffer().get(i).getTitle());
                }
                 System.out.println("3");
            } }
        );  
    System.out.println("4");
    return result.size();   

为了帮助我添加了 log带数字 - System.out.println("number"); 当我调用此方法时,Log返回我的下一个序列: 1、4、2、3。 所以我做错了ArrayList , returnCallBack() 之前调用

请帮忙,我如何调用回调 ** ResultCallback<DriveApi.MetadataBufferResult>之前return ?

最佳答案

您使用的是异步方法,因此输出不足为奇。当你想同步访问文件夹内容时,使用await:

With this release of Google Play services, you now have the option to specify if an API call should execute asynchronously (you will receive a callback once it is finished), or synchronously (the thread will block until the operation has completed). This is achieved by using the classes PendingResult, Result, and Status in the com.google.android.gms.common.api package.

In practice, this means that API operations will return an instance of PendingResult, and you can choose if you want the method to execute asynchronously using setResultCallback or synchronously using await. (http://android-developers.blogspot.de/2014/02/new-client-api-model-in-google-play.html).

关于java - 带有回调的方法中的操作顺序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22474791/

相关文章:

java - 使用 JTWAIN 访问网络扫描仪

java线程对静态类的影响

带有方法调用的 Java 8 过滤器

java - 尽管设备已收到消息,但未从 SMS 检索器 API 接收 SMS 内容

c++ - 将 userData 从回调开始传递到回调结束

java - 从 GAE 上传和下载 blob

android - firebase实时数据库如何读取数据?

android - 通过蓝牙播放 TTS

java - 从位于异步任务 doInBackGround 中的回调返回一个值

jquery回调函数