android - 无法为 Wear OS 应用导入 com.google.android.wearable.intent.RemoteIntent

标签 android android-intent wear-os

我尝试在 Wear OS 应用上实现“在手机上打开”按钮,但无法导入 RemoteIntent 类。

当我将 import com.google.android.wearable.intent.RemoteIntent 添加到我的 MainActivity.java 时,可穿戴设备将以红色下划线显示,并带有“无法解析符号”错误。

我是否需要向我的项目添加特定的依赖项才能使其可用?

docs对于 RemoteIntent 不要给我任何关于我可能缺少什么的线索。

对于上下文,我尝试在我的应用程序中使用以下代码段:

private void openOnPhone(Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    RemoteIntent.startRemoteActivity(this, intent, null);
    // 'Open on phone' confirmation overlay
    new ConfirmationOverlay().setType(ConfirmationOverlay.OPEN_ON_PHONE_ANIMATION).setMessage("Opened on phone").showOn(this);
}

最佳答案

您需要导入:

androidx.wear:wear-remote-interactions:1.0.0

然后使用类似的东西:

RemoteActivityHelper(application).startRemoteActivity(
            Intent(Intent.ACTION_VIEW).apply {
                addCategory(Intent.CATEGORY_DEFAULT)
               .addCategory(Intent.CATEGORY_BROWSABLE)
                data = Uri.parse("some://uri")
            },
            null
        )

但是,该库确实需要compileSdkVersion 31 (Android 12)

关于android - 无法为 Wear OS 应用导入 com.google.android.wearable.intent.RemoteIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68816157/

相关文章:

java - Android 上的 SQLite 错误

android - 从 "Choose Application"列表中隐藏 NFC 应用程序/通过外部 NFC Intent 禁用启动

wear-os - 在 Android Wear 应用程序中使可穿戴设备振动

wear-os - GridViewPager的页面位置

从 Play 商店安装应用程序时,Android BroadcastReceiver 不工作

android - 如何在 Android Wear 中更改语音识别语言

java - 错误 : No resource identifier found for attribute `requestLegacyExternalStorage` and `preserveLegacyExternalStorage` in package `android`

android editText 光标可见吗?

android - ActionbarSherlock 和 homeAsUpIndicator

javascript - 从 Facebook 帖子打开 Chrome/Firefox (Android OS) 的 URL 方案