我有一个正在运行的智能手机应用程序。我现在要扩展它,以便还有一个可穿戴部件。我试图从多个来源(https://www.binpress.com/tutorial/a-guide-to-the-android-wear-message-api/152和https://developer.android.com/training/building-wearables.html)汇总所有内容,但似乎无法将数据发送到可穿戴设备。
我究竟做错了什么?
这是我到目前为止的代码...
我在电话方面的基本活动:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Android wear support
initGoogleApiClient();
}
// Android wear support
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mApiClient.isConnected());
mApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
sendMessage(START_ACTIVITY, "Test 12345");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mApiClient.isConnected()) {
mApiClient.disconnect();
}
}
private void sendMessage(final String path, final String text) {
new Thread(new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mApiClient).await();
for (Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mApiClient, node.getId(), path, text.getBytes()).await();
}
}
}).start();
}
}
在Android Studio中的可穿戴模块中,我有以下清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.appname.wear">
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.myapp.appname.wear.WearMessageListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
</application>
我的WearMessageListenerService看起来像这样:
public class WearMessageListenerService extends WearableListenerService {
private static final String START_ACTIVITY = "/start_activity";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equalsIgnoreCase(START_ACTIVITY)) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
super.onMessageReceived(messageEvent);
}
}
}
这是我的wear应用程序中的MainActivity.java:
public class MainActivity extends Activity implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks {
private TextView mTextView;
private static final String WEAR_MESSAGE_PATH = "/start_activity";
GoogleApiClient mApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.textView);
}
});
mApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.build();
mApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Wearable.MessageApi.addListener(mApiClient, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onMessageReceived( final MessageEvent messageEvent ) {
runOnUiThread( new Runnable() {
@Override
public void run() {
if( messageEvent.getPath().equalsIgnoreCase( WEAR_MESSAGE_PATH ) ) {
mTextView.setText( new String( messageEvent.getData() ));
}
}
});
}
}
同样在我的智能手机应用程序构建配置中,我使用以下命令:
wearApp project(':mywearapp')
最佳答案
您的主应用程序和Wear应用程序的程序包名称需要完全匹配:您的程序包名称似乎设置为com.myapp.appname.wear
(尽管您不包括build.gradle
,这可能会覆盖它)。
另外,请确保在构建GoogleApiClient
时致电addConnectionCallbacks()-否则,在连接时它不知道呼叫谁。