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

如何通过Complication的setTapAction()广播接收器启动Wear OS应用?

解决Wear OS Complication点击启动应用的问题

1. 修正Manifest配置

确保MainActivity在Manifest中配置正确的权限与Intent过滤规则,这是外部(系统Complication服务)能启动它的基础:

<activity
    android:name="au.gondwanasoftware.ontrack.presentation.MainActivity"
    android:exported="true"> <!-- 必须设置为true,允许系统Complication服务启动该Activity -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <!-- 若使用自定义Action,需在此添加 -->
        <action android:name="au.gondwanasoftware.action.ONTRACK" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

2. 修复BroadcastReceiver中的Intent代码

方案一:直接指定Activity类(最可靠)

避免隐式Intent的匹配问题,直接通过类名构建Intent,同时添加适配后台启动的Flag:

override fun onReceive(context: Context, intent: Intent) {
    val appIntent = Intent(context, MainActivity::class.java).apply {
        // NEW_TASK确保在新任务栈启动,REORDER_TO_FRONT避免重复创建Activity实例
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
    }
    try {
        context.startActivity(appIntent)
    } catch (e: ActivityNotFoundException) {
        Log.e(tag, "启动MainActivity失败", e)
    }
}

方案二:修正自定义Action的写法

若坚持使用自定义Action,需修正代码中的变量名错误(之前误将appIntent写成intent):

override fun onReceive(context: Context, intent: Intent) {
    val appIntent = Intent("au.gondwanasoftware.action.ONTRACK").apply {
        setClassName(
            "au.gondwanasoftware.ontrack", 
            "au.gondwanasoftware.ontrack.presentation.MainActivity"
        )
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }
    try {
        context.startActivity(appIntent)
    } catch (e: ActivityNotFoundException) {
        Log.e(tag, "启动MainActivity失败", e)
    }
}

3. 更优方案:直接用PendingIntent启动Activity(跳过BroadcastReceiver)

Complication的setTapAction可以直接绑定启动Activity的PendingIntent,无需额外的BroadcastReceiver,更符合Wear OS设计规范:
在Complication Provider类中构建点击事件:

val tapIntent = Intent(context, MainActivity::class.java).apply {
    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
}
val pendingIntent = PendingIntent.getActivity(
    context,
    0, // 请求码,可按需修改
    tapIntent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE // Android 12+必须添加IMMUTABLE/MUTABLE
)
// 绑定到Complication
complicationData = ComplicationData.Builder(...)
    .setTapAction(pendingIntent)
    .build()

关键注意事项

  • Android 10+后台启动限制:用户点击Complication属于主动交互,系统允许启动Activity,但必须使用明确Intent(指定ComponentName或直接关联Activity类),隐式Intent仅靠Action匹配可能被拦截。
  • android:exported="true":必须为启动的Activity设置该属性,否则系统无法从外部访问。
  • PendingIntent Flags:Android 12及以上版本,创建PendingIntent时必须添加FLAG_IMMUTABLE或FLAG_MUTABLE,无需修改Intent内容时用FLAG_IMMUTABLE更安全。

内容的提问来源于stack exchange,提问作者Peter McLennan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 07:07:45