如何建立Google客户端api连接?

时间:2017-06-30 07:12:46

标签: android android-googleapiclient

我在创建google api客户端连接首选项屏幕时遇到了问题。 google api客户端在oncreateView()方法上成功连接,但我的问题是google api客户端必须连接,当google api上的首选项开关应该连接时。

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

当它放在oncreateView()中时,上面的工作正常。

    record.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValues) {

                if (((Boolean) newValues)) {
 mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

                    Toast.makeText(getActivity(), "Connection On", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), "Connection OFF", Toast.LENGTH_SHORT).show();
                }

                return true;
            }
        });

在上面的代码中,google api客户端连接被置于首选项开关内,工作异常正在发生。请帮我解决这个问题。

单击首选项开关时会显示此异常

 E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
06-30 12:45:59.810 4029-4029/com.seyali.callLog E/MessageQueue-JNI: java.lang.ClassCastException: com.seyali.callLog.SettingsActivity cannot be cast to com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
                                                                        at com.seyali.callLog.PreferenceFragments$4.onPreferenceClick(PreferenceFragments.java:233)
                                                                        at android.preference.Preference.performClick(Preference.java:996)
                                                                        at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:249)

1 个答案:

答案 0 :(得分:0)

在此类活动中实施 GoogleApiClient.ConnectionCallbacks GoogleApiClient.OnConnectionFailedListener

public class MainActivity extends Activity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {}