我正在尝试Icloud
通过 Dropbox SYNC API 在 android 中实现功能(在不同设备中同时下沉)。我能够读写文件到 Dropbox。但我的问题是,即使我已经实现了DbxFile.Listener
. 我在这里发布我的示例代码。通过这个示例代码,我的目标是如果我在 Dropbox 中修改了一个文件,它也应该反映在应用程序中。此外,在两台设备上运行此应用程序时,Dropbox 文件夹中会发生文件冲突。谁能告诉我哪里出错了??在这方面的任何帮助都是非常可观的。
public class SecondActivity extends Activity implements OnClickListener {
private DbxAccountManager mDbxAcctMgr;
private static final String APP_KEY = "xxxxxxxxx";
private static final String APP_SECRET = "xxxxxxxxxx";
DbxFile testFile;
DbxFileSystem dbxFs;
DbxPath testPath;
Button btn;
EditText edit;
String contents;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
edit = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(this);
mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(),
APP_KEY, APP_SECRET);
testPath = new DbxPath(DbxPath.ROOT, "mytext.txt");
if (!mDbxAcctMgr.hasLinkedAccount()) {
mDbxAcctMgr.startLink((Activity) this, 0);
} else {
try {
dbxFs = DbxFileSystem
.forAccount(mDbxAcctMgr.getLinkedAccount());
readFile(dbxFs);
} catch (Unauthorized e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
try {
dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr
.getLinkedAccount());
readFile(dbxFs);
} catch (Unauthorized e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void readFile(DbxFileSystem dbxFs2) {
try {
try {
if (dbxFs2.exists(testPath)) {
testFile = dbxFs.open(testPath);
testFile.addListener(listener);
String contenString = testFile.readString().toString();
showtoast(contenString);
} else {
testFile = dbxFs.create(testPath);
}
} catch (DbxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
testFile.close();
}
}
private void showtoast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
contents = edit.getText().toString();
if (!TextUtils.isEmpty(contents)) {
try {
if (dbxFs.exists(testPath)) {
new syncDropBox().execute();
} else {
dbxFs.create(testPath);
}
} catch (DbxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
testFile.close();
}
}
}
private class syncDropBox extends AsyncTask<Void, Void, Boolean> {
boolean result = false;
@Override
protected Boolean doInBackground(Void... paramArrayOfParams) {
try {
dbxFs.syncNowAndWait();
result = true;
} catch (DbxException e) {
result = false;
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
showtoast("sync succeed");
try {
testFile = dbxFs.open(testPath);
} catch (DbxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
testFile.writeString(contents);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
showtoast("sync failed");
}
super.onPostExecute(result);
}
}
DbxFile.Listener listener = new Listener() {
@Override
public void onFileChange(DbxFile paramDbxFile) {
showtoast("file contents modified");
}
};
}