读取的Dropbox文件 [英] Read a file from dropbox

查看:177
本文介绍了读取的Dropbox文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Dropbox文件系统,还有一个文件'check.txt包含值(0/1),我要检查每5分钟。结果
到Dropbox的访问是成功的,但该文件的读取并不总是正确的。结果
在第一文件中包含0和第一读取返回正确的值(0)。然后,如果我手动更改值1到文件中,下次阅读将再次返回值0,正确的值是众多读数后返回。结果
我使用Dropbox的同步和我的Andr​​oid版本为4.3

In my Dropbox file-system, there is a file 'check.txt' that contains a value (0/1) which I have to check every 5 minutes.
The access to Dropbox is successful but the reading of this file is not always right.
At first the file contains 0 and the first reading returns the correct value (0). Then if I manually change the value in 1 into the file, the next reading will return again the value 0 and the correct value is returned after many readings.
I use the Dropbox Synch and my android version is 4.3

这是code的一部分:

This is a part of code:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    try {       
        DbxAccountManager AcctMgr = DbxAccountManager.getInstance(getApplicationContext(), DropboxActivity.appKey, DropboxActivity.appSecret);          
        DbxFileSystem dbxFs = DbxFileSystem.forAccount(AcctMgr.getLinkedAccount());

        DbxFile file = dbxFs.open(DropboxActivity.path);

        DbxFileStatus status = file.getSyncStatus();
        if (!status.isCached) {
            file.addListener(new DbxFile.Listener() {
                @Override
                public void onFileChange(DbxFile file) {
                    try {
                        if (file.getSyncStatus().isCached) {
                          file.update();
                          // deal with the new value
                          Log.e("TAG", "*** VALUE *** " + file.readString());
                        }
                    } catch (DbxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }

        if((file.readString()).equals("0")) { 
            Log.d("TAG", "Value: " + file.readString());    

        }
        else { 
            Log.d("TAG", "Value: " + file.readString());
            flag = 1;

            stopAlarm();                
            startService(new Intent(this, GpsService.class));

        }

        file.close();

    } catch(Exception e) {
        e.printStackTrace();
    }



    stopSelf();

    return startId;
}

我如何通过CA使用 file.getNewerStatus() file.update()

或其他方法来正确地更新缓存文件?

How ca I use file.getNewerStatus() and file.update() or other methods to correctly update the cache files?

编辑:

推荐答案

您是在正确的轨道上。您需要保存的文件打开同步API下载新的内容,然后你要听更改,所以一定不要关闭它。请参见 https://www.dropbox.com/developers/sync/start/android#听众的。事情是这样的:

You're on the right track. You need to hold the file open for the Sync API to download new content, and then you need to listen for changes, so be sure to not close it. See https://www.dropbox.com/developers/sync/start/android#listeners. Something like this:

DbxFileStatus status = testFile.getSyncStatus();
if (!status.isCached) {
    testFile.addListener(new DbxFile.Listener() {
        @Override
        public void onFileChange(DbxFile file) {
            if (file.getSyncStatus().isCached) {
              file.update();
              // deal with the new value
            }
        }
    });
}

一旦你做到这一点,就没有必要检查文件每五秒钟......每次发生​​变化时,你会得到一个通知。

Once you do this, there's no need to check the file every five seconds... you'll get a notification every time it changes.

(另外,你可能想看看进入数据存储API而不是

(Also, you might want to look into the Datastore API instead.)

这篇关于读取的Dropbox文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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