简体   繁体   English

Android在运行过程中“暂停”调用“活动暂停”

[英]Android “something” call onPause of Activity during running

I try to implement recording app. 我尝试实现录音应用。 I have problem with Activity life cycle . 我对Activity life cycleActivity life cycle After some time (5min, 25min, 27 min, ... it is different) "something" call onPause method of Activity . 一段时间(5分钟,25分钟,27分钟……不同)之后,“某物”调用了Activity onPause方法。 It is problem for me, because i have some releases ( Camera , recording , GPS , etc.). 这对我来说是个问题,因为我发布了一些版本( CamerarecordingGPS等)。 When i comment all releases in onPause method, everything is OK, i could recording over 60 min. 当我用onPause方法注释所有发行版时,一切正常,我可以录制60分钟以上。 Nobody touch on device. 没人触摸设备。

Could i get information, who is caller onPause method? 我可以获取信息吗,谁是onPause方法的调用者?

Edit: 编辑:
I tried new created application without any logic, and onPause is called too (after 20 min.). 我尝试了没有逻辑的新创建的应用程序,并且也调用了onPause (20分钟后)。 I think, that problem is out of application. 我认为,该问题已不适用。 I have device Huawei Honor 4C, Android 5.1.1 with EMUI 3.1. 我有设备Huawei Honor 4C,Android 5.1.1 with EMUI 3.1。 Maybe device has some "watchdog logic". 设备可能具有一些“看门狗逻辑”。

Edit2: 编辑2:
I tried (with Huawei) record with native camera application, and it was automatically stopped after 15 minutes. 我尝试使用本机相机应用程序(与华为合作)录制,但15分钟后自动停止。 (SDcard is not full, and file does not have > 4GB (FAT32)). (SD卡未满,文件没有> 4GB(FAT32))。 I tried (with Nexus) and app run over 60mins. 我尝试(使用Nexus)并运行了60分钟。 without call onPause. 无需调用onPause。

SOLVED: It's device-dependent. 求助:这取决于设备。

I have enabled wakeLock (I have permission WAKE_LOCK in manifest.): 我启用了wakeLock(清单中有WAKE_LOCK权限。):

//KEEP SCREEN ON
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();

Structure: 结构体:

Activity (MediaProjectionActivity4 class)   
|    
| -- Thread of recording video (ScreenRecorder class)
| -- Thread of recording audio (AudioThread class)
| -- Thread of writing to file (SyncFile class)

After added: Log.e(TAG, "onPause()", new RuntimeException()); 添加后: Log.e(TAG, "onPause()", new RuntimeException()); to onPause method. onPause方法。 (onPause was called after 12 minutes). (onPause在12分钟后被调用)。 I added pre and post log messages of call onPause by LogCat without filtering. 我通过LogCat添加了onPause调用之前和之后的日志消息,而没有进行过滤。

03-16 16:14:14.600 25668-25668/com.example.mytestapp I/MainActivity: onCreateOptionsMenu()
03-16 16:40:40.770 25668-25668/com.example.mytestapp E/MainActivity: onPause()
 java.lang.RuntimeException
     at com.example.mytestapp.MainActivity.onPause(MainActivity.java:97)
     at android.app.Activity.performPause(Activity.java:6225)
     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1321)
     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3510)
     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3483)
     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3453)
     at android.app.ActivityThread.access$1400(ActivityThread.java:163)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5595)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
03-16 16:40:40.810 25668-25668/com.example.mytestapp I/MainActivity: onResume()
03-16 17:10:41.560 25668-25668/com.example.mytestapp E/MainActivity: onPause()
 java.lang.RuntimeException
     at com.example.mytestapp.MainActivity.onPause(MainActivity.java:97)
     at android.app.Activity.performPause(Activity.java:6225)
     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1321)
     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3510)
     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3483)
     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3453)
     at android.app.ActivityThread.access$1400(ActivityThread.java:163)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5595)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
03-16 17:10:41.590 25668-25668/com.example.mytestapp I/MainActivity: onResume()
03-16 17:40:42.360 25668-25668/com.example.mytestapp E/MainActivity: onPause()
 java.lang.RuntimeException
     at com.example.mytestapp.MainActivity.onPause(MainActivity.java:97)
     at android.app.Activity.performPause(Activity.java:6225)
     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1321)
     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3510)
     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3483)
     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3453)
     at android.app.ActivityThread.access$1400(ActivityThread.java:163)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5595)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
03-16 17:40:42.390 25668-25668/com.example.mytestapp I/MainActivity: onResume()

Android is responsible for calling all the lifecycle methods on an Activity. Android负责在Activity上调用所有生命周期方法。 It's called when the activity is no longer receiving input focus, which means something else is probably showing on top of it, such as a dialog. 当活动不再获得输入焦点时调用它,这意味着可能在其上方显示其他内容,例如对话框。 You can read the documentation for that and other lifecycle methods here . 您可以在此处阅读有关该方法和其他生命周期方法的文档。

There may be several things responsible for it , but you can prevent the calling of onPause by making the app not going to sleep. 可能有几种原因,但是可以通过使应用程序不进入睡眠状态来阻止onPause的调用。

you can set 你可以设置

setKeepScreenOn(true)

on the view involve, this will also work 在涉及的观点上,这也将起作用

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

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

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