简体   繁体   English

带有功能参数的应用程序操作“打开应用程序”有时会忽略参数

[英]App Action "open app" with feature parameter sometimes ignores parameter

I have been going through the codelab"Extend an Android app to the Google Assistant with App Actions (Level 2)" and am working on the step"Add the Open app feature BII capability" I have added the relevant configurations to the shortcuts.xml file (see below).我一直在通过代码实验室“使用应用操作将 Android 应用扩展到 Google 助理(第 2 级)” ,并且正在执行“添加打开应用功能 BII 功能”步骤我已将相关配置添加到快捷方式.xml文件(见下文)。 The following has been tested with both a real device (A Motorola Moto G Power running Android 11 with 3664M of memory) and virtual devices (A Pixel 5 running Android 12 (API 31) that was tested with both 1536MB and 4096MB of memory)以下内容已使用真实设备(运行 Android 11 且内存为 3664M 的摩托罗拉 Moto G Power)和虚拟设备(运行 Android 12 (API 31) 的 Pixel 5,同时使用 1536MB 和 4096MB 内存进行测试)进行了测试

I've confirmed that this works with the App Action test tool.我已经确认这适用于 App Action 测试工具。

If I then invoke it from the Assistant, it will start the app and show the correct information based on the feature.如果我随后从助手调用它,它将启动应用程序并根据该功能显示正确的信息。

If, however, I then return to the Android home screen and again invoke it from the Assistant, trying out a different feature, it will bring up the app, but often stay on the same information as before.但是,如果我随后返回 Android 主屏幕并再次从 Assistant 调用它,尝试不同的功能,它将调出应用程序,但通常保持与以前相同的信息。 If I make sure to close the app (by swiping it away in the app list), it will always work, but if I just return to the home screen, sometimes it works correctly and sometimes it does not.如果我确保关闭应用程序(通过在应用程序列表中将其滑动),它将始终有效,但如果我只是返回主屏幕,有时它可以正常工作,有时它不能正常工作。

Working on a hunch, and having only a tenuous grasp of Android, I suspected that the call to TasksActivity.onCreate() wasn't being called because the app hadn't been destroyed.凭直觉工作,并且对 Android 的掌握很薄弱,我怀疑对TasksActivity.onCreate()的调用没有被调用,因为应用程序没有被销毁。 Adding logging for this method, along with onStart() , onResume() , onPause() , and onStop() seems to confirm this.添加此方法的日志记录以及onStart()onResume()onPause()onStop()似乎证实了这一点。 From what I can gather, it is in onCreate() where it builds the layout that includes the TasksFragment .据我所知,它在onCreate()中构建包含TasksFragment的布局。

I can confirm that TasksFragment.onCreateView() is being called only when TasksActivity.onCreate() is.我可以确认只有在 TasksActivity.onCreate( TasksFragment.onCreateView()被调用时才调用TasksActivity.onCreate() It seems to use activity?.intent?.extras to get the feature parameter.似乎使用activity?.intent?.extras来获取功能参数。

At the suggestion of some friends who are better Android developers than me, they suggested that I add a TasksFragment.onResume() to catch when the view is being re-shown and to use activity?.intent?.extras again to et the feature parameter.在一些比我更好的 Android 开发者的朋友的建议下,他们建议我添加一个TasksFragment.onResume()来捕捉视图何时重新显示并再次使用activity?.intent?.extras来设置该功能范围。 While I can verify that it is being called when I re-invoke it from the home screen (and when onCreate() / onCreateView() is not called), it seems to retain the old value for "feature" (and, I assume, the entire old Intent).虽然我可以验证当我从主屏幕重新调用它时它正在被调用(并且当onCreate() / onCreateView()没有被调用时),它似乎保留了“功能”的值(并且,我假设,整个旧意图)。

The question is thus:因此问题是:

  • How can I correctly capture the correct Intent and parameter under all circumstances - both when a "cold start" of an app as well as returning to an app that is already running?如何在所有情况下正确捕获正确的 Intent 和参数 - 无论是在应用程序的“冷启动”还是返回到已经运行的应用程序时?

Should we be doing something in a different part of the lifecycle?我们应该在生命周期的不同部分做某事吗? Doing work somewhere besides onCreate() ?onCreate()以外的地方工作?

Portion of log when invoking for the first time using "show my completed tasks using AppName".首次使用“使用 AppName 显示我完成的任务”调用时的部分日志。 Note the onCreateView and onCreate and that feature=completed_tasks .注意onCreateViewonCreate以及feature=completed_tasks :

2022-07-12 20:07:55.912 18392-18392/com.addventure.appactions.common D/TasksFragment: >> onCreateView
2022-07-12 20:07:55.912 18392-18392/com.addventure.appactions.common D/TasksFragment: [feature=completed_tasks]
2022-07-12 20:07:55.912 18392-18392/com.addventure.appactions.common D/TasksFragment: [com.google.android.apps.gsa.shared.util.starter.IntentStarter.ERROR_TOAST_ID=2132082905]
2022-07-12 20:07:55.912 18392-18392/com.addventure.appactions.common D/TasksFragment: [extra_accl_intent=true]
2022-07-12 20:07:55.987 18392-18392/com.addventure.appactions.common D/TasksActivity: >> onCreate
2022-07-12 20:07:55.988 18392-18392/com.addventure.appactions.common D/TasksActivity: [feature=completed_tasks]
2022-07-12 20:07:55.988 18392-18392/com.addventure.appactions.common D/TasksActivity: [com.google.android.apps.gsa.shared.util.starter.IntentStarter.ERROR_TOAST_ID=2132082905]
2022-07-12 20:07:55.988 18392-18392/com.addventure.appactions.common D/TasksActivity: [extra_accl_intent=true]
2022-07-12 20:07:56.012 18392-18392/com.addventure.appactions.common D/TasksActivity: >> onStart
2022-07-12 20:07:56.018 18392-18392/com.addventure.appactions.common D/TasksActivity: >> onResume
2022-07-12 20:07:56.020 18392-18392/com.addventure.appactions.common D/TasksFragment: >> onResume
2022-07-12 20:07:56.020 18392-18392/com.addventure.appactions.common D/TasksFragment: [feature=completed_tasks]
2022-07-12 20:07:56.020 18392-18392/com.addventure.appactions.common D/TasksFragment: [com.google.android.apps.gsa.shared.util.starter.IntentStarter.ERROR_TOAST_ID=2132082905]
2022-07-12 20:07:56.020 18392-18392/com.addventure.appactions.common D/TasksFragment: [extra_accl_intent=true]

Portion of log when returning to home screen :返回主屏幕时的部分日志

2022-07-12 20:09:20.073 18392-18392/com.addventure.appactions.common D/TasksActivity: >> onPause
2022-07-12 20:09:20.124 18392-18392/com.addventure.appactions.common D/TasksActivity: >> onStop

Portion of log when then re-invoking app with "show my open tasks using AppName".然后使用“使用 AppName 显示我的打开任务”重新调用应用程序时的日志部分。 Note that onCreate and onCreateView are not logged, and that feature=completed_tasks even tho this should be "active_tasks" based on the invocation feature parameter :请注意, onCreateonCreateView没有被记录,并且即使这应该是基于调用功能参数的“active_tasks”, feature=completed_tasks

2022-07-12 20:09:22.130 18392-18392/com.addventure.appactions.common D/TasksActivity: >> onStart
2022-07-12 20:09:22.132 18392-18392/com.addventure.appactions.common D/TasksActivity: >> onResume
2022-07-12 20:09:22.132 18392-18392/com.addventure.appactions.common D/TasksFragment: >> onResume
2022-07-12 20:09:22.133 18392-18392/com.addventure.appactions.common D/TasksFragment: [feature=completed_tasks]
2022-07-12 20:09:22.133 18392-18392/com.addventure.appactions.common D/TasksFragment: [com.google.android.apps.gsa.shared.util.starter.IntentStarter.ERROR_TOAST_ID=2132082905]
2022-07-12 20:09:22.133 18392-18392/com.addventure.appactions.common D/TasksFragment: [extra_accl_intent=true]

Relevant portion of shortcuts.xml file : shortcuts.xml 文件的相关部分

    <!--    OPEN APP FEATURE with inline inventory
        https://developers.google.com/assistant/app/reference/built-in-intents/common/open-app-feature

        Be sure to replace all occurrences of PUT_YOUR_APPLICATION_ID_HERE with the
        application ID you specified in app/build.gradle.
    -->
    <!-- Add Get Thing BII shortcuts   -->
    <shortcut
        android:shortcutId="active_tasks"
        android:shortcutShortLabel="@string/label_active"
        android:enabled="false">
        <capability-binding
            android:key="actions.intent.OPEN_APP_FEATURE">
            <parameter-binding
                android:key="feature"
                android:value="@array/active_tasks_synonyms" />
        </capability-binding>
    </shortcut>

    <shortcut
        android:shortcutId="completed_tasks"
        android:shortcutShortLabel="@string/label_completed"
        android:enabled="false">
        <capability-binding
            android:key="actions.intent.OPEN_APP_FEATURE">
            <parameter-binding
                android:key="feature"
                android:value="@array/completed_tasks_synonyms" />
        </capability-binding>
    </shortcut>

    <!-- Add Open App Feature BII capability   -->
    <capability android:name="actions.intent.OPEN_APP_FEATURE">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.addventure.appactions.common"
            android:targetClass="com.example.android.architecture.blueprints.todoapp.tasks.TasksActivity">
            <parameter
                android:name="feature"
                android:key="feature"/>
        </intent>
    </capability>

Relevant portion of TasksActivity.kt : TasksActivity.kt 的相关部分

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.tasks_act)
        setupNavigationDrawer()
        setSupportActionBar(findViewById(R.id.toolbar))

        // Logging for troubleshooting purposes
        log(TAG, "onCreate", intent)

        val navController: NavController = findNavController(R.id.nav_host_fragment)
        appBarConfiguration =
            AppBarConfiguration.Builder(R.id.tasks_fragment_dest, R.id.statistics_fragment_dest)
                .setOpenableLayout(drawerLayout)
                .build()
        setupActionBarWithNavController(navController, appBarConfiguration)
        findViewById<NavigationView>(R.id.nav_view)
            .setupWithNavController(navController)
    }

    override fun onStart() {
        super.onStart()
        log(TAG, "onStart")
    }

    override fun onResume() {
        super.onResume()
        log( TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        log( TAG, "onPause" )
    }

    override fun onStop() {
        super.onStop()
        log( TAG, "onStop" )
    }

    override fun onDestroy(){
        super.onDestroy()
        log( TAG, "onDestroy" )
    }

    companion object {
        fun log( tag: String, f: String ){
            log( tag, f, null )
        }

        fun log(tag: String, f: String, intent: Intent?) {
            Log.d(tag, ">> $f")
            val bundle: Bundle = intent?.extras ?: return

            bundle.keySet().forEach { key ->
                Log.d(tag, "[$key=${bundle.get(key)}]");
            }
        }
    }

Relevant portion of TasksFragment.kt : TasksFragment.kt 的相关部分

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        viewDataBinding = TasksFragBinding.inflate(inflater, container, false).apply {
            viewmodel = viewModel
        }
        setHasOptionsMenu(true)

        // Set a listener on task button
        setupFab()

        setFiltering( "onCreateView" )

        return viewDataBinding.root
    }

    override fun onResume() {
        super.onResume()
        setFiltering( "onResume" )
    }

    private fun setFiltering(f: String){
        TasksActivity.log( TAG, f, activity?.intent )
        viewModel.setFiltering(TasksFilterType.find(activity?.intent?.extras?.getString(OPEN_APP_FEATURE)))

        viewModel.setFiltering(activity?.intent?.extras?.getString(GET_THING))
    }

So one thing to check is Tasks and Back Stack and various setting in the Android Manifest所以要检查的一件事是Android Manifest中的Tasks 和 Back Stack以及各种设置

android:launchMode

To make sure that you are receiving the most recent Intent.确保您收到最新的 Intent。

I would also check what the result is with Activity.onNewIntent() to see what is being passed to the app.我还会检查Activity.onNewIntent()的结果,以查看传递给应用程序的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM