对于数据传输,您可以使用库 Emmet
https://github.com/florent37/emmet
我们可以想象这样的协议
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);
}
磨损/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
移动/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){
}
});