GIT 提交信息:

优化突发新闻应用的代码结构。

- 通过实施新的服务检查和设置来增强可访问性功能。
- 优化 `MainActivity` 的代码结构,以提高可读性和可维护性。
- 在 `MyAccessibilityService` 中引入静态实例机制,以便于访问和管理。
This commit is contained in:
lvlisong 2025-01-21 20:48:17 +08:00
parent 94b09a235f
commit 7713c2cfc3
3 changed files with 74 additions and 67 deletions

View File

@ -4,6 +4,7 @@
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="testRunner" value="CHOOSE_PER_TEST" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" /> <option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
<option name="modules"> <option name="modules">

View File

@ -27,54 +27,26 @@ import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxAdView import com.applovin.mediation.ads.MaxAdView
import io.sixminutes.breakingnews.ui.theme.BreakingnewsTheme import io.sixminutes.breakingnews.ui.theme.BreakingnewsTheme
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
private val gaidHelper by lazy { GAIDHelper(this) } private val gaidHelper by lazy { GAIDHelper(this) }
private val TAG = "BreakingNews" private val TAG = "BreakingNews"
private var accessibilityService: MyAccessibilityService? = null private var accessibilityService: MyAccessibilityService? = null
private lateinit var enableAccessibilityLauncher: ActivityResultLauncher<Intent>
private val ACCESSIBILITY_SERVICE_NAME = "io.sixminutes.breakingnews.MyAccessibilityService" private val ACCESSIBILITY_SERVICE_NAME = "io.sixminutes.breakingnews.MyAccessibilityService"
// ActivityResultLauncher 用于启动无障碍设置页面
private val connection = object : ServiceConnection { private lateinit var enableAccessibilityLauncher: ActivityResultLauncher<Intent>
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
if (service is MyAccessibilityService.MyBinder) {
accessibilityService = (service as MyAccessibilityService.MyBinder).getService()
Log.d(TAG, "Accessibility service connected")
} else {
Log.e(TAG, "Invalid binder type")
}
}
override fun onServiceDisconnected(name: ComponentName?) {
accessibilityService = null
Log.d(TAG, "Accessibility service disconnected")
}
}
private fun checkAndRequestAccessibilityPermission() {
val enabledServices = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
if (enabledServices == null ||!enabledServices.contains(ACCESSIBILITY_SERVICE_NAME)) {
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
enableAccessibilityLauncher.launch(intent)
Log.d(TAG, "Accessibility service is not enabled. Redirecting user to settings.")
} else {
Log.d(TAG, "Accessibility service is already enabled.")
continueMainActivityExecution()
}
}
private fun continueMainActivityExecution() {
// 在这里添加 MainActivity 的后续操作,例如更新 UI 或执行其他任务
Log.d(TAG, "Continuing MainActivity execution after accessibility service is enabled.")
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// 绑定服务 // 初始化 ActivityResultLauncher
val intent = Intent(this, MyAccessibilityService::class.java) enableAccessibilityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
bindService(intent, connection, Context.BIND_AUTO_CREATE) // 用户从设置页面返回后,再次检查无障碍服务是否可用
checkAccessibilityService()
}
// 检查无障碍服务是否可用
checkAccessibilityService()
setContent { setContent {
BreakingnewsTheme { BreakingnewsTheme {
@ -104,27 +76,44 @@ class MainActivity : ComponentActivity() {
} }
} }
} }
enableAccessibilityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val enabledServices = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
if (enabledServices!= null && enabledServices.contains(ACCESSIBILITY_SERVICE_NAME)) {
Log.d(TAG, "Accessibility service has been enabled.")
continueMainActivityExecution()
} else {
Log.d(TAG, "Accessibility service is still not enabled.")
}
}
}
checkAndRequestAccessibilityPermission()
// 显示插页式广告 // 显示插页式广告
//InterstitialAdManager.showAd(this) //InterstitialAdManager.showAd(this)
} }
/**
* 检查无障碍服务是否可用如果不可用则打开设置页面
*/
private fun checkAccessibilityService() {
accessibilityService = MyAccessibilityService.getInstance()
if (accessibilityService == null) {
Log.e(TAG, "Accessibility service is not available")
openAccessibilitySettings()
} else {
Log.d(TAG, "Accessibility service is available: $accessibilityService")
continueMainActivityExecution()
}
}
/**
* 打开无障碍设置页面
*/
private fun openAccessibilitySettings() {
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
enableAccessibilityLauncher.launch(intent)
Log.d(TAG, "Opening accessibility settings")
}
/**
* 无障碍服务可用后继续执行 MainActivity 的逻辑
*/
private fun continueMainActivityExecution() {
// 在这里添加 MainActivity 的后续操作,例如更新 UI 或执行其他任务
Log.d(TAG, "Continuing MainActivity execution after accessibility service is enabled.")
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
// 解除服务绑定
unbindService(connection)
} }
// 调用点击功能的方法 // 调用点击功能的方法

View File

@ -10,12 +10,19 @@ import android.view.accessibility.AccessibilityEvent
import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Rect
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
class MyAccessibilityService : AccessibilityService() { class MyAccessibilityService : AccessibilityService() {
private val TAG = "BreakingNews" private val TAG = "BreakingNews"
private var serviceInstance: MyAccessibilityService? = null private var serviceInstance: MyAccessibilityService? = null
private val binder = MyBinder() private val binder = MyBinder()
companion object {
private var instance: MyAccessibilityService? = null
fun getInstance(): MyAccessibilityService? {
return instance
}
}
inner class MyBinder : Binder() { inner class MyBinder : Binder() {
fun getService(): MyAccessibilityService? { fun getService(): MyAccessibilityService? {
Log.d(TAG, "Binder getService called") Log.d(TAG, "Binder getService called")
@ -26,7 +33,16 @@ class MyAccessibilityService : AccessibilityService() {
override fun onServiceConnected() { override fun onServiceConnected() {
super.onServiceConnected() super.onServiceConnected()
serviceInstance = this serviceInstance = this
Log.d(TAG, "Service connected") instance = this // 设置静态实例
Log.d(TAG, "Service connected, instance set to $instance")
// 无障碍服务已启用,启动 MainActivity
startMainActivity()
}
override fun onDestroy() {
super.onDestroy()
instance = null // 清除静态实例
} }
override fun onInterrupt() { override fun onInterrupt() {
@ -39,17 +55,10 @@ class MyAccessibilityService : AccessibilityService() {
override fun onUnbind(intent: Intent?): Boolean { override fun onUnbind(intent: Intent?): Boolean {
serviceInstance = null serviceInstance = null
Log.d(TAG, "Service unbound, serviceInstance set to null")
return super.onUnbind(intent) return super.onUnbind(intent)
} }
companion object {
private var instance: MyAccessibilityService? = null
fun getInstance(): MyAccessibilityService? {
return instance
}
}
fun simulateTap(x: Int, y: Int) { fun simulateTap(x: Int, y: Int) {
val rootNode = AccessibilityNodeInfoCompat.wrap(rootInActiveWindow) val rootNode = AccessibilityNodeInfoCompat.wrap(rootInActiveWindow)
if (rootNode != null) { if (rootNode != null) {
@ -82,4 +91,12 @@ class MyAccessibilityService : AccessibilityService() {
} }
} }
} }
private fun startMainActivity() {
// 创建 Intent 启动 MainActivity
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK // 必须设置 FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
Log.d(TAG, "MainActivity started")
}
} }