Skip to content

Commit 1f8dac1

Browse files
committed
完善:ContextCenter的逻辑,使其能正常获取Activity
1 parent 084a44b commit 1f8dac1

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

lib/src/main/java/com/pmm/silentupdate/core/ContextCenter.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import java.util.*
1414
*/
1515
internal object ContextCenter {
1616
private lateinit var applicationContext: WeakReference<Context>
17-
private val activityStack = Stack<WeakReference<Activity?>>()
17+
private val activityStack = Stack<Activity>()
1818

1919
internal fun getTopActivity(): Activity? {
2020
var targetActivity: Activity? = null
2121
try {
22-
targetActivity = activityStack.peek().get()
22+
targetActivity = activityStack.peek()
2323
} catch (e: Exception) {
2424
//do nothing
2525
}
@@ -40,11 +40,11 @@ internal object ContextCenter {
4040
context.registerActivityLifecycleCallbacks(object : ActivityLifeListener() {
4141

4242
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
43-
activityStack.add(WeakReference(activity))
43+
activityStack.add(activity)
4444
}
4545

4646
override fun onActivityDestroyed(activity: Activity) {
47-
activityStack.remove(WeakReference(activity))
47+
activityStack.remove(activity)
4848
}
4949
})
5050
}

lib/src/main/java/com/pmm/silentupdate/core/KTX.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ internal fun ContextWrapper?.showInstallDialog(uri: Uri, isForce: Boolean) {
357357
}
358358
}
359359
} catch (e: Exception) {
360+
this?.loge("showInstallDialog error : $e")
360361
//Resolve:android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@2132278 is not valid; is your activity running?
361362
}
362363
}
@@ -381,6 +382,7 @@ private fun ContextWrapper?.showCustomInstallDialog(uri: Uri?) {
381382
}
382383
)
383384
} catch (e: Exception) {
385+
this?.loge("showCustomInstallDialog error : $e")
384386
//Resolve:android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@2132278 is not valid; is your activity running?
385387
}
386388
}
@@ -410,6 +412,7 @@ internal fun ContextWrapper?.showDownloadDialog(
410412
}
411413
}
412414
} catch (e: Exception) {
415+
this?.loge("showDownloadDialog error : $e")
413416
//Resolve:android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@2132278 is not valid; is your activity running?
414417
}
415418
}
@@ -434,6 +437,7 @@ private fun ContextWrapper?.showCustomDownloadDialog(
434437
SPCenter.modifyDialogTime(Calendar.getInstance().time.time)//记录
435438
})
436439
} catch (e: Exception) {
440+
this?.loge("showCustomDownloadDialog error : $e")
437441
//Resolve:android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@2132278 is not valid; is your activity running?
438442
}
439443
}

0 commit comments

Comments
 (0)