Reputation: 3730
I'm trying to detect long press on any area of the screen using Accessiblity Service , I have set up the code but it is not working .. any help would be appreciated
** Accessibility Service
class RecentAppsAccessibilityService : AccessibilityService() {
private var touchStartTime: Long = 0
private val longPressThreshold = 500
private val lockedApps = mutableSetOf<String>()
private lateinit var database: DatabaseReference
private val appPinCodes = mutableMapOf<String, String>()
companion object {
private const val TAG = "RecentAppsService"
}
override fun onCreate() {
super.onCreate()
database = FirebaseDatabase.getInstance().reference
fetchLockedPackages()
}
private fun fetchLockedPackages() {
database.child("childApp").addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
appPinCodes.clear() // Clear previous data
for (childSnapshot in dataSnapshot.children) {
val packageName = childSnapshot.child("package_name").getValue(String::class.java) ?: ""
val pinCode = childSnapshot.child("pin_code").getValue(String::class.java) ?: ""
if (packageName.isNotEmpty() && pinCode.isNotEmpty()) {
appPinCodes[packageName] = pinCode
lockedApps.add(packageName)
}
}
Log.d(TAG, "Updated locked apps: $lockedApps")
}
override fun onCancelled(databaseError: DatabaseError) {
Log.e("FirebaseError", "Error fetching data: ${databaseError.message}")
}
})
}
override fun onAccessibilityEvent(event: AccessibilityEvent) {
when (event.eventType) {
AccessibilityEvent.TYPE_VIEW_LONG_CLICKED -> {
Log.d(TAG, "Long press detected via manual method")
}
}
}
}
// Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.SET_COMPONENT_ENABLED_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"
tools:ignore="ProtectedPermissions" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LockCompose"
tools:targetApi="31">
<service android:name=".FirebaseListenerService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="specialUse"/>
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.LockCompose">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".x.RecentAppsAccessibilityService"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
</application>
</manifest>
// Service Config
<accessibility-service android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackSpoken"
android:accessibilityFlags="flagDefault"
android:canRequestFilterKeyEvents="true"
android:notificationTimeout="100"
android:packageNames="com.app.lockcomposeChild"
android:canRetrieveWindowContent="true"
xmlns:android="http://schemas.android.com/apk/res/android" />
Upvotes: 1
Views: 42
Reputation: 378
You can detect Long press by using below code
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
if (event?.eventType == AccessibilityEvent.TYPE_VIEW_LONG_CLICK) {
// LongPress Detected here
}
}
Upvotes: 0