【问题标题】:Slow loading of the application after clicking from the AppWidget从 AppWidget 点击后应用加载缓慢
【发布时间】:2021-03-22 13:15:52
【问题描述】:

当您点击应用中的按钮时,浮动应用小部件会打开。

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

我在下面列出了两种方法

问题:

点击后,应用需要几秒才能打开。

我希望点击后打开内存栏中的应用程序

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)
    }
}

【问题讨论】:

  • 什么是“浮动应用小部件”?你在用ACTION_MANAGE_OVERLAY_PERMISSION吗?

标签: android android-widget


【解决方案1】:

试试我的 sn-p,首先检查评论中的链接(对不起 Java,它只是从工作项目中复制粘贴)。代码在Service 中执行,当所有Activites 都被放到后台时开始执行(又名“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();
            }

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 2022-12-21
    • 1970-01-01
    • 2018-02-11
    • 2019-12-20
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多