You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android与Wear OS配套应用:如何从手表通知打开手表端应用?

解决方案

要让同步到Wear OS的通知能直接打开手表端应用,核心是在手机端构建通知时,为Wear OS设备指定专属的交互逻辑,具体步骤如下:

  • 为Wear OS添加专属的PendingIntent
    在手机端创建通知时,除常规配置外,需通过NotificationCompat.WearableExtender为手表端单独设置打开应用的Intent。由于手机和手表应用包名一致,直接指定手表端目标Activity的组件即可:

    // 手表端目标Activity的ComponentName(包名一致,只需指定类名)
    val watchComponent = ComponentName(
        packageName,
        "com.your.package.name.WatchMainActivity" // 替换为手表端实际Activity路径
    )
    
    // 构建打开手表应用的Intent
    val watchIntent = Intent(Intent.ACTION_VIEW).apply {
        component = watchComponent
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
    }
    
    // 生成PendingIntent
    val watchPendingIntent = PendingIntent.getActivity(
        context,
        0,
        watchIntent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
    
    // 构建通知并添加Wear OS扩展
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setContentTitle("你的通知标题")
        .setContentText("你的通知内容")
        .setSmallIcon(R.drawable.ic_notification)
        // 点击通知直接打开手表应用
        .extend(
            NotificationCompat.WearableExtender()
                .setContentIntent(watchPendingIntent)
                // 可选:添加自定义按钮入口
                .addAction(
                    NotificationCompat.Action.Builder(
                        R.drawable.ic_open_watch,
                        "打开手表应用",
                        watchPendingIntent
                    ).build()
                )
        )
        .build()
    
  • 确保手表端Activity能响应Intent
    在手表端的AndroidManifest.xml中,给目标Activity添加对应的intent-filter,保证系统能匹配到发送的Intent:

    <activity
        android:name=".WatchMainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    
  • 关键注意事项

    • 确认手表端应用是手机应用的配套模块(手机端build.gradle配置wearApp project(':wear'),或作为独立应用但包名完全一致)
    • 确保Wear OS设备已授予通知访问权限,且手机处于未使用状态(屏幕熄灭/锁屏)时,通知才会同步到手表
    • Android 12+版本中,PendingIntent需添加FLAG_IMMUTABLE(若无需动态修改Intent内容)

内容的提问来源于stack exchange,提问作者Rahul Mishra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 09:25:19