可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I followed the tutorial to invoke a Wearable activity from phone, posted here How to send notification from handheld to wear to open Activity on wear device. However, I grab the source code from the answer. However, I couldn't get it running. It looks like onDataChanged()
is never called. I ask this as a question on its own because it seems the example works for others. I'm on KitKat 4.4.2, if that matters.
Any tip where to check, thanks.
回答1:
There are two things you can check:
packageName
of the wear app should be identical to packageName
of the device app onDataChanged()
gets only called when the data really changes. If you put the same data into the DataApi multiple times, the method is only called once until you write different data. You could add a timestamp to the data to make sure that the method gets called once for each write operation. However, using the DataApi is potentially more expensive in terms of battery usage than sending a message. So if you just want to trigger an action after putting data into the DataApi, simply send a message when you are done.
回答2:
With version 8.3.0 of the Play Services the messages could be delayed up to 30 minutes. With the new setUrgent() method it is send without a delay.
Finally, if you are developing for wearables, you’ll know that battery life and optimization of power usage are critical in having a great user experience. With Google Play services 8.3, we’ve updated the DataApi to allow for urgency in how data items are synced. Now, a priority can be added to the data item to determine when it should be synced. For example, if you are building an app that requires immediate syncing, such as a remote control app, it can still be done immediately by calling setUrgent(), but for something such as updating your contacts, you could tolerate some delay. Non-urgent DataItems may be delayed for up to 30 minutes, but you can expect that in most cases they will be delivered within a few minutes. Low priority is now the default, so setUrgent() is needed to obtain the previous timing.
http://android-developers.blogspot.nl/2015/11/whats-new-in-google-play-services-83.html
回答3:
make sure that the applicationId in the build.gradle file ist the same for your handheld and your wearable module
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "your.applicationid" // needs to be the same in both modules minSdkVersion 20 targetSdkVersion 23 versionCode 1 versionName "1.0" targetCompatibility = '1.7' } ...
回答4:
For me, the culprit was this line in the service declaration:
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
I'm not exactly sure what about this line causes the service not to receive events, but when it was removed it started functioning properly.
回答5:
1.check build.gradle for mobile and for wear to have the same play version compile 'com.google.android.gms:play-services-wearable:6.5.87'
Mine has a + sign on mobile and it took the last version on mobile only
Upgrade same version on both until it works
2.Stupid workaround, if nothing works (!! this works with different play versions on pairs, if you do step one you do not need this)
.... Wearable.DataApi.putDataItem(mGoogleApiClient, request) ....... this line after your put Wearable.DataApi.deleteDataItems(mGoogleApiClient, request.getUri());
It is a replication trick that do 3 things :
My problem was that data was queued and paused on watch until first touch - now it is real time bidirectional
回答6:
If you are using Android Wear emulator than be sure that Android Wear app on the phone has Connected to the emulator and you run adb tcp forwarding command.
Additional details here: https://stackoverflow.com/a/25506889/1044864
回答7:
after onDataChanged method, I am forcing
onDataChanged(DataEventBuffer dataEvents) {
--doDataStuff---
mGoogleApiClient.disconnect(); mGoogleApiClient.connect();
}
only with this setting the onDataChanged is working for me.