0
按下磨損應用程序上的按鈕,它會向手機發送消息。它通過移動設備上運行的服務接收消息。但是,當從移動設備發回應答消息時,似乎不起作用。我正在使用來自(https://github.com/Mariuxtheone)的庫。Android Wear,發送服務響應
WEAR主要活動
import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.wearable.DataMap;
import com.mariux.teleport.lib.TeleportClient;
import com.mariux.teleport.lib.TeleportService;
public class MainActivity extends Activity {
private static final String LOG_TAG = "MainActivity";
private TeleportClient mTeleportClient;
@Override
protected void onStart() {
super.onStart();
mTeleportClient.connect();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTeleportClient = new TeleportClient(this);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTeleportClient.sendMessage("message from wearable", null);
mTeleportClient.syncString("", "");
}
});
mTeleportClient.setOnGetMessageTask(new TeleportClient.OnGetMessageTask() {
@Override
protected void onPostExecute(String path) {
Toast.makeText(MainActivity.this, "setOnGetMessageTask#onPostExecute("+path+")", Toast.LENGTH_SHORT).show();
}
});
mTeleportClient.setOnSyncDataItemTask(new TeleportClient.OnSyncDataItemTask() {
@Override
protected void onPostExecute(DataMap result) {
Toast.makeText(MainActivity.this, "setOnSyncDataItemTask#onPostExecute("+result.toString()+")", Toast.LENGTH_SHORT).show();
}
});
mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());
mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());
}
@Override
protected void onStop() {
mTeleportClient.disconnect();
super.onStop();
}
// public class GetMessageCallback extends TeleportClient.OnGetMessageCallback {
//
// @Override
// public void onCallback(String dataMap) {
// switch (dataMap) {
// case "hello": {
// Toast.makeText(getApplicationContext(), "GETSTRTING", Toast.LENGTH_SHORT).show();
// //mTeleportClient.syncString("hello", "Hello, World!");
// mTeleportClient.sendMessage("hello2", null);
//
// }
// }
// }
// }
//
//
// public class ShowToastHelloWorldTask extends TeleportClient.OnSyncDataItemTask {
//
// @Override
// protected void onCancelled() {
// super.onCancelled();
// Log.e(LOG_TAG, "onCancelled()");
// }
//
// @Override
// protected void onPostExecute(DataMap dataMap) {
// Log.e(LOG_TAG, "onPostExecute()");
//
// String hello = dataMap.getString("hello");
// Toast.makeText(getApplicationContext(), hello, Toast.LENGTH_SHORT).show();
//
//
// mTeleportClient.setOnSyncDataItemTask(new ShowToastHelloWorldTask());
//
// }
//
//
// }
public class OnSyncDataItemCallback extends TeleportClient.OnSyncDataItemCallback {
@Override
public void onDataSync(DataMap dataMap) {
Log.i(LOG_TAG, "onDataSync()");
Toast.makeText(MainActivity.this, "DataMap dataMap", Toast.LENGTH_SHORT).show();
mTeleportClient.sendMessage("message from wearable", null);
mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());
}
}
public class OnGetMessageCallback extends TeleportClient.OnGetMessageCallback {
@Override
public void onCallback(String message) {
Log.i(LOG_TAG, "onCallback()");
Toast.makeText(MainActivity.this, "GETSTRTING", Toast.LENGTH_SHORT).show();
mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());
}
}
}
MOBILE主要活動
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.mariux.teleport.lib.TeleportClient;
public class MainActivity extends ActionBarActivity {
private TeleportClient mTeleportClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTeleportClient = new TeleportClient(this);
}
@Override
protected void onStart() {
super.onStart();
mTeleportClient.connect();
}
@Override
protected void onStop() {
mTeleportClient.disconnect();
super.onStop();
}
}
移動服務
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.wearable.DataMap;
import com.mariux.teleport.lib.TeleportClient;
import com.mariux.teleport.lib.TeleportService;
/**
* Created by aFrogleap on 7/1/2015.
*/
public class WearService extends TeleportService {
private static final String LOG_TAG = "WearService";
private TeleportClient mTeleportClient;
@Override
public void onStart(Intent intent, int startId) {
Log.i(LOG_TAG, "onStart()");
super.onStart(intent, startId);
mTeleportClient.connect();
mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());
mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());
mTeleportClient.setOnGetMessageTask(new TeleportClient.OnGetMessageTask() {
@Override
protected void onPostExecute(String path) {
Toast.makeText(WearService.this, "setOnGetMessageTask#onPostExecute(" + path + ")", Toast.LENGTH_SHORT).show();
}
});
mTeleportClient.setOnSyncDataItemTask(new TeleportClient.OnSyncDataItemTask() {
@Override
protected void onPostExecute(DataMap result) {
Toast.makeText(WearService.this, "setOnSyncDataItemTask#onPostExecute(" + result.toString() + ")", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCreate() {
Log.i(LOG_TAG, "onCreate()");
super.onCreate();
mTeleportClient = new TeleportClient(this);
//setOnGetMessageCallback(new StartActivityTask());
}
@Override
public void onDestroy() {
Log.i(LOG_TAG, "onDestroy()");
super.onDestroy();
mTeleportClient.disconnect();
}
public class OnSyncDataItemCallback extends TeleportClient.OnSyncDataItemCallback {
@Override
public void onDataSync(DataMap dataMap) {
Log.i(LOG_TAG, "onDataSync()");
Toast.makeText(WearService.this, "DataMap dataMap", Toast.LENGTH_SHORT).show();
mTeleportClient.sendMessage("message from device", null);
mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());
}
}
public class OnGetMessageCallback extends TeleportClient.OnGetMessageCallback {
@Override
public void onCallback(String message) {
Log.i(LOG_TAG, "onCallback()");
switch (message) {
case "hello": {
Toast.makeText(WearService.this, "GETSTRTING", Toast.LENGTH_SHORT).show();
mTeleportClient.sendMessage("message from device", null);
}
mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());
}
}
}
}
已經有人也與這個問題衣食住行?這個問題真的需要一些幫助。