Reputation: 685
I'm learning data communication in android wear. My understanding is that both mobile and wear apps need to connect to DataItem via Google Services API in order to read data from the one or the other.
I have data saved in sharedpreference in the mobile app. Only when I open my wear app, I want to read the data from sharedpreference in the mobile app to display on the wear.
Would it be like, whenever the mobile app updates this data in sharedpreference, have that activity connected to Google Services API and put a request in DataItem. Then the wear app would be listening to changes by WearableListenerService?
I prefer not to have service running the entire time at least not on the mobile side. What would be an approach to accomplish this?
Upvotes: 0
Views: 513
Reputation: 979
for data transfer you can use the library Emmet
https://github.com/florent37/emmet
We can imagine a protocol like this
public interface SmartphoneProtocole{
void getStringPreference(String key);
void getBooleanPreference(String key);
}
public interface WearProtocole{
void onStringPreference(String key, String value);
void onBooleanPreference(String key, boolean value);
}
wear/WearActivity.java
//access "MY_STRING" sharedpreference
SmartphoneProtocole smartphoneProtocol = emmet.createSender(SmartphoneProtocole.class);
emmet.createReceiver(WearProtocole.class, new WearProtocole(){
@Override
void onStringPreference(String key, String value){
//use your received preference value
}
@Override
void onBooleanPreference(String key, boolean value){
}
});
smartphoneProtocol.getStringPreference("MY_STRING"); //request the "MY_STRING" sharedpreference
mobile/WearService.java
final WearProtocole wearProtocol = emmet.createSender(WearProtocole.class);
emmet.createReceiver(SmartphoneProtocol.class, new SmartphoneProtocol(){
//on received from wear
@Override
void getStringPreference(String key){
String value = //read the value from sharedpreferences
wearProtocol.onStringPreference(key,value); //send to wear
}
@Override
void getBooleanPreference(String key){
}
});
Upvotes: 0
Reputation: 4835
That would be the approach to take but to save you the hassle implementing it there's already a library that does this.
Upvotes: 0