GIT 提交信息:
优化突发新闻应用的代码结构。 - 通过实施新的服务检查和设置来增强可访问性功能。 - 优化 `MainActivity` 的代码结构,以提高可读性和可维护性。 - 在 `MyAccessibilityService` 中引入静态实例机制,以便于访问和管理。
This commit is contained in:
parent
94b09a235f
commit
7713c2cfc3
|
@ -4,6 +4,7 @@
|
|||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="testRunner" value="CHOOSE_PER_TEST" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
|
||||
<option name="modules">
|
||||
|
|
|
@ -27,54 +27,26 @@ import com.applovin.mediation.MaxError
|
|||
import com.applovin.mediation.ads.MaxAdView
|
||||
import io.sixminutes.breakingnews.ui.theme.BreakingnewsTheme
|
||||
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private val gaidHelper by lazy { GAIDHelper(this) }
|
||||
private val TAG = "BreakingNews"
|
||||
private var accessibilityService: MyAccessibilityService? = null
|
||||
private lateinit var enableAccessibilityLauncher: ActivityResultLauncher<Intent>
|
||||
private val ACCESSIBILITY_SERVICE_NAME = "io.sixminutes.breakingnews.MyAccessibilityService"
|
||||
|
||||
|
||||
private val connection = object : ServiceConnection {
|
||||
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.")
|
||||
}
|
||||
// ActivityResultLauncher 用于启动无障碍设置页面
|
||||
private lateinit var enableAccessibilityLauncher: ActivityResultLauncher<Intent>
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// 绑定服务
|
||||
val intent = Intent(this, MyAccessibilityService::class.java)
|
||||
bindService(intent, connection, Context.BIND_AUTO_CREATE)
|
||||
// 初始化 ActivityResultLauncher
|
||||
enableAccessibilityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
// 用户从设置页面返回后,再次检查无障碍服务是否可用
|
||||
checkAccessibilityService()
|
||||
}
|
||||
|
||||
// 检查无障碍服务是否可用
|
||||
checkAccessibilityService()
|
||||
|
||||
setContent {
|
||||
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)
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查无障碍服务是否可用,如果不可用则打开设置页面
|
||||
*/
|
||||
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() {
|
||||
super.onDestroy()
|
||||
// 解除服务绑定
|
||||
unbindService(connection)
|
||||
}
|
||||
|
||||
// 调用点击功能的方法
|
||||
|
@ -135,7 +124,7 @@ class MainActivity : ComponentActivity() {
|
|||
|
||||
@Composable
|
||||
fun GAIDInfo(gaid: String?) {
|
||||
Text(text = gaid?: "Loading GAID...")
|
||||
Text(text = gaid ?: "Loading GAID...")
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
@ -181,4 +170,4 @@ object InterstitialAdManager {
|
|||
val intent = Intent(activity, InterstitialActivity::class.java)
|
||||
activity.startActivity(intent)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,12 +10,19 @@ import android.view.accessibility.AccessibilityEvent
|
|||
import androidx.compose.ui.geometry.Rect
|
||||
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
|
||||
|
||||
|
||||
class MyAccessibilityService : AccessibilityService() {
|
||||
private val TAG = "BreakingNews"
|
||||
private var serviceInstance: MyAccessibilityService? = null
|
||||
private val binder = MyBinder()
|
||||
|
||||
companion object {
|
||||
private var instance: MyAccessibilityService? = null
|
||||
|
||||
fun getInstance(): MyAccessibilityService? {
|
||||
return instance
|
||||
}
|
||||
}
|
||||
|
||||
inner class MyBinder : Binder() {
|
||||
fun getService(): MyAccessibilityService? {
|
||||
Log.d(TAG, "Binder getService called")
|
||||
|
@ -26,7 +33,16 @@ class MyAccessibilityService : AccessibilityService() {
|
|||
override fun onServiceConnected() {
|
||||
super.onServiceConnected()
|
||||
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() {
|
||||
|
@ -39,20 +55,13 @@ class MyAccessibilityService : AccessibilityService() {
|
|||
|
||||
override fun onUnbind(intent: Intent?): Boolean {
|
||||
serviceInstance = null
|
||||
Log.d(TAG, "Service unbound, serviceInstance set to null")
|
||||
return super.onUnbind(intent)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private var instance: MyAccessibilityService? = null
|
||||
|
||||
fun getInstance(): MyAccessibilityService? {
|
||||
return instance
|
||||
}
|
||||
}
|
||||
|
||||
fun simulateTap(x: Int, y: Int) {
|
||||
val rootNode = AccessibilityNodeInfoCompat.wrap(rootInActiveWindow)
|
||||
if (rootNode!= null) {
|
||||
if (rootNode != null) {
|
||||
val clickableNodes = ArrayList<AccessibilityNodeInfoCompat>()
|
||||
findClickableNodes(rootNode, clickableNodes)
|
||||
for (node in clickableNodes) {
|
||||
|
@ -76,10 +85,18 @@ class MyAccessibilityService : AccessibilityService() {
|
|||
val childCount = node.childCount
|
||||
for (i in 0 until childCount) {
|
||||
val child = node.getChild(i)
|
||||
if (child!= null) {
|
||||
if (child != null) {
|
||||
findClickableNodes(child, clickableNodes)
|
||||
child.recycle()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user