如何通过点击 Wear watch android OS 中的通知操作按钮启动移动应用程序

问题描述

我是 Android 智能手表开发的新手。我想通过 Wear Watch 通知操作按钮打开移动应用。

用于创建通知的移动应用代码

Intent notificationIntent = new Intent(context,NotificationActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationIntent.putExtra("ID",Id);
    notificationIntent.putExtra("Title",title);
    notificationIntent.setAction("NOTIFICATION");
   
    PendingIntent alertPendingIntent = PendingIntent.getActivity(context,Integer.parseInt(Id),notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    //When the entire notification group is clicked
    Intent groupIntent = new Intent(context,NotificationActivity.class);
    groupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    groupIntent.setAction("GroupNotification");
    PendingIntent groupPendingIntent = PendingIntent.getActivity(context,groupIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    //Channel ID for the notification
    String NOTIFICATION_CHANNEL_ID = "12321312";
    notificationmanager notificationmanager = (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  
    String GROUP_KEY = "com.example.notification.alerts.utils.sample";
    Uri defaultSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID)
                    .setSmallIcon(R.drawable.icon_notification)
                    .setColor(MyApplication.getInstance().getResources().getColor(R.color.app_icon_color))
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setShowWhen(true)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                    .setGroup(GROUP_KEY)
                    .setPriority(Notification.PRIORITY_MAX);

    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notification_icon));

    PendingIntent pendingIntentSearch = searchActionClicked(context,notificationIntent);
            notificationBuilder.addAction(R.drawable.search,"Search",pendingIntentSearch );
            notificationBuilder.extend(new WearableExtender().addAction(createAction(R.drawable.search,pendingIntentSearch )));
            PendingIntent pendingIntent = openActionButtonClicked(context,notificationIntent);
            notificationBuilder.addAction(R.drawable.open,"open",pendingIntent);
            notificationBuilder.setContentIntent(alertPendingIntent);
            notificationBuilder.extend(new WearableExtender().addAction(createAction(R.drawable.open,pendingIntent)));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Notification summaryNotification =
                new NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID)
                        .setSmallIcon(R.drawable.small_icon_notification)
                        .setColor(MyApplication.getInstance().getResources().getColor(R.color.app_icon_color))
                        .setGroup(GROUP_KEY)
                        .setGroupSummary(true)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(groupPendingIntent)
                        .build();
        notificationmanager.notify(0,summaryNotification);
    }

    notificationmanager.notify(Integer.parseInt(Id),notificationBuilder.build());


private static PendingIntent searchCenteractionClicked(Context context,Intent notificationIntent) {
    Intent searchCenter = new Intent(context,NotificationActivity.class);
    searchCenter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    searchCenter.putExtra("ID",Id);
    searchCenter.putExtra("Title",title);
    searchCenter.setAction("Search");
    return PendingIntent.getActivity(context,searchCenter,PendingIntent.FLAG_UPDATE_CURRENT);
}

private static PendingIntent openActionButtonClicked(Context context,Intent notificationIntent) {
    Intent open = new Intent(context,NotificationActivity.class);
    open.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    open.putExtra("ID",Id);
    open.putExtra("Title",title);
    open.setAction("open");
    return PendingIntent.getActivity(context,open,PendingIntent.FLAG_UPDATE_CURRENT);
}

我希望 Android 智能手表在连接到手机后会自动收到通知

但是我们如何知道在智能手表中任何操作按钮都被点击,所以我可以在手表示例中点击操作按钮时在手机中打开移动应用程序,就像用户点击手机一样。

只需点击智能手表中的通知即可在移动应用中打开活动。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...