Reputation: 51
I have a call recorder app.
As you know, for Android 10 and above, I have to use the AccessibilityService.
Everything is working just fine from the start but after a while onServiceConnected is no longer running even though accessibility service is ON.
I have no idea why or how to find a cause of this problem.
For example, when I restart the phone, onServiceConnected no longer runs and other cases.
MainActivity code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 29) {
if (!isAccessibilityServiceEnabled()) {
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
}
}
}
private boolean isAccessibilityServiceEnabled() {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + RecordingAccessibilityService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(getApplicationContext().getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
}
return false;
}
AccessibilityService code:
public class RecordingAccessibilityService extends AccessibilityService {
@Override
protected void onServiceConnected() {
instance = this;
super.onServiceConnected();
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
Toast.makeText(getApplicationContext(), "onServiceConnected", Toast.LENGTH_SHORT).show();
updateNotification();
startRecord();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
stopRecord();
}
super.onCallStateChanged(state, incomingNumber);
}
};
if (mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
Manifest Code:
<service
android:name=".RecordingAccessibilityService"
android:exported="false"
android:label="@string/accessibility_description"
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>
accessibility service config code:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowContentChanged|typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagReportViewIds|flagRetrieveInteractiveWindows"
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_description"
android:label="@string/app_name"
android:notificationTimeout="100" />
Tested on Huawei y9 phone with android 10.
Upvotes: 1
Views: 1250
Reputation: 1610
@ShahinSadeghi Accessibility Service onServiceConnected
function is called only when the user turns on your accessibility service, not afterwards. So in your MainActivity you should implement a button which when pressed should invoke the function which you defined in onServiceConnected.
You can also do that in onAccessibilityEvent function based on some criteria rather than onServiceConnected.
Upvotes: 1
Reputation: 5881
According to Google's new policies you can no longer use Accessibility Services to perform call recording article from the Verge, article from digital news today and announced in their 2022 Policy update.
“Only services that are designed to help people with disabilities access their device or otherwise overcome challenges stemming from their disabilities are eligible to declare that they are accessibility tools.”
I'm not sure if this is the reason why you are facing the problem you are facing, but it will be pertinent information for anyone looking at this question in the future.
Upvotes: 0