【问题标题】:GoogleApiClient onConnected never called on Wearable deviceGoogleApiClient onConnected 从未在可穿戴设备上调用
【发布时间】:2014-07-10 16:25:07
【问题描述】:

我有一个可穿戴设备,我正在尝试连接到 GoogleApiClient,但从未调用回调(onConnected、onConnectionSuspended 或 onConnectionFailed)。其他一切工作正常,DataLayerListenerService 能够从手持设备接收消息,并且在连接时调用 onPeerConnected。我在模拟器和三星 Gear Live 设备上都试过了。这是我在尝试连接到 GoogleApiClient 的 Activity 中的代码。

public class WearReaderActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    public static final String CONTENT_EXTRA = "contentExtra";
    private String LOG_TAG = WearReaderActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reader);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("Connected?", String.valueOf(mGoogleApiClient.isConnected()));
        //new Thread(new GetContent()).start();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d("Connected", "Connected");
        new Thread(new GetContent()).start();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d("Connection suspened", "Connection suspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d("Connection suspened", "Connection suspended");
    }
...
}

不确定它是否有帮助,但这是我的可穿戴应用清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="my.packagename">
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

    <activity
        android:name=".WearReaderActivity"
        android:label="Reading" >
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

<meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version" />

    <service
        android:name=".DataLayerListenerService" >
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>

</application>

有什么想法吗?

编辑:将以下内容添加到可穿戴清单中,但仍然无法正常工作

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

【问题讨论】:

    标签: android google-api-client wear-os


    【解决方案1】:

    哇哦,答案简单得令人尴尬。我错过了onStart() 中您需要致电mGoogleApiClient.connect() 的部分。

    【讨论】:

    • 当文档“课程”忽略提及这一点时,这并不令人尴尬。
    • 正是我在想 iforce2d。
    • 我已提出要求在此处修复文档。
    • 文档课程忽略了提及它,因为@iforce2d 指向我们。小心在 onStop() 中调用 mGoogleApiClient.disconnect()。
    【解决方案2】:

    您可以在 onStart 和 onStop 生命周期方法中手动调用连接和断开连接,也可以使用enableAutoManage 功能。

    mCredentialsApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .enableAutoManage(this, this)
                .addApi(Auth.CREDENTIALS_API)
                .build();
    

    【讨论】:

      【解决方案3】:

      此外,我注意到如果您在用户尚未设置 Google Play 时尝试执行 GoogleApiClient,则可能会出现连接冲突。 (我刚刚重置了我的设备,这就是发生的事情)。因此,最好使用

      测试来自 GoogleApiClient 的连接
      mCredentialsApiClient = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
      

      或者在执行任何任务之前检查 mCredentialsApiClient.isConnected()。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多