当我读到Activity
和Service
之间的沟通时,我发现我们可以使用
我对前两个感兴趣。因此,当我尝试实现此功能以在Activity
和WearableListenerService
之间进行通信时,我需要覆盖onBind
函数。
但是,我收到编译错误说
无法覆盖最终方法" onBind"
当我使用普通Service
时,我不会收到这样的错误。所以,
1。这是否意味着我们无法使用IBinder
或Messenger
方法与WearableListenerService
中的Activity
进行通信?
2. 如果是,那么从WearableListenerService
向Activity
传递邮件的下一个最佳方法是什么(或从活动中调用该服务的公共方法) ?
答案 0 :(得分:14)
经过一番挖掘后,我找到了解决方案。希望它能帮助别人。
我们可以使用WearableListenerService
函数将活动信息发送到Wearable.MessageApi
。
当Activity
和WearableListenerService
位于同一节点(设备)上时,我们需要获取本地节点的实例(从中发送消息的当前节点)发送消息如下
NodeApi.GetLocalNodeResult nodes = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
而不是
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
用于获取连接到手机的其他设备(如磨损)的列表。
所以,我能够成功地从我的Activity向WearableListenerService发送消息,如下所示
活动代码
public class PhoneActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final String TAG = "PhoneActivity";
public static final String CONFIG_START = "config/start";
public static final String CONFIG_STOP= "config/stop"
Intent intent;
TextView txtview;
GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone);
if(null == mGoogleApiClient) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Log.v(TAG, "GoogleApiClient created");
}
if(!mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
Log.v(TAG, "Connecting to GoogleApiClient..");
}
startService(new Intent(this, PhoneService.class));
}
@Override
public void onConnectionSuspended(int cause) {
Log.v(TAG,"onConnectionSuspended called");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v(TAG,"onConnectionFailed called");
}
@Override
public void onConnected(Bundle connectionHint) {
Log.v(TAG,"onConnected called");
}
@Override
protected void onStart() {
super.onStart();
Log.v(TAG, "onStart called");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.phone, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_start_) {
new SendActivityPhoneMessage(CONFIG_START,"").start();
}else if (id == R.id.action__stop) {
new SendActivityPhoneMessage(CONFIG_STOP,"").start();
}else if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class SendActivityPhoneMessage extends Thread {
String path;
String message;
// Constructor to send a message to the data layer
SendActivityPhoneMessage(String p, String msg) {
path = p;
message = msg;
}
public void run() {
NodeApi.GetLocalNodeResult nodes = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
Node node = nodes.getNode();
Log.v(TAG, "Activity Node is : "+node.getId()+ " - " + node.getDisplayName());
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await();
if (result.getStatus().isSuccess()) {
Log.v(TAG, "Activity Message: {" + message + "} sent to: " + node.getDisplayName());
}
else {
// Log an error
Log.v(TAG, "ERROR: failed to send Activity Message");
}
}
}
}
服务代码
public class PhoneService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final String TAG = "PhoneService";
public static final String CONFIG_START = "config/start";
public static final String CONFIG_STOP = "config/stop";
GoogleApiClient mGoogleApiClient;
public PhoneService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "Created");
if(null == mGoogleApiClient) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Log.v(TAG, "GoogleApiClient created");
}
if(!mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
Log.v(TAG, "Connecting to GoogleApiClient..");
}
}
@Override
public void onDestroy() {
Log.v(TAG, "Destroyed");
if(null != mGoogleApiClient){
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
Log.v(TAG, "GoogleApiClient disconnected");
}
}
super.onDestroy();
}
@Override
public void onConnectionSuspended(int cause) {
Log.v(TAG,"onConnectionSuspended called");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v(TAG,"onConnectionFailed called");
}
@Override
public void onConnected(Bundle connectionHint) {
Log.v(TAG,"onConnected called");
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
Log.v(TAG, "Data Changed");
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
if(messageEvent.getPath().equals(CONFIG_START)){
//do something here
}else if(messageEvent.getPath().equals(CONFIG_STOP)){
//do something here
}
}
@Override
public void onPeerConnected(Node peer) {
super.onPeerConnected(peer);
Log.v(TAG, "Peer Connected " + peer.getDisplayName());
}
@Override
public void onPeerDisconnected(Node peer) {
super.onPeerDisconnected(peer);
Log.v(TAG, "Peer Disconnected " + peer.getDisplayName());
}
}