Read file from Dropbox

There is a check.txt file in my Dropbox file system that contains a value (0/1) that I have to check every 5 minutes.
Access to Dropbox is successful, but reading this file is not always correct.
First, the file contains 0, and the first read returns the correct value (0). Then, if I manually changed the value of 1 to the file, the next read will return the value 0 again and the correct value will be returned after many reads.
I use Dropbox Synch and my version is for Android 4.3.

This is part of the 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 do I use file.getNewerStatus() and file.update() or other methods to properly update cache files?

Edit:

+2
source share
1 answer

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

 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 is no need to check the file every five seconds ... you will receive a notification every time it changes.

(Alternatively, you may need to use the Datastore API instead.

+1
source

Source: https://habr.com/ru/post/1241149/


All Articles