我遇到了同样的问题。我的解决方案是实现一个活动并将自定义布局添加到此活动中。按照这个步骤。
第 1 步:在您的穿戴模块中创建自定义布局。示例:customlayout.xml
第二步:在穿戴模块中创建一个Activity:
public class WearNotificationActivity extends Activity{
private ImageView mSomeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customlayout);
mSomeButton= (ImageView) this.findViewById(R.id.somebutton);
mSomeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do something here
}
});
}
}
第 3 步。将您想要的数据从您的手机发送到您的穿戴:
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private Button mSomeButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
mSomeButton=(Button) findViewById(R.id.somebutton);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(AppIndex.APP_INDEX_API)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mSomeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sendToWear("title","description");
}
});
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
if(!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(mGoogleApiClient!=null) {
mGoogleApiClient.disconnect();
}
}
public void sendToWear(String title, String description){
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/wear");
putDataMapReq.getDataMap().putString("title", title);
putDataMapReq.getDataMap().putString("description", description);
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
}
}
Step 4. 接收您穿戴的数据并发出通知。为此,您必须在为 WearableListenerService 扩展的穿戴模块中创建一个类,并将该类添加到您的穿戴清单中。
public class NotificationUpdateService extends WearableListenerService
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
ResultCallback<DataApi.DeleteDataItemsResult> {
private GoogleApiClient mGoogleApiClient;
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent dataEvent : dataEvents) {
if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
DataItem item = dataEvent.getDataItem();
if (item.getUri().getPath().compareTo("/wear") == 0) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
String title = dataMap.getString("title");
String description=dataMap.getString("description");
buildWearableOnlyNotification(title, description)
}
} else if (dataEvent.getType() == DataEvent.TYPE_DELETED) {
}
}
}
/**
* Builds a simple notification on the wearable.
*/
private void buildWearableOnlyNotification(String title, String content) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVibrate(new long[]{10, 10, 10, 10, 10})
.setContentTitle(title)
.setContentText(content);
Intent notificationIntent = new Intent(this, WearNotificationActivity.class);
PendingIntent pendingNotificationIntent =
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder secondpage =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.extend(new NotificationCompat.WearableExtender()
.setDisplayIntent(pendingNotificationIntent)
.setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_FULL_SCREEN)
);
mNotificationBuilder = new NotificationCompat.WearableExtender()
.addPage(secondpage.build()).extend(builder);
Notification notification=mNotificationBuilder.build();
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.notify(Constants.WATCH_ONLY_ID, notification);
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
在你的清单中:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<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>
<activity
android:name=".WearNotificationActivity"
android:exported="true"
android:allowEmbedded="true"
android:taskAffinity=""
android:theme="@android:style/Theme.DeviceDefault.Light"
>
</activity>
<service android:name=".NotificationUpdateService">
<intent-filter>
<action
android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
</application>
最后,您需要在手机中添加所有依赖项并佩戴 gradle。
电话:
compile 'com.google.android.gms:play-services-wearable:7.5.0'
compile 'com.android.support:support-v4:23.1.0'
wearApp project(':wearmodule')
穿:
compile 'com.google.android.support:wearable:1.3.0'
provided 'com.google.android.wearable:wearable:+'
compile 'com.google.android.gms:play-services-wearable:8.1.0'
我希望这对你有用。