diff --git a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt index 9ee28d9c..2f96ec94 100644 --- a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt +++ b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt @@ -32,6 +32,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.cancel import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.io.File import javax.inject.Inject @@ -91,7 +92,7 @@ class MyAccessibilityService : AccessibilityService() { appPackageName = rootNodePackageName } - if (!whiteListRepository.isAppInWhiteList(rootNodePackageName) && scanTimes < 30) { + if (!whiteListRepository.isAppInWhiteList(rootNodePackageName) && scanTimes < 50) { val that = this serviceScope.launch { val targetRect = @@ -99,7 +100,9 @@ class MyAccessibilityService : AccessibilityService() { targetRect?.let { rect -> val rectStr = rect.toString() if (!clickedRect.contains(rectStr)) { - click(that, rect) + withContext(Dispatchers.Main) { + click(that, rect) + } clickedRect.add(rectStr) LogUtils.d("clicked: packageName is $rootNodePackageName rect is $rectStr") } @@ -120,7 +123,7 @@ class MyAccessibilityService : AccessibilityService() { scanTimes++ } catch (e: Exception) { -// LogUtils.e(e) + LogUtils.e(e) } } diff --git a/app/src/main/java/com/android/skip/service/MyForegroundService.kt b/app/src/main/java/com/android/skip/service/MyForegroundService.kt index 2da13d66..57771786 100644 --- a/app/src/main/java/com/android/skip/service/MyForegroundService.kt +++ b/app/src/main/java/com/android/skip/service/MyForegroundService.kt @@ -1,5 +1,6 @@ package com.android.skip.service +import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent @@ -12,6 +13,8 @@ import com.android.skip.R import com.android.skip.ui.main.MainActivity class MyForegroundService : Service() { + private lateinit var notification: Notification + override fun onBind(p0: Intent?): IBinder? { TODO("Not yet implemented") } @@ -28,8 +31,8 @@ class MyForegroundService : Service() { manager.createNotificationChannel(channel) val intent = Intent(this, MainActivity::class.java) - val pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_IMMUTABLE) - val notification = NotificationCompat.Builder(this, "SKIP_FOREGROUND_SERVICE") + val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) + notification = NotificationCompat.Builder(this, "SKIP_FOREGROUND_SERVICE") .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.notification_accessibility_service_running)) .setSmallIcon(R.drawable.favicon32) @@ -40,4 +43,10 @@ class MyForegroundService : Service() { startForeground(2, notification) } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + super.onStartCommand(intent, flags, startId) + startForeground(2, notification) + return START_STICKY + } } \ No newline at end of file