Android Wear - Gradle dependency issues “Cannot re

2019-07-29 14:24发布

问题:

I followed the instructions here to build a simple app with mobile and wear components in Android Studio. I have the following code in my mobile activity, trying to call MyWearActivity which is in wear module:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Intent intent = new Intent(this, MyWearActivity.class);
    startActivity(intent);
}

My mobile build.gradle file has the following dependencies:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services-wearable:+'
compile 'com.google.android.support:wearable:+'
}

However, I am getting the following error "Cannot resolve symbol MyWearActivity', trying to run the "mobile" run configuration from Android Studio. I would think the wearApp project(':wear') bit would take care of the mobile dependency on wear but it seems not. Can anyone identify where I'm going wrong? Thanks

回答1:

You should instead use the MessageAPI to send a message to your Wear device. You can learn how to do so here: https://developer.android.com/training/wearables/data-layer/index.html and here is an example (assuming you've already created and connected a GoogleApiClient)

private void fireMessage(final String message) {
    // Send the RPC
    PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(apiClient);
    nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(NodeApi.GetConnectedNodesResult result) {
            for (int i = 0; i < result.getNodes().size(); i++) {
                Node node = result.getNodes().get(i);
                String nName = node.getDisplayName();
                String nId = node.getId();
                Log.d(TAG, "Node name and ID: " + nName + " | " + nId);

                PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(apiClient, node.getId(),
                        message, null);
                messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                        Status status = sendMessageResult.getStatus();
                        Log.d(TAG, "Status: " + status.toString() + " Msg: " + message);
                        if (status.getStatusCode() != WearableStatusCodes.SUCCESS) {
                            //alertButton.setProgress(-1);
                            Toast.makeText(getApplicationContext(), "Tap to retry. Alert not sent :(", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }
    });
}

Then, in your wear module, you should implement a listener, or better yet, a WearableListenerService with an onMessageReceived method to respond to that message. For example, you could have that service fire an intent based on the message to start an Activity, or build a notification to display on the device. Here is a guide to building notifications https://developer.android.com/training/wearables/notifications/index.html



回答2:

No, think of the Wear module and the Mobile module as two completely different apks or apps (because they are). You do not directly call to the activities in the Wear module from the Mobile app. The wear app is started through user interaction. What exactly are you trying to do?

Ok, based on your comment below, the problem is still that you can't call a WearActivity from the Mobile app since that is in a different library/app that is not linked together. The example code from above in the answer from Nickjm is a good start.