【问题标题】:onSyncStatusChange is called twice in Dropbox Sync APIonSyncStatusChange 在 Dropbox Sync API 中被调用两次
【发布时间】:2014-05-29 12:47:29
【问题描述】:

我已成功在我的应用中实现了 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() 方法。

为什么会这样?

【问题讨论】:

  • 我可以知道投反对票的原因吗?

标签: android dropbox dropbox-api


【解决方案1】:

如果您从onSyncStatusChange 记录实际状态,我想每次都会发生什么变化。

我的猜测是当状态更改为上传时调用一次,然后在上传完成时再次调用。所以如果你登录fs.getSyncStatus().uploading.inProgress,你会看到它翻转为真,然后又变回假,这是有道理的。

【讨论】:

  • 日志以真、真、假、假的形式出现,这没有多大意义。
  • @Harish 然后检查同步状态的其他组件。我相信您总会看到一些变化(即使不是 uploading 字段)。
  • 我想不出任何其他与我相关的领域。我正在创建一个文件,想知道文件何时实际上传到上传。当设备连接但设备没有任何网络连接时,它会在数据连接变为活动状态时同步。我正在努力捕捉这个事件,有什么帮助吗?
  • 我试图解释为什么您可能会在连续调用中看到相同的值。 (这可能意味着另一个字段正在发生变化。)要回答您的问题,如果您启动应用程序并将uploading.inProgress 视为true,这意味着即将发生待处理的上传。然后看到false就知道上传完成了。
猜你喜欢
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多