关于android:Activity延迟后打开 | 珊瑚贝

Activity opens after delay

本问题已经有最佳答案,请猛点这里访问。


场景 – ActivityA 可见。在按下 Android 设备的 Home 按钮时,ActivityA/App 进入后台并在 onUserLeaveHint() 中,触发打开 ActivityB 的意图。该应用程序会立即最小化,但 ActivityB 会在延迟 5-6 秒后打开。经过一些调试后,intent 会立即触发,但 ActivityB 的 onCreate() 会在 5-6 秒后调用。
PS – ActivityB 具有启动模式 – 单实例。

知道为什么会这样吗?

打开activity的代码如下-

1
2
3
4
5
6
    override fun onUserLeaveHint() {
    super.onUserLeaveHint()

    val intent = Intent(this, ActivityB::class.java)
    startActivity(intent)
}

ActivityB 在 manifest 文件中定义如下 –

1
2
3
4
5
6
7
8
9
10
11
<activity android:name=”.activity.ActivityB”
             android:allowTaskReparenting=”true”
             android:autoRemoveFromRecents=”true”
             android:configChanges=”screenSize|smallestScreenSize|screenLayout|orientation”
             android:excludeFromRecents=”true”
             android:screenOrientation=”portrait”
             android:noHistory=”true”
             android:launchMode=”singleInstance”
             android:taskAffinity=””
             android:supportsPictureInPicture=”true”
             android:theme=”@style/PipTheme”/>

PipTheme –

1
2
3
4
5
6
7
8
9
<style name=”PipTheme” parent=”Theme.AppCompat.Light.NoActionBar”>
   <item name=”android:windowIsTranslucent”>true</item>
   <item name=”android:windowBackground”>@android:color/transparent</item>
   <item name=”android:windowContentOverlay”>@null</item>
   <item name=”android:windowNoTitle”>true</item>
   <item name=”android:windowIsFloating”>false</item>
   <item name=”android:windowActionBar”>false</item>
   <item name=”android:windowFullscreen”>true</item>
   <item name=”android:backgroundDimEnabled”>false</item>
  • 你能分享你的代码吗?
  • @RoyalGriffin,添加了代码,请检查。
  • 将 ActivityB 传递给 onResume() 方法…
  • 看起来活动在按下主页后被故意抑制 5 秒(并且有充分的理由)。看到这个(在许多类似的帖子中)。


https://issuetracker.google.com/issues/36910222
与其说是一个问题,不如说是一个框架功能,它阻止了应用程序在按下主页按钮时强制自己打开。

有几个解决方法:

  • 使用pendingIntent-

    val intent = Intent(context, ActivityB::class.java)
    val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
    未决意图.send()

  • 使用报警管理器打开activtiy

    • 谢谢。有效。我使用了 PendingIntent 方法。


    来源:https://www.codenong.com/53540194/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9328.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?