简体   繁体   English

Android Google Drive集成-驱动器未连接

[英]Android Google Drive Integration - Drive not connecting

I am new to android and trying to use Google Drive to store and retrieve data.I have write a code to connect google drive. 我是Android的新手,正尝试使用Google云端硬盘存储和检索数据。我已经编写了连接google云端硬盘的代码。 It is showing account chooser dialog and on selecting of account nothing is happening. 它显示了帐户选择器对话框,并且在选择帐户时没有任何反应。

public class SyncActivity extends MainActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener
{
    GoogleApiClient googleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sync);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        Button btnConnectDrive = (Button)findViewById(R.id.connectDrive);
        btnConnectDrive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    googleApiClient.connect();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onConnected(Bundle bundle) {
        super.onConnected(bundle);
        System.out.println("Connected!!!!!!!!!!!!!!!");
    }

    @Override
    public void onConnectionSuspended(int i) {
        super.onConnectionSuspended(i);
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        super.onConnectionFailed(connectionResult);
        if(connectionResult.hasResolution()){
            try {
                connectionResult.startResolutionForResult(this, ConnectionResult.RESOLUTION_REQUIRED);
            } catch (IntentSender.SendIntentException e) {
                // Unable to resolve, message user appropriately
                e.printStackTrace();
            }
        }
        else {
            GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
        }
    }
}

There's a bunch of steps you need to follow like setup SHA1 fingerprint and obtaining credentials from your Google Dev Console. 您需要遵循一系列步骤,例如设置SHA1指纹和从Google Dev Console获取凭据。 Follow the Steps indicated in Android Quickstart , the code snippets are provided too. 按照Android快速入门中指示的步骤进行操作,还提供了代码段。

If you want a quick code reference, download the Android Demo for Drive API . 如果您想快速参考代码,请下载Drive APIAndroid演示 I was able to run it on my Android device. 我可以在我的Android设备上运行它。

Here's a snippet on the Account chooser: 以下是帐户选择器的摘要:

/**
     * Called when an activity launched here (specifically, AccountPicker
     * and authorization) exits, giving you the requestCode you started it with,
     * the resultCode it returned, and any additional data from it.
     * @param requestCode code indicating which activity result is incoming.
     * @param resultCode code indicating the result of the incoming
     *     activity result.
     * @param data Intent (containing result data) returned by incoming
     *     activity result.
     */
    @Override
    protected void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            case REQUEST_GOOGLE_PLAY_SERVICES:
                if (resultCode != RESULT_OK) {
                    mOutputText.setText(
                            "This app requires Google Play Services. Please install " +
                            "Google Play Services on your device and relaunch this app.");
                } else {
                    getResultsFromApi();
                }
                break;
            case REQUEST_ACCOUNT_PICKER:
                if (resultCode == RESULT_OK && data != null &&
                        data.getExtras() != null) {
                    String accountName =
                            data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                    if (accountName != null) {
                        SharedPreferences settings =
                                getPreferences(Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString(PREF_ACCOUNT_NAME, accountName);
                        editor.apply();
                        mCredential.setSelectedAccountName(accountName);
                        getResultsFromApi();
                    }
                }
                break;
            case REQUEST_AUTHORIZATION:
                if (resultCode == RESULT_OK) {
                    getResultsFromApi();
                }
                break;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM