问题描述
当我读到一个活动
和服务
之间的交流,我发现,我们可以使用
- 的IBinder
- 使者
- AIDL
我感兴趣的是前两种。所以,当我试图实现一个这样的活动
和 WearableListenerService
之间的沟通,我需要重写 onBind
的功能。
不过,我得到一个编译器错误说它
我没有得到这样一个错误,当我用普通服务
。因此,
1 这是不是意味着我们不能使用的IBinder
或使者
办法沟通与 WearableListenerService
从活动
?
2 如果是这样,什么是从活动传递消息给
(或调用该服务从活动的公共方法)? WearableListenerService
下一个最好的办法
经过一番挖掘,我找到了解决办法。希望它可以帮助别人。
我们可以从活动中使用 Wearable.MessageApi
功能发送消息给 WearableListenerService
。当活动
和 WearableListenerService
在同一个节点(设备),我们需要的获取实例本地节点(当前节点从该消息发送)来发送消息,如下
NodeApi.GetLocalNodeResult节点= Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
而不是
NodeApi.GetConnectedNodesResult节点= Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
这是用来获得的其他设备(诸如磨损),其连接到手机列表中。
所以,我是能够成功地发送邮件从我的活动,以WearableListenerService如下:
公共类PhoneActivity扩展活动器具
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
私有静态最后字符串变量=PhoneActivity;
公共静态最后弦乐CONFIG_START =配置/启动;
公共静态最后弦乐CONFIG_STOP =配置/停止
意向意图;
TextView的txtview;
GoogleApiClient mGoogleApiClient;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_phone);
如果(空== mGoogleApiClient){
mGoogleApiClient =新GoogleApiClient.Builder(本)
.addApi(Wearable.API)
.addConnectionCallbacks(本)
.addOnConnectionFailedListener(本)
。建立();
Log.v(TAG,GoogleApiClient创建);
}
如果(!mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
Log.v(TAG,连接到GoogleApiClient ..);
}
startService(新意图(这一点,PhoneService.class));
}
@覆盖
公共无效onConnectionSuspended(INT原因){
Log.v(TAG,onConnectionSuspended名为);
}
@覆盖
公共无效onConnectionFailed(ConnectionResult connectionResult){
Log.v(TAG,onConnectionFailed名为);
}
@覆盖
公共无效onConnected(包connectionHint){
Log.v(TAG,onConnected名为);
}
@覆盖
保护无效的OnStart(){
super.onStart();
Log.v(TAG,ONSTART名为);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.phone,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
INT的id = item.getItemId();
如果(ID == R.id.action_start_){
新SendActivityPhoneMessage(CONFIG_START,)开始()。
}否则,如果(ID == R.id.action__stop){
新SendActivityPhoneMessage(CONFIG_STOP,)开始()。
}否则,如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
类SendActivityPhoneMessage继承Thread {
字符串路径;
字符串消息;
//构造以将消息发送到该数据层
SendActivityPhoneMessage(串P,弦乐味精){
路径= P;
消息=味精;
}
公共无效的run(){
NodeApi.GetLocalNodeResult节点= Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
节点node = nodes.getNode();
Log.v(TAG,活动节点是:+ node.getId()+ - + node.getDisplayName());
。MessageApi.SendMessageResult结果= Wearable.MessageApi.sendMessage(mGoogleApiClient,node.getId(),路径,message.getBytes())等待();
如果(result.getStatus()。isSuccess()){
Log.v(TAG,活动消息:{+信息+}发送到:+ node.getDisplayName());
}
其他 {
//记录错误
Log.v(TAG,错误:未能发送活动信息);
}
}
}
}
公共类PhoneService扩展WearableListenerService工具
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
私有静态最后字符串变量=PhoneService;
公共静态最后弦乐CONFIG_START =配置/启动;
公共静态最后弦乐CONFIG_STOP =配置/停止;
GoogleApiClient mGoogleApiClient;
公共PhoneService(){
}
@覆盖
公共无效的onCreate(){
super.onCreate();
Log.v(TAG,创建);
如果(空== mGoogleApiClient){
mGoogleApiClient =新GoogleApiClient.Builder(本)
.addApi(Wearable.API)
.addConnectionCallbacks(本)
.addOnConnectionFailedListener(本)
。建立();
Log.v(TAG,GoogleApiClient创建);
}
如果(!mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
Log.v(TAG,连接到GoogleApiClient ..);
}
}
@覆盖
公共无效的onDestroy(){
Log.v(TAG,破坏);
如果(NULL!= mGoogleApiClient){
如果(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
Log.v(TAG,GoogleApiClient断开);
}
}
super.onDestroy();
}
@覆盖
公共无效onConnectionSuspended(INT原因){
Log.v(TAG,onConnectionSuspended名为);
}
@覆盖
公共无效onConnectionFailed(ConnectionResult connectionResult){
Log.v(TAG,onConnectionFailed名为);
}
@覆盖
公共无效onConnected(包connectionHint){
Log.v(TAG,onConnected名为);
}
@覆盖
公共无效onDataChanged(DataEventBuffer dataEvents){
super.onDataChanged(dataEvents);
Log.v(TAG,数据更改);
}
@覆盖
公共无效onMessageReceived(MessageEvent的MessageEvent事件){
super.onMessageReceived(MessageEvent的);
如果(messageEvent.getPath()。等于(CONFIG_START)){
//做的东西在这里
}否则,如果(messageEvent.getPath()。等于(CONFIG_STOP)){
//做的东西在这里
}
}
@覆盖
公共无效onPeerConnected(节点等){
super.onPeerConnected(同行);
Log.v(TAG,同行保持通话+ peer.getDisplayName());
}
@覆盖
公共无效onPeerDisconnected(节点等){
super.onPeerDisconnected(同行);
Log.v(TAG,同行断开连接的+ peer.getDisplayName());
}
}
When I read about communication between an Activity
and Service
, I found that we can use either
- IBinder
- Messenger
- AIDL
I am interested in the first two. So when I tried implementing this to communicate between an Activity
and WearableListenerService
, I needed to override the onBind
function.
But then, I am getting a compiler error saying it
I dont get such an error when I use a normal Service
. So,
1. Does that mean we cannot use IBinder
or Messenger
approach to communicate with the WearableListenerService
from an Activity
?
2. If so, what is the next best way to pass message to WearableListenerService
from an Activity
(or call a public method of that service from an Activity)?
After some digging, I found the solution. Hope it helps somebody else.
We can send message from an Activity to WearableListenerService
using Wearable.MessageApi
functions.When the Activity
and WearableListenerService
are on the same Node (device), we need to get the instance of the local node (current node from which the message is sent) for sending the message as below
NodeApi.GetLocalNodeResult nodes = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
rather than
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
which is used to get the list of other devices (such as wear) connected to the phone.
So, I was able to successfully send a message from my Activity to WearableListenerService as follows
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());
}
}
这篇关于从活动将数据发送到WearableListenerService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!