Background
At the beginning of the author in the study of data transmission and reception, see Wear data Class Datamap in addition to the put basic type, there is a Frombundle method to build a Datamap object. So in a breath of the original function of the serialized object passed through, the end of the watch has not received the message.
Cause of the problem
Then we looked at the developer documentation and found that the bundle was not as powerful as the data we passed in the intent, and it doesn't support serialization! A description of the developer's documentation is attached first:
Returns a DataMap from a Bundle. The input Bundle is expected to contain only elements supported by DATAMAP. Any elements in the Bundle isn't supported by DataMap would be dropped.
Http://developer.android.com/reference/com/google/android/gms/wearable/DataMap.html#fromBundle ( Android.os.Bundle)
The key phrase is "the input Bundle is expected to contain only elements supported by DataMap" and only the types supported by DATAMAP itself can be passed to the bundle, so that means we A data bean that is built on its own cannot be serialized directly into the bundle, which results in the result that the "any elements in the bundle isn't supported by DataMap would be dropped", meaning that the data is not passed, but is Abandon.
Here's the problem.
So, here's the problem! How can you gracefully pass a data bean?
Next, I will give my own solution.
Two. Solution1. Use of public works
- Create Public works: Because data is shared between phone engineering and wear engineering , the best way to avoid owning data classes in both projects is to naturally pull this part out. After creation, tick "is library", set public works as library project.
- Import Public Works:Phone engineering and wear engineering all cite public works.
Public Works templates: use one city data as an example below. Note: To use the Datamap class, you must refer to the Googleplayservices above version 5.0.
import Com.google.android.gms.wearable.DataMap; Public classWearcitybean { Public StaticFinal String KEY ="/bean/wear/city";Private StaticFinal String m_city_id ="Mcityid";Private StaticFinal String M_city_name ="Mcityname";Private StaticFinal String M_country_name ="Mcountryname";//City IDPrivateString Mcityid;//City NamePrivateString Mcityname;//Country namePrivateString Mcountryname; PublicWearcitybean () {} PublicWearcitybean (DataMap map) {Setcityid (map.getstring (m_city_id)); Setcityname (map.getstring (m_city_name)); Setcountryname (map.getstring (M_country_name));} PublicDataMap Getdatamap () {DataMap map=NewDataMap (); Map.putstring (m_city_id, Mcityid); Map.putstring (M_city_name, mcityname); Map.putstring (M_country_name, mcountryname); returnmap;} PublicString Getcityid () {returnMcityid;} Public voidSetcityid (String cityid) {Mcityid=Cityid;} PublicString Getcityname () {returnMcityname;} Public voidsetcityname (String cityname) {mcityname=CityName;} PublicString Getcountryname () {returnMcountryname;} Public voidsetcountryname (String countryname) {mcountryname=CountryName;} @Override PublicString toString () {return "Wearcitybean [mcityid="+ Mcityid +", Mcityname="+Mcityname+", Mcountryname="+ Mcountryname +"]";}}
Critical code Analysis-send data: One end of the data to be sent, a Datamap object is passed into the method, and the basic data of the bean itself is passed to the Datamap. In this way, the end of the data to be sent without a lot of scattered code, and if the phone and wear to achieve data transmission, there is no need to repeat two code.
Public DataMap Getdatamap () { new DataMap (); Map.putstring (m_city_id, Mcityid); Map.putstring (M_city_name, mcityname); Map.putstring (M_country_name, mcountryname); return map;}
Send-side code: The following initializes a Wearcitybean object, then obtains the Datamap object from it, and finally puts it into the data request object Putdatamaprequest, and completes the package of the send.
Private void Packagecitydata (putdatamaprequest dataMap) {// build and initialize data objects through Getwearcitybean () Wearcitybean Wearcitybean == wearcitybean.getdatamap ();d atamap.getdatamap (). Putdatamap ( Wearcitybean.key, map);}
Critical code Analysis-Receive data: The received Datamap is used to build a Wearcitybean object to complete the parsing of the data.
Public Wearcitybean (DataMap map) { Setcityid (map.getstring (m_city_id)); Setcityname (map.getstring (M_city_name)); Setcountryname (map.getstring (M_country_name));}
The code on the receiving side is too simple to be attached.
something
- The above is the author in the API enough to meet the needs of the situation to come up with a set of solutions, after many tests, run well.
- In addition to directly passing the basic data type into DATAMAP, you can encapsulate the data in JSON or other ways into a string and then into the Datamap. This is also a good solution.
Android Wear development-Data Communication-section fourth: Data encapsulation (troubleshooting not serializable issues)