我已成功将Dropbox的Sync API应用到我的应用中。这是代码
public class MainActivity extends Activity implements SyncStatusListener
{
private static final String appKey = "xxxxx";
private static final String appSecret = "xxx";
private static final int REQUEST_LINK_TO_DBX = 0;
private TextView mTestOutput;
private Button mLinkButton;
private Button saveButton;
private EditText txtFile;
private DbxAccountManager mDbxAcctMgr;
private static String fileName = "data.txt";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtFile = ( EditText ) findViewById( R.id.writeFile );
mTestOutput = (TextView) findViewById(R.id.test_output);
mLinkButton = (Button) findViewById(R.id.link_button);
saveButton = (Button) findViewById( R.id.save );
mLinkButton.setOnClickListener( new OnClickListener()
{
@Override
public void onClick ( View view )
{
if ( mDbxAcctMgr != null )
{
mDbxAcctMgr.startLink( (Activity)MainActivity.this, REQUEST_LINK_TO_DBX );
}
}
});
saveButton.setOnClickListener( new OnClickListener()
{
@Override
public void onClick ( View view )
{
SaveFile();
}
});
mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
}
@Override
protected void onResume()
{
super.onResume();
if (mDbxAcctMgr.hasLinkedAccount())
{
loadFileFromDropbox();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if ( requestCode == REQUEST_LINK_TO_DBX )
{
if (resultCode == Activity.RESULT_OK)
{
loadFileFromDropbox();
}
else
{
mTestOutput.setText("Link to Dropbox failed or was cancelled.");
}
}
else
{
super.onActivityResult(requestCode, resultCode, data);
}
}
private void loadFileFromDropbox()
{
try
{
DbxPath testPath = new DbxPath( DbxPath.ROOT, fileName );
// Create DbxFileSystem for synchronized file access.
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
if (dbxFs.isFile(testPath))
{
String resultData;
DbxFile testFile = dbxFs.open(testPath);
try
{
resultData = testFile.readString();
}
finally
{
testFile.close();
}
mTestOutput.append("\nRead file '" + testPath + "' and got data:\n " + resultData);
txtFile.setText(resultData);
}
else if (dbxFs.isFolder(testPath))
{
mTestOutput.append("'" + testPath.toString() + "' is a folder.\n");
}
}
catch ( Exception e )
{
System.out.println ( "loadFileFromDropbox Error : " + e.toString() );
}
}
private void SaveFile()
{
try
{
DbxPath testPath = new DbxPath( DbxPath.ROOT, fileName );
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
// Create a test file only if it doesn't already exist.
String fileData = txtFile.getText().toString().trim();
dbxFs.addSyncStatusListener(this);
if (!dbxFs.exists(testPath))
{
DbxFile testFile = dbxFs.create(testPath);
try
{
if ( !fileData.equals( "" ) )
{
testFile.writeString( fileData );
}
}
finally
{
testFile.close();
}
mTestOutput.append("\nCreated new file '" + testPath + "'.\n");
}
else
{
DbxFile testFile = dbxFs.open( testPath );
try
{
if ( !fileData.equals( "" ) )
{
testFile.writeString( fileData );
}
}
finally
{
testFile.close();
}
}
}
catch ( Exception e )
{
System.out.println ( "Save Error : " + e.toString() );
}
}
@Override
public void onSyncStatusChange( DbxFileSystem fs )
{
try
{
if ( fs.getSyncStatus() != null )
{
loadFileFromDropbox();
}
}
catch ( Exception e )
{
System.out.println ( "OK " + e.toString() );
}
}
}
我有一个EditText,它将加载文件的内容。我可以添加/更新其内容并将其保存在我的保管箱帐户中。保存后,它会多次调用onSyncStatusChange()
方法。
为什么这样?
答案 0 :(得分:0)
如果您从onSyncStatusChange
记录实际状态,我想每次都会发生什么变化。
我的猜测是,当状态更改为上传时会调用一次,然后在上传完成时再次调用。因此,如果您记录fs.getSyncStatus().uploading.inProgress
,您会看到它翻转为true然后返回false,这是有道理的。