当前位置: 首页 > 知识库问答 >
问题:

从AppWidget单击后应用程序加载缓慢

年运珧
2023-03-14

当您单击应用程序中的一个按钮时,浮动应用程序小部件就会打开。

我写了一个函数,当点击Widget时应该打开应用程序屏幕。(代码如下)

我在下面列出了两种方法

问题:

点击后,应用程序打开需要几秒钟的时间。

我想在点击后打开内存栏中的应用程序

MotionEvent.ACTION_UP -> {
    if (System.currentTimeMillis() - clickStartTimer < FloatingWidgetView.CLICK_DELTA) {
        Toast.makeText(context, "clicked floating widget", Toast.LENGTH_SHORT).show()

        val configIntent = Intent(context, MainActivity::class.java)
        configIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        configIntent.data = Uri.parse(configIntent.toUri(Intent.URI_INTENT_SCHEME))

        val pendIntent = PendingIntent.getActivity(
            context,
            0,
            configIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )

        // Second method
        // val intent = Intent(context, MainActivity::class.java)
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        // startActivity(context, intent, null)
    }
}

共有1个答案

东方明亮
2023-03-14

试用一下我的片段,首先检查一下来自评论的链接(对不起Java,它只是从工作项目复制粘贴的)。代码在服务中执行,当所有活动被删除到后台(也称为“homed”)时,该服务将启动

            Intent i = new Intent(this, mainActivityClass);
            // brings back whole activitys stack
            i.setAction(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_LAUNCHER);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);

            // http://code.google.com/p/android/issues/detail?id=4536
            boolean showToastBecauseOfDelay;
            try { // works without delay till Android P, on Q also delayed...
                pendingIntent.send();
                showToastBecauseOfDelay = Build.VERSION.SDK_INT >= 27;
            } catch (PendingIntent.CanceledException e) {
                // startActivity may cause delay!!
                // when called less that 5 sec after Home button press...
                startActivity(i);
                showToastBecauseOfDelay = true;
            }

            // service starts when app/activity goes to background ("homed")
            // Toast will be shown only if user want to go back quickly after that
            // (just for some distration and feedback that click was registered)
            long diff = System.currentTimeMillis() - serviceStartedAt;
            if (showToastBecauseOfDelay && diff < 5000) {
                // long - 3.5 sec, short 2 - sec, no other option
                int toastDuration = diff < 2000 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
                Toast.makeText(this, R.string.resuming_app, toastDuration).show();
            }
 类似资料:
  • 我已经在我的应用程序上实现了推送通知,它们工作得很好。我遇到的问题是,当我点击下拉菜单中的它们时,它们会立即重新加载应用程序。为了解决这个问题,我让应用程序创建了一个新的活动实例。这现在打开了一个新的页面,但当点击从这个新的页面返回时,它有同样的问题,并重新加载整个应用程序。

  • 问题内容: 单击“后退”按钮时,Safari浏览器加载旧的youtube视频时出现问题。我尝试将onunload=“”此处提到[防止Safari5中的后退按钮缓存添加到body标签,但是在这种情况下不起作用。 有什么方法可以防止从某个页面的缓存中加载safari? 问题答案: 您的问题是由后退缓存引起的。当用户离开时应该保存页面的完整状态。当用户使用后退按钮导航时,可以非常快地从缓存中加载页面。这

  • 我在亚马逊购物应用上测试过。我有一个问题后滑动找到一个元素。因为列表视图太长了。我需要滚动(使用刷卡API)。但是在我滑动之后,我不能再点击元素了。它没有返回错误,但应用程序没有响应。 函数ScrollToElement()用于滑动到搜索元素。我试着用触控动作轻拍,但没有希望。我试图找到一个准确的(372466)位置,但效果并不理想。提前谢谢。

  • 如何通过编程方式从应用程序中单击Android“后退按钮”?可能吗?如adb外壳输入键事件4

  • null null null null 我为测试目的编写了一个APK-https://github.com/yccheok/google-drive-bug/releases/download/1.0/demo.APK

  • 我正在尝试使用selenium/python从印度中央污染控制委员会读取数据表。这是一个输出示例。我基本上遵循此处介绍的方法:https://github.com/RachitKamdar/Python-Scraper. 多亏了@Prophet,我才能够从第一页读取数据(使用Python的XPATH选择元素?)但我无法让selenium在切换到第2页时等待数据表重新加载。我试图添加webdrive