简体   繁体   中英

Android “something” call onPause of Activity during running

I try to implement recording app. I have problem with Activity life cycle . After some time (5min, 25min, 27 min, ... it is different) "something" call onPause method of Activity . It is problem for me, because i have some releases ( Camera , recording , GPS , etc.). When i comment all releases in onPause method, everything is OK, i could recording over 60 min. Nobody touch on device.

Could i get information, who is caller onPause method?

Edit:
I tried new created application without any logic, and onPause is called too (after 20 min.). I think, that problem is out of application. I have device Huawei Honor 4C, Android 5.1.1 with EMUI 3.1. Maybe device has some "watchdog logic".

Edit2:
I tried (with Huawei) record with native camera application, and it was automatically stopped after 15 minutes. (SDcard is not full, and file does not have > 4GB (FAT32)). I tried (with Nexus) and app run over 60mins. without call onPause.

SOLVED: It's device-dependent.

I have enabled wakeLock (I have permission WAKE_LOCK in manifest.):

//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()); to onPause method. (onPause was called after 12 minutes). I added pre and post log messages of call onPause by LogCat without filtering.

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. 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.

you can set

setKeepScreenOn(true)

on the view involve, this will also work

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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