未从 BroadcastReceiver 接收到 WearableListerService 的消息
Message not received from BroadcastReceiver to WearableListerService
我正在尝试从我的移动应用向可穿戴应用中的服务发送消息。
消息需要从 BroadcastReceiver 发送。在 BroadcastReceiver 中消息发送成功,至少日志中是这么说的,但是没有触发 onMessageReceive 方法。
我不太确定问题出在哪里,我在两个 build.gradle 文件中都有相同的 applicationId,但包名称不同。
在移动清单中,接收方具有以下声明:
<receiver android:name="com.app.mobile.NewAlarm"
android:exported="false">
<intent-filter>
<action android:name="com.example.android.support.wearable.notifications.ACTION_EXAMPLE" />
</intent-filter>
</receiver>
BroadcastReceiver class:
public class TimeAlarm extends BroadcastReceiver {
public static final String TAP_ACTION_PATH = "/tap";
private static final String TAG = "NotificationReceiver";
public void onReceive(Context context, Intent paramIntent) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int cause) {
}
}).build();
mGoogleApiClient.connect();
sendMessage();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void getNodes() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(
new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
HashSet<String> results = new HashSet<String>();
for (Node node : getConnectedNodesResult.getNodes()) {
results.add(node.getId());
Log.d(TAG,node.getId().toString());
}
Log.d(TAG,results.toString());
sendMessageApi(results);
}
}
);
}
private void sendMessageApi(Collection<String> nodes) {
for (String node : nodes) {
Wearable.MessageApi.sendMessage(
mGoogleApiClient, node, TAP_ACTION_PATH, null).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG, "ERROR: failed to send Message: " + sendMessageResult.getStatus());
} else {
Log.e(TAG, "Success!");
}
}
}
);
}
}
private void sendMessage() {
getNodes();
}
}
WearableListenerService 在 Wear 应用程序清单中以这种方式声明:
<service android:name=".DataLayerListenerService" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
最后是 DataLayerListenerService:
public class DataLayerListenerService extends WearableListenerService {
public static final String START_ACTIVITY_PATH = "/notice";
public static final String TAP_ACTION_PATH = "/tap";
private static final String TAG = "DataLayerListenerService";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
Log.d(TAG, "Message Received !!");
if (START_ACTIVITY_PATH.equals(messageEvent.getPath())) {
Log.d(TAG, "Message Received !!");
int vibeTime = 5000;
String strVibeTime = new String(messageEvent.getData());
if (!strVibeTime.equals("") && !strVibeTime.equals("0")) {
vibeTime = (Integer.parseInt(strVibeTime)) * 1000;
}
sleep(1000);
vibrator.vibrate(vibeTime);
} else if (TAP_ACTION_PATH.equals(messageEvent.getPath())) {
Log.d(TAG, "Tapping Received !!");
vibrator.cancel();
}
}
public synchronized void sleep(long msec) {
try
{
wait(msec);
}catch(InterruptedException e){
Log.e(TAG, e.getMessage());
}
}
}
您试图在客户端连接之前发送消息,因此 getConnectedNodesResult.getNodes()
返回一个空列表。
试试这个:
public void onReceive(Context context, Intent paramIntent) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
sendMessage();
}
@Override
public void onConnectionSuspended(int cause) {
}
}).build();
mGoogleApiClient.connect();
}
我正在尝试从我的移动应用向可穿戴应用中的服务发送消息。 消息需要从 BroadcastReceiver 发送。在 BroadcastReceiver 中消息发送成功,至少日志中是这么说的,但是没有触发 onMessageReceive 方法。 我不太确定问题出在哪里,我在两个 build.gradle 文件中都有相同的 applicationId,但包名称不同。 在移动清单中,接收方具有以下声明:
<receiver android:name="com.app.mobile.NewAlarm"
android:exported="false">
<intent-filter>
<action android:name="com.example.android.support.wearable.notifications.ACTION_EXAMPLE" />
</intent-filter>
</receiver>
BroadcastReceiver class:
public class TimeAlarm extends BroadcastReceiver {
public static final String TAP_ACTION_PATH = "/tap";
private static final String TAG = "NotificationReceiver";
public void onReceive(Context context, Intent paramIntent) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int cause) {
}
}).build();
mGoogleApiClient.connect();
sendMessage();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void getNodes() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(
new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
HashSet<String> results = new HashSet<String>();
for (Node node : getConnectedNodesResult.getNodes()) {
results.add(node.getId());
Log.d(TAG,node.getId().toString());
}
Log.d(TAG,results.toString());
sendMessageApi(results);
}
}
);
}
private void sendMessageApi(Collection<String> nodes) {
for (String node : nodes) {
Wearable.MessageApi.sendMessage(
mGoogleApiClient, node, TAP_ACTION_PATH, null).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG, "ERROR: failed to send Message: " + sendMessageResult.getStatus());
} else {
Log.e(TAG, "Success!");
}
}
}
);
}
}
private void sendMessage() {
getNodes();
}
}
WearableListenerService 在 Wear 应用程序清单中以这种方式声明:
<service android:name=".DataLayerListenerService" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
最后是 DataLayerListenerService:
public class DataLayerListenerService extends WearableListenerService {
public static final String START_ACTIVITY_PATH = "/notice";
public static final String TAP_ACTION_PATH = "/tap";
private static final String TAG = "DataLayerListenerService";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
Log.d(TAG, "Message Received !!");
if (START_ACTIVITY_PATH.equals(messageEvent.getPath())) {
Log.d(TAG, "Message Received !!");
int vibeTime = 5000;
String strVibeTime = new String(messageEvent.getData());
if (!strVibeTime.equals("") && !strVibeTime.equals("0")) {
vibeTime = (Integer.parseInt(strVibeTime)) * 1000;
}
sleep(1000);
vibrator.vibrate(vibeTime);
} else if (TAP_ACTION_PATH.equals(messageEvent.getPath())) {
Log.d(TAG, "Tapping Received !!");
vibrator.cancel();
}
}
public synchronized void sleep(long msec) {
try
{
wait(msec);
}catch(InterruptedException e){
Log.e(TAG, e.getMessage());
}
}
}
您试图在客户端连接之前发送消息,因此 getConnectedNodesResult.getNodes()
返回一个空列表。
试试这个:
public void onReceive(Context context, Intent paramIntent) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
sendMessage();
}
@Override
public void onConnectionSuspended(int cause) {
}
}).build();
mGoogleApiClient.connect();
}