我实现了Android锁屏应用。
执行LockScreenActivity时发生RuntimeException(无论Android版本如何)。 但是此异常是间歇性发生的,不会报告给Crashlytics。
例外内容为:
2018-11-20 16:09:10.037 7657-7657/hanmo.com.drinkingwaterassistant E/ActivityThread: Performing stop of activity that is already stopped: {hanmo.com.drinkingwaterassistant/hanmo.com.drinkingwaterassistant.lockscreen.LockScreenActivity}
java.lang.RuntimeException: Performing stop of activity that is already stopped: {hanmo.com.drinkingwaterassistant/hanmo.com.drinkingwaterassistant.lockscreen.LockScreenActivity}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4200)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4288)
at android.app.ActivityThread.-wrap25(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1730)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
首先,我重现了这个间歇性异常,它是我想发生的。
繁殖条件如下。
透明活动的样式:
<style name="transparentView" parent="android:Theme.Translucent">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
当透明活动出现在lockScreenActivity上时发生。
所以我做了一个非透明的lockScreenActivity。但是,Exception会间歇性地持续。
我想知道此异常的原因。
这是LockScreenActivity中我可疑的部分。
清单
<activity android:name=".lockscreen.LockScreenActivity"
android:showOnLockScreen="true"
android:launchMode="singleInstance" />
在LockScreenActivity中onWindowFocusChanged
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (hasFocus) window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
)
}
}
LockScreenActivity中的onAttachedToWindow
override fun onAttachedToWindow() {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
)
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
super.onAttachedToWindow()
}
在LockScreenActivity中启用Admob
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/transparent"
app:adSize="SMART_BANNER"
app:adUnitId="@string/admobBanner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
我的LockScreenActivity示例为here