2

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

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;
}

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

Edit:

user2955666
  • 23
  • 1
  • 4

1 Answers1

1

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.

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

user94559
  • 59,196
  • 6
  • 103
  • 103
  • I'm sorry but I can't make it work. I added the new code. Basically I have an activity that calls this service through an alarm every 5 minutes to check the file. The method onFileChange() is not called. What I'm doing wrong? – user2955666 Nov 07 '13 at 17:26
  • Perhaps you're not keeping the file open? Maybe you could share your current code. – user94559 Nov 07 '13 at 19:06
  • I resolved my problem. I closed the file in the wrong position. Thanks for your help :) – user2955666 Nov 08 '13 at 14:09
  • 1
    Hi @smarx, I am working on a project in android studio and I need to read a file from dropbox called 'Changed', please could you help me with that. OP only provided part of their code, so I am not really sure how I can go about reading the file. The file has a public shared link. Any help will be nice. Thanks. – Henry Jan 21 '16 at 13:24