sqlite - Using existing database in Android Wear -
i'm try build wear-app existing app. have sqlite database in handheld-app, want try use them in wear app.
is possibility send database wear or can access database on handheld wear app?
my current idea transfer items via wearable.dataapi, that's sounds not best solution.
for example, don't believe google keep transfer notes separately.
anyone idea?
i've found quick solution transfering whole database phone smartwatch.
first create helper class converts database content json-string, can send smartwatch using wearable.dataapi:
databasetojson.java:
public class databasetojson { databasehandler dbhandler; public databasetojson(context context) { dbhandler = new databasehandler(context); } public jsonobject getjson() throws jsonexception{ item[] item = null; jsonobject pl = new jsonobject(); item = dbhandler.getitems(); dbhandler.close(); jsonarray jsonarray = new jsonarray(); for(int i=0;i<item.length;i++){ jsonobject val = new jsonobject(); try { val.put("id", item[i].getid()); val.put("name", item[i].getname()); ... jsonarray.put(val); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } pl.put(string.valueof(j), jsonarray); } if(jsonarray.length()<1){ pl.put(string.valueof(j),new jsonarray()); } } jsonobject result = new jsonobject(); result.put("data",pl); return result; } }
demoactivity.java (phone):
public class demoactivity extends activity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener { /** android wear **/ googleapiclient googleclient; @override public void onstart(){ super.onstart(); googleclient.connect(); } @override public void onstop(){ if (null != googleclient && googleclient.isconnected()) { googleclient.disconnect(); } super.onstop(); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); googleclient = new googleapiclient.builder(this) .addapi(wearable.api) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); ... } @override public void onconnected(bundle bundle) { databasetojson dbjson = new databasetojson(demoactivity.this); try { jsonobject json = dbjson.getjson(); new sendtodatalayerthread("/path", json.tostring()).start(); } catch (jsonexception e) { e.printstacktrace(); } } class sendtodatalayerthread extends thread { string path; string message; sendtodatalayerthread(string p, string msg) { path = p; message = msg; } public void run() { nodeapi.getconnectednodesresult nodes = wearable.nodeapi.getconnectednodes(googleclient).await(); (node node : nodes.getnodes()) { messageapi.sendmessageresult result = wearable.messageapi.sendmessage(googleclient, node.getid(), path, message.getbytes()).await(); if (result.getstatus().issuccess()) { log.v("mytag", "message: {" + message + "} sent to: " + node.getdisplayname()); } else { log.v("mytag", "error: failed send message"); } } } } @override public void onconnectionsuspended(int i) { } @override public void onconnectionfailed(connectionresult connectionresult) { } }
datalayerlistenerservice.java (wear)
public class datalayerlistenerservice extends wearablelistenerservice { @override public void onmessagereceived(messageevent messageevent) { if (messageevent.getpath().equals("/path")) { final string message = new string(messageevent.getdata()); // want json-string sharedpreferences pref = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); sharedpreferences.editor edit = pref.edit(); edit.putstring("demo_json",message).apply(); } else { super.onmessagereceived(messageevent); } }
add androidmanifest.xml (wear)
<service android:name=".datalayerlistenerservice" > <intent-filter> <action android:name="com.google.android.gms.wearable.bind_listener" /> </intent-filter> </service>
after receiving json-string on wear can save them within database on wear or else it...
i think thats simplest way transfer such data between handheld , wear device.
Comments
Post a Comment