Mariuxtheone / Teleport

Data Sync & Messaging Library for Android Wear
370 stars 47 forks source link

How to reuse a method with Teleport #19

Closed kla4dj closed 9 years ago

kla4dj commented 9 years ago

Hi,

I want to send data when pressing a button from a wear to a handheld. With the following code it works the first time, but I have no idea how to reset the method. What do I have to code if I want to use the string several times?

public void submit(View v) {

    String name = "2";

    //set the AsyncTask to execute when the Data is Synced
    mTeleportClient.setOnSyncDataItemTask(new ShowToastOnSyncDataItemTask());

    //Let's sync a String!
    mTeleportClient.syncString("name", name.toString());

}

and the method:

//Task to show the String from DataMap with key "string" when a DataItem is synced
public class ShowToastOnSyncDataItemTask extends TeleportClient.OnSyncDataItemTask {

    protected void onPostExecute(DataMap dataMap) {

        String s = dataMap.getString("name");

        Toast.makeText(getApplicationContext(),"DataItem - " +s,Toast.LENGTH_SHORT).show();

        mTeleportClient.setOnSyncDataItemTask(new ShowToastOnSyncDataItemTask());
    }
}
youssefhg commented 9 years ago

From What I understand the data API detects changes in the data items. So one way to make sure you always get the notifications is to also send the timestamp as a Data Item along with your data. Hope this works.

kla4dj commented 9 years ago

thank you @youssefhg! that helped! had the same idea about making the data item unique, but thought that i am just using the library wrong :-) btw. really nice work!