2

当从网络更改该文件时,我想获取文件或文件夹 ID。我浏览了文档监听更改事件 Git Hub 演示 但我无法实现它。 在我的应用程序中,我正在尝试收听更改并将这些更改也修改为本地文件(同步)。

在这个测试代码中,我只是创建一个文件夹并将一个监听器附加到该文件夹​​。但是当我从 web Listner 重命名文件夹时不听为什么?

1>我试过这个代码

DriveId ddid = sky.getDriveFolder().getDriveId();
        DriveFolder df = Drive.DriveApi.getFolder(mGoogleApiClient, ddid);
        df.addChangeListener(getGoogleApiClient(), changeListener2);

2> 还有这条线

 DriveFolderResult sky = Drive.DriveApi.getRootFolder(getGoogleApiClient())
            .createFolder(getGoogleApiClient(), changeSet).await();
 sky.getDriveFolder().addChangeListener(getGoogleApiClient(), changeListener2);

类文件。

public class MainActivity extends Activity implements ConnectionCallbacks,
    OnConnectionFailedListener {


private static final int REQUEST_CODE_CREATOR = 2;
private static final int REQUEST_CODE_RESOLUTION = 3;
private static final int PICKFILE_RESULT_CODE = 1;


private ContentsResult contentsresult;
private GoogleApiClient mGoogleApiClient;

String EXISTING_FILE_ID = "";
int folderCreated = 0;
SharedPreferences prefs;
ArrayList<String> dbfileid = new ArrayList<String>();
ArrayList<String> dbfilename = new ArrayList<String>();

String fdd="";
DriveFolderResult sky;
private DriveId mFolderDriveId;
String isfolder;
SharedPreferences sp;
String Shared="Shared";
String folderid="";

 int j=0;
 String songfileid="";


    String realid ="";


@Override
protected void onResume() {
    super.onResume();

    initDrive();

}

private void initDrive() {
            if (mGoogleApiClient == null) {

        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(com.google.android.gms.drive.Drive.API)
                .addScope(com.google.android.gms.drive.Drive.SCOPE_FILE).setAccountName("shivrajp130@gmail.com")
                .addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    }

    mGoogleApiClient.connect();

}


@Override
public void onConnectionFailed(ConnectionResult result) {
    // Called whenever the API client fails to connect.

    if (!result.hasResolution()) {
        // show the localized error dialog.
        showToast("Error in on connection failed");
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                0).show();
        return;
    }

    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (SendIntentException e) {
        showToast("error" + e.toString());

    }
}

@Override
public void onConnected(Bundle connectionHint) {

    showToast("Inside Connected");
     sp = getSharedPreferences(Shared, Context.MODE_PRIVATE);


    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            showToast("Thread Started");

            createSkyFolder();
        }
    });

     t.start();

}



private void createSkyFolder()
{
    // TODO Auto-generated method stub

    try
    {
      showToast("creating Folder");

     if(!sp.getString(isfolder, "false").contains("created"))
     {

        MetadataChangeSet changeSet = new MetadataChangeSet.Builder().
                                        setTitle("Sky folder").build();

        sky = Drive.DriveApi.getRootFolder(getGoogleApiClient())
            .createFolder(getGoogleApiClient(), changeSet).await();

        sky.getDriveFolder().addChangeListener(getGoogleApiClient(), changeListener2);

        showToast("folder created");

        sp.edit().putString(isfolder, "created").commit();

    //  To store secret ID string of file or folder so that we can later get a DriveId object.

         realid = sky.getDriveFolder().getDriveId().encodeToString();

         sp.edit().putString(folderid, realid).commit();
         showToast("Real== "+realid);


     }

    Status s = Drive.DriveApi.requestSync(mGoogleApiClient).await();
         if(s.isSuccess())
            {
                showToast("Success");
            }
   }

@Override
protected void onActivityResult(final int requestCode,
        final int resultCode, final Intent data) {
    if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
        mGoogleApiClient.connect();
        showToast("Connected");
    }
}

@Override
protected void onPause() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onPause();
}




public void showToast(final String toast) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), toast,
                    Toast.LENGTH_SHORT).show();
        }
    });
}




public GoogleApiClient getGoogleApiClient() {
    return mGoogleApiClient;
}






    final private Listener<ChangeEvent> changeListener2 = new Listener<ChangeEvent>() {
        @Override
        public void onEvent(ChangeEvent event) {
            showToast("Listening now");
            showToast(String.format("The New Id Is    %s", event.hasMetadataChanged()));
        }
    };



@Override
public void onConnectionSuspended(int cause) {
    showToast("GoogleApiClient connection suspended");
}
}

请提供相同的任何代码片段。

4

0 回答 0