gpt4 book ai didi

java - Android手持设备和Wear通信不一致

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:26 24 4
gpt4 key购买 nike

我们有一个 Android 移动应用程序,您可以在其中购买一段时间的 parking 票。现在,我们计划将其与 Android Wear 集成。

我们在这里做的是:

  1. 我们希望用户在票证到期前 15 小时收到通知。
  2. 为此,我们创建本地通知并使用 Alarm Manger 安排它。
  3. 此预定通知由 Android 广播接收器接收,并在移动设备上的 Android 通知部分显示此通知。
  4. 此外,该接收器调用 Intent 服务来发送佩戴通知。在此步骤中,我们创建 googleApiClient 和 onConnected 回调,我们将数据发送到 Wear 以显示通知。
  5. 佩戴后,用户可以查看通知,点击后,用户可以延长购买门票的时间。点击通知后,此流程包含 3-4 次浏览。

我们在步骤 4 中遇到问题。大多数情况下,在第一次连接(通知)时,wear 不会显示通知,而在第二次连接(通知)时,wear 同时显示第一个和第二个通知,之后就可以正常工作了。

我们试图找出问题所在,但没有成功。下面是Receiver、Intent Service和穿戴端ListnerServices的代码 fragment ,方便理解。

public class WearNotificationService extends IntentService {

private static final String TAG = "PhoneActivity";
private GoogleApiClient mGoogleApiClient;
public static String title;
public static String desc;
public static String data;

public WearNotificationService() {
super("WearNotificationService");
}

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, title +"--"+ desc , Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
sendNotification(title,desc,data);
Log.d(TAG, "onConnected: " + connectionHint);
}

@Override
public void onConnectionSuspended(int cause) {
Log.d(TAG, "onConnectionSuspended: " + cause);
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d(TAG, "onConnectionFailed: " + result);
}
}).addApi(Wearable.API).build();
mGoogleApiClient.connect();
}

@Override
protected void onHandleIntent(Intent intent) {
}

private void sendNotification(String title,String desc,String data) {
Log.e(TAG, "i am onConnectiond: ");
PutDataMapRequest dataMapRequest = PutDataMapRequest.create(Constants.PATH_NOTIFICATION);
dataMapRequest.getDataMap().putDouble(Constants.NOTIFICATION_TIMESTAMP, System.currentTimeMillis());
dataMapRequest.getDataMap().putString(Constants.KEY_TITLE, title);
dataMapRequest.getDataMap().putString(Constants.KEY_DESC, desc);
dataMapRequest.getDataMap().putString(Constants.KEY_DATA, data);
PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
}

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (null != intent) {
String action = intent.getAction();
if (Constants.ACTION_DISMISS.equals(action)) {
dismissNotification();
}
}
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent dataEvent : dataEvents) {
if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
if (Constants.PATH_NOTIFICATION.equals(dataEvent.getDataItem().getUri().getPath())) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(dataEvent.getDataItem());
String title = dataMapItem.getDataMap().getString(Constants.KEY_TITLE);
String content = dataMapItem.getDataMap().getString(Constants.KEY_DESC);
String data = dataMapItem.getDataMap().getString(Constants.KEY_DATA);
String id = null;
try {
JSONObject obj = new JSONObject(data);
id = (String) obj.get("id");
} catch (JSONException e) {
e.printStackTrace();
}
sendNotification(title, content, data,id);
}
}
}
}

private void sendNotification(String title, String content, String data,String id) {
Intent notificationIntent = new Intent(this, HoursExtension.class);
Log.e("data1111", data);
HoursExtension.data = data;
HoursExtension.id = id;
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(notificationPendingIntent)
.extend(new NotificationCompat.WearableExtender().setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.rtabg)))
;

Notification notification = builder.build();

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(Integer.parseInt(id), notification);
}

最佳答案

我通过在应用程序安装上发送虚拟请求解决了这个问题。一旦安装了应用程序并通过吃第一个请求来处理磨损,就会发送该请求,这解决了我的问题。 :)

关于java - Android手持设备和Wear通信不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28276028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com