Browse Source

1.处理MainActivity拉起问题。

0423x6e6f
wangwanlei 1 day ago
parent
commit
ca35b1d48f

+ 26
- 2
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt View File

@@ -8,7 +8,6 @@ import android.Manifest
8 8
 import android.annotation.SuppressLint
9 9
 import android.app.ActivityManager
10 10
 import android.app.AlertDialog
11
-import android.bluetooth.BluetoothAdapter
12 11
 import android.content.ComponentName
13 12
 import android.content.Context
14 13
 import android.content.Intent
@@ -176,6 +175,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
176 175
 
177 176
     //判断是否在onresume状态
178 177
     private var isResumeFlag = true
178
+    //执行了自启请求
179
+    private var netAutoAppFlag=false
179 180
 
180 181
     override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
181 182
 
@@ -580,6 +581,11 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
580 581
         viewModel.viewModelScope.launch {
581 582
             viewModel.autoAppData.collect {
582 583
                 it?.let {
584
+                    if (!netAutoAppFlag){
585
+                        return@collect
586
+                    }else{
587
+                        netAutoAppFlag=false
588
+                    }
583 589
                     //连接不为空,并且应用已安装则跳转应用
584 590
                     /* it.authUrl="com.xhkjedu.xh_control_browser.MainActivity"
585 591
                      it.appPackage="com.xhkjedu.xh_control_browser"*/
@@ -596,6 +602,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
596 602
                             } else if ((!it.authUrl.isNullOrEmpty()&&!it.appPackage.isNullOrEmpty()) && AppUtils.isAppInstalled(this@MainActivity, it.appPackage)) {
597 603
                                 //请求成功地址不为空,并且安装了跳转
598 604
                                 spUtils.saveJson(Const.AUTOAPPKEY, it)
605
+                                LogShow("打开app拉起111")
599 606
                                 startAutoApp(it)
600 607
                             } else if (!it.netSucessFlag){
601 608
                                 //请求失败走缓存
@@ -606,6 +613,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
606 613
                                 if ((!autoAppBean.authUrl.isNullOrEmpty()&&!autoAppBean.appPackage.isNullOrEmpty())&&
607 614
                                     AppUtils.isAppInstalled(this@MainActivity, autoAppBean.appPackage) && !it.netSucessFlag) {
608 615
                                     //缓存地址不为空,并且安装了则跳转
616
+                                    LogShow("打开app拉起222")
609 617
                                     startAutoApp(autoAppBean)
610 618
                                 } else {
611 619
                                     //其余情况都允许触控
@@ -640,6 +648,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
640 648
         }
641 649
 
642 650
         lifecycleScope.launch {
651
+            isCreatFlag = true
643 652
             withContext(Dispatchers.IO){
644 653
                 delay(2000)
645 654
             }
@@ -648,7 +657,6 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
648 657
             lastTimeLong = System.currentTimeMillis()
649 658
             getLocation()
650 659
             startWorkManager()
651
-            isCreatFlag = true
652 660
             updateAllStrategy()
653 661
         }
654 662
     }
@@ -657,6 +665,19 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
657 665
 
658 666
     private fun startAutoApp(autoAppBean: AutoAppBean) {
659 667
         try {
668
+            try {
669
+                val am = getSystemService(ACTIVITY_SERVICE) as ActivityManager
670
+                val runningTasks = am.getRunningTasks(1)
671
+                if (!runningTasks.isNullOrEmpty()){
672
+                    val contains = runningTasks[0].topActivity?.className.toString()
673
+                        .contains(autoAppBean.appPackage!!)
674
+                    if (contains){
675
+                        return
676
+                    }
677
+                }
678
+            }catch (e:Exception){
679
+
680
+            }
660 681
             val launchIntent = Intent()
661 682
             launchIntent.setComponent(
662 683
                 ComponentName(
@@ -1183,6 +1204,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
1183 1204
     }
1184 1205
 
1185 1206
     private fun useTimeStrategy() {
1207
+        LogShow("开始执行了吧哈哈")
1186 1208
         //获得sputils中的平板锁定策略
1187 1209
         try {
1188 1210
             //如果存储的是锁定则锁定
@@ -1229,7 +1251,9 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
1229 1251
         LogShow("锁定了吧" + mainLockFlag + "|||" + autoAppFlag)
1230 1252
         //自动退出时优先退出而不是拉起app
1231 1253
         if (autoAppFlag&&!loginoutFlag) {
1254
+            //只有网络请求之后两秒延迟后才执行
1232 1255
             userBean?.let {
1256
+                netAutoAppFlag=true
1233 1257
                 viewModel.postAutoAppDetail(it.regionid)
1234 1258
             }
1235 1259
         }

Loading…
Cancel
Save