Android onSyncStatusChange在Dropbox Sync API中被调用两次

Android onSyncStatusChange在Dropbox Sync API中被调用两次,android,dropbox,dropbox-api,Android,Dropbox,Dropbox Api,我已经成功地在我的应用程序中实现了Dropbox的同步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_

我已经成功地在我的应用程序中实现了Dropbox的同步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,它将加载文件的内容。我可以添加/更新其内容并将其保存在我的dropbox帐户中。保存后,它多次调用
onSyncStatusChange()
方法


为什么会这样

如果您从
onSyncStatusChange
记录实际状态,我想每次更改的内容都会很清楚


我的猜测是,当状态更改为上载时会调用一次,然后在上载完成时再次调用。因此,如果您记录
fs.getSyncStatus().upload.inProgress
,您将看到它先转为true,然后再转为false,这是有意义的。

我可以知道下选的原因吗?日志的格式是true,true,然后是false,false,这没有多大意义。@Harish然后检查同步状态的其他组件。我相信你总会看到一些变化(即使不是
上传
字段)。我想不出任何其他与我相关的字段。我正在创建一个文件,想知道该文件何时实际上载到上载。当设备连接时,它会立即同步,但当设备没有任何网络连接时,它会在数据连接激活时同步。我正在努力捕捉这个事件,有什么帮助吗?我试图解释为什么你可能在连续的通话中看到相同的值。(这可能意味着另一个字段正在更改。)要回答您的问题,如果您启动应用程序并看到
上传。inProgress
true
,这意味着即将发生一个挂起的上传。然后,当您看到
false
时,您就知道上载已经完成。