android 磨损时未调用 onDataChanged()
onDataChanged() not being called on android wear
我正在尝试使用数据项向我的服装发送一些字符串,但我的服装似乎从未收到任何信号,因为从未调用过 onDataChanged()。我什至设置了一个时间戳,以确保每次发送的数据总是不同的。
是否有特定的方法可以让我在两台设备上都安装该应用程序以使其正常工作?我只是点击 运行 并选择我的 phone,然后切换模块并对我的穿戴设备执行相同的操作。
这是我 phone 上的主要 activity 的代码:
public class HomeActivity extends Activity{
public static String TAG = "HomeActivity";
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home_view);
Button mButton = (Button) findViewById(R.id.send_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendData();
}
});
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}).addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
private void sendData(){
if (mGoogleApiClient!=null){
return;
}
final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE");
final DataMap map = putRequest.getDataMap();
map.putInt("color", Color.RED);
map.putLong("date", new Date().getTime());
Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}
}
这是我的可穿戴设备上的代码:
public class LayoutFaceService extends CanvasWatchFaceService implements DataApi.DataListener{
@Override
public void onCreate(){
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected");
Wearable.DataApi.addListener(mGoogleApiClient, LayoutFaceService.this);
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.build();
mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {
Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
}
}
}
还有我的穿着清单:
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault" >
<service
android:name=".LayoutFaceService"
android:label="@string/my_digital_name"
android:permission="android.permission.BIND_WALLPAPER" >
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/watch_face" />
<meta-data
android:name="com.google.android.wearable.watchface.preview"
android:resource="@drawable/preview_digital" />
<meta-data
android:name="com.google.android.wearable.watchface.preview_circular"
android:resource="@drawable/preview_digital_circular" />
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
<category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
在sendData()
中,您的条件似乎不正确; mGoogleApiClient != null
是真的,所以你就在那里退出。解决这个问题,看看你是否能取得进一步的进展;可能还有其他问题,但这是第一个明显的问题。如果这不能完全解决您的问题,请确保您还在 post.
中包含 phone 中的清单
我正在尝试使用数据项向我的服装发送一些字符串,但我的服装似乎从未收到任何信号,因为从未调用过 onDataChanged()。我什至设置了一个时间戳,以确保每次发送的数据总是不同的。
是否有特定的方法可以让我在两台设备上都安装该应用程序以使其正常工作?我只是点击 运行 并选择我的 phone,然后切换模块并对我的穿戴设备执行相同的操作。
这是我 phone 上的主要 activity 的代码:
public class HomeActivity extends Activity{
public static String TAG = "HomeActivity";
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home_view);
Button mButton = (Button) findViewById(R.id.send_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendData();
}
});
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}).addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
private void sendData(){
if (mGoogleApiClient!=null){
return;
}
final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE");
final DataMap map = putRequest.getDataMap();
map.putInt("color", Color.RED);
map.putLong("date", new Date().getTime());
Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}
}
这是我的可穿戴设备上的代码:
public class LayoutFaceService extends CanvasWatchFaceService implements DataApi.DataListener{
@Override
public void onCreate(){
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected");
Wearable.DataApi.addListener(mGoogleApiClient, LayoutFaceService.this);
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.build();
mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {
Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
}
}
}
还有我的穿着清单:
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault" >
<service
android:name=".LayoutFaceService"
android:label="@string/my_digital_name"
android:permission="android.permission.BIND_WALLPAPER" >
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/watch_face" />
<meta-data
android:name="com.google.android.wearable.watchface.preview"
android:resource="@drawable/preview_digital" />
<meta-data
android:name="com.google.android.wearable.watchface.preview_circular"
android:resource="@drawable/preview_digital_circular" />
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
<category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
在sendData()
中,您的条件似乎不正确; mGoogleApiClient != null
是真的,所以你就在那里退出。解决这个问题,看看你是否能取得进一步的进展;可能还有其他问题,但这是第一个明显的问题。如果这不能完全解决您的问题,请确保您还在 post.