我的应用程序一直正常工作,直到BIND_LISTENER弃用,我已经做了任何需要更改但我的应用程序无法正常工作。我成功地从磨损中发送数据,但手机端没有任何反应。
我的主要代码(发件人):
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private TextView mTextView;
GoogleApiClient googleClient;
String WEARABLE_DATA_PATH = "/wearable_data";
ArrayList<String> dataBlock = new ArrayList();
Button startButton;
@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.text);
}
});
startButton = (Button) findViewById(R.id.startButton);
// Build a new GoogleApiClient that includes the Wearable API
googleClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
public void onConnected(Bundle connectionHint) {
Log.v("CONNECTED", "CONNECTED");
DataMap map = new DataMap();
map.putLong("Time",System.currentTimeMillis());
new SendToDataLayerThread(WEARABLE_DATA_PATH, map).start();
}
public void onStartClicked(View view) {
Log.v("StartClicked", "in on start clicked");
googleClient.connect();
}
@Override
public void onConnectionSuspended(int cause) {
Log.v("Suspended", "Suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v("Failed", "onConnectionFailed: ConnectionResult.getErrorCode() = "
+ connectionResult.getErrorCode());
}
class SendToDataLayerThread extends Thread {
String path;
DataMap dataMap;
// Constructor for sending data objects to the data layer
SendToDataLayerThread(String p, DataMap data) {
path = p;
dataMap = data;
}
public void run() {
// Construct a DataRequest and send over the data layer
PutDataMapRequest putDMR = PutDataMapRequest.create(path);
putDMR.getDataMap().putAll(dataMap);
putDMR.setUrgent();
PutDataRequest request = putDMR.asPutDataRequest();
DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();
if (result.getStatus().isSuccess()) {
Log.v("myTag", "DataMap: " + dataMap + " sent successfully to data layer ");
}
else {
// Log an error
Log.v("myTag", "ERROR: failed to send DataMap to data layer");
}
}
}
protected void onPause() {
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
protected void onResume() {
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
}
我的穿着Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.datacollector">
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
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>
</application>
</manifest>
电话方面的听众服务:
public class ListenerService extends WearableListenerService {
//private static final String START_ACTIVITY_PATH = "/start-activity";
private static final String WEARABLE_DATA_PATH = "/wearable_data";
public void onMessageReceived(MessageEvent messageEvent) {
Log.v("myTag", "Test");
}
public void onDataChanged(DataEventBuffer dataEvents) {
DataMap dataMap;
Log.v("myTag", "DataMap received from watch: ");
for (DataEvent event : dataEvents) {
// Check the data type
if (event.getType() == DataEvent.TYPE_CHANGED) {
// Check the data path
String path = event.getDataItem().getUri().getPath();
if (path.equals(WEARABLE_DATA_PATH)) {}
dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
Log.v("myTag", "DataMap received on watch: " + dataMap);
}
}
}
}
我的手机清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.name.datacollector">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED"/>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear"
android:host="*"/>
</intent-filter>
</service>
</application>
</manifest>
答案 0 :(得分:0)
当我不得不停止使用绑定侦听器时,同样的事情发生在我身上。手机没有收到任何内容,因为手机的清单中有android:exported="false"
。删除它或将其设置为true
,您应该能够再次接收数据。
编辑:您还错过了手机清单中的路径。您正在使用路径/wearable_data
将消息发送到手机,但它永远不会收到路径未列在清单中。只需在android:pathPrefix="/wearable_data"
的{{1}}部分android:host="*"
之后添加data
即可。您可以阅读更多相关信息here。
答案 1 :(得分:0)
为了在项目中的移动应用和磨损应用之间发送和同步数据他们必须具有相同的包名称,如开发人员说明here中所述,
使用Android Studio打包Wear 1.x应用
在Android Studio中打包Wear 1.x应用程序:
- 确保手表和手机应用程序模块具有相同的程序包名称。
醇>
在这里
如果您的Wear 2.0应用程序配有随附的手机应用程序,请为您的Wear应用程序和该手机应用程序使用相同的程序包名称。
改变磨损和磨损移动应用程序具有相同的程序包名称:
com.example.myname.datacollector
或
com.example.name.datacollector
如果您不确定如何更改现有项目模块的包名称here
,此堆栈溢出问题将对您有所帮助