简体   繁体   English

关闭屏幕后,有时我的应用程序活动会进入后台

[英]Activity of my app goes into background sometimes after I turn off the screen

I am running Camera as a service attached to an activity. 我正在将Camera作为附加到活动的服务运行。 There are other services such as upload and Firebase are running along with the camera service. 还有其他服务(例如上载和Firebase)与摄像头服务一起运行。 Now my requirement is to keep the service running after I turn off the screen. 现在,我的要求是在关闭屏幕后保持服务运行。 I am acquiring PARTIAL_WAKE_LOCK too. 我也正在获得PARTIAL_WAKE_LOCK。 The services run perfectly for the initial 10 or 12 minutes. 该服务在最初的10或12分钟内可以完美运行。 After that, the app stops sending any logs to ADB. 之后,该应用程序停止将任何日志发送到ADB。 In the device, the app goes to the background by itself. 在设备中,该应用程序会自行进入后台。 Not even onPause or onDestroy is logging anything. 甚至onPause或onDestroy都不记录任何内容。 The logs just stop coming to Android Studio. 日志只是停止进入Android Studio。 For resuming the normal functioning I have to manually open the app again, 要恢复正常运行,我必须再次手动打开该应用,

These are the things that I have already tried, 这些是我已经尝试过的东西,

1.Given permission of acquiring PARTIAL_WAKE_LOCK. 1.获得获得PARTIAL_WAKE_LOCK的许可。

2.Setting android:largeHeap="true" 2.设置android:largeHeap =“ true”

3.Foreground services 3,前台服务

According to Doze mode documentation it ignores wake locks. 根据打ze模式文档,它会忽略唤醒锁。
Also starting from Android Pie it is impossible to gain camera access from background app. 同样从Android Pie开始,无法从后台应用程序获得相机访问权限。
Also according to android developer guidelines you must free camera, when you app is paused, because it will block all other apps. 另外,根据android开发人员指南,当您的应用暂停时,您必须释放相机,因为它会阻止所有其他应用。

onSaveInstanceState function should be called in these situations, check with you and if its called then open the activity again with the delay. 在这种情况下,应调用onSaveInstanceState函数,并与您检查是否被调用,然后在延迟的情况下再次打开活动。

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
            try {
                Intent intent = new Intent(getBaseContext(), YourActivity.class);
                startActivity(intent);
                finish();
            } catch (Exception ex) { }
      }
    }, 200);

}

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

相关问题 屏幕关闭时如何开始活动? - How do i start activity when screen goes off? 在Activity / App进入后台后60秒调用方法 - Calling a method 60 seconds after Activity/App goes background RecyclerView仅在屏幕关闭后更改 - RecyclerView only changes after the screen goes off 关闭/锁定屏幕-不启动活动-Android - Turn Off/Lock Screen - Without Launching the Activity - Android 球离开屏幕 - ball goes off the screen 我有一个没有反应的按钮。 在我关闭屏幕并将其重新打开后,它会响应。 其他按钮工作正常 - I have a button that doesnt respond. After I turn off screen and turn it back on it responds. Other button works fine 如果我退出活动后打开cocos 2d项目,则android屏幕会变黑 - android screen goes black if I open cocos 2d project after getting out of an activity 我应该从哪里开始一个应该在应用程序进入后台时触发的活动? - Where should I start an activity which is suppose to be triggered when app goes to background? 如何防止手机屏幕关闭后音乐停止播放? - How to prevent music to stop after the phone's screen goes off? 试图避免子弹离开屏幕后强制关闭 - Trying to avoid a Force close after a bullet goes off screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM