In my program, I have a switch, a text view and an accessibility service running.
When the switch is turned ON, the second screen opens from where I need to switch on my service and by doing this, the text in the first screen should change to ON(the text should change to ON only if the service is enabled).
But the problem is that the text in the first screen is changing to ON, as soon as I switch ON the switch, but before I enable my service from the second screen.
This is my code for the switch:
private void setSwitchAndText(){
boolean ifON;
ifON = isAccessibilitySettingsOn(getApplicationContext());
if(ifON) {
mySwitch.setChecked(true);
} else {
mySwitch.setChecked(false);
}
myTextview.setText(ifON ? R.string.on : R.string.off);
}
and this is how I check whether the service is enable or not:
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + myService.class.getCanonicalName();
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.d(LOGTAG, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.d(LOGTAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.d(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.d(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
if (accessibilityService.equalsIgnoreCase(service)) {
Log.d(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return accessibilityFound;
}
and this is the relevant code in the onCreate:
myTextview = (TextView) findViewById(R.id.my_text);
mySwitch = (Switch) findViewById(R.id.my_switch);
mySwitch.setChecked(false);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myTextview.setText(isChecked ? R.string.on : R.string.off);
if (isChecked) {
askToStartAccessibilityService(getResources().getString(R.string.on));
} else {
askToStartAccessibilityService(getResources().getString(R.string.off));
}
}
});
The way your detecting active accessibility services is pretty intense and there's no reason for this. Try this code instead:
AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
List<AccessibilityServiceInfo> serviceInfoList = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN);
return (serviceInfoList.size() > 0);
Related
Hi i have created accessibility service permission using below code .
public static boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = mContext.getPackageName() + "/" + YouTubeFilterService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
if (accessibilityService.equalsIgnoreCase(service)) {
Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}
This is working properly in android 8 above when i give permission its save permission status but in android 8.0 and android 8.1 when i give permission to ON and again if i kill and launch then its asking again permission while it should be save permission status please help me what i am doing wrong .
I know the following method can be used, But sometimes that doesn't work.
public void removeNetwork(String sSid){
WifiConfiguration tempConfig = isExist(sSid);
if (tempConfig != null) {
try {
boolean isRemoved = mWifiManager.removeNetwork(tempConfig.networkId);
boolean isDisabled = mWifiManager.disableNetwork(tempConfig.networkId);
boolean isSaved = mWifiManager.saveConfiguration();
Log.i(TAG, "isRemoved: "+ isRemoved + ", isDisabled" + isDisabled+ ", isSaved" + isSaved);
} catch (Exception e){
Log.i(TAG, "RemoveNetwork Exception: "+e.getMessage());
}
}
}
I'm looking at creating my gesture lock screen, which will be shown once screen is on.
It can not achieved using screen on\off broadcast receiver as they won't work if registered from manifest. And you can not keep service running all time.
My Service looks like.
public class CustomAccessibilityService extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
// event.getSource(), findFocus(int), getWindows(), or getRootInActiveWindow().
Toast.makeText(this, "onAccessibilityEvent" + event.getSource(), Toast.LENGTH_SHORT).show();
}
#Override
public void onInterrupt() {
Toast.makeText(this, "onInterrupt", Toast.LENGTH_SHORT).show();
}
#Override
protected boolean onGesture(int gestureId) {
Toast.makeText(this, "onGesture" + gestureId, Toast.LENGTH_SHORT).show();
return super.onGesture(gestureId);
}
}
Manifest file
<service
android:name=".CustomAccessibilityService"
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/service_configuration" />
</service>
And Service_configuration.xml as below.
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewAccessibilityFocused|typeViewAccessibilityFocusCleared"
android:accessibilityFeedbackType="feedbackAllMask"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100"
android:packageNames="com.xxxx.gesturelockscreen" />
I am stuck on it.
I know that we need to activate accessibility service (Gesture lock screen) and I am using below code to check if it is enabled.
if (!isAccessibilityEnabled()) {
Toast.makeText(this, "Not Enabled!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);
} else {
//Good to go!
}
public boolean isAccessibilityEnabled() {
int accessibilityEnabled = 0;
final String ACCESSIBILITY_SERVICE_NAME = "com.xxx.gesturelockscreen/com.xxx.gesturelockscreen.CustomAccessibilityService";
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.d(TAG, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.d(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.d(TAG, "***ACCESSIBILIY IS ENABLED***: ");
String settingValue = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
Log.d(TAG, "Setting: " + settingValue);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
Log.d(TAG, "Setting: " + accessabilityService);
if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)) {
Log.d(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
Log.d(TAG, "***END***");
} else {
Log.d(TAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}
Now I want when screen is on my custom accessibility service's need to be called.
Please help, any help is appreciated, thanks in advance.
I have a weird problem. When i click the first time on the checkbox it works, but when i uncheck and check again, the image won`t show.
Please help.
private void ratedialog() {
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Rate it!");
dialog.setCancelable(true);
dialog.show();
seekbar = (SeekBar) dialog.findViewById(R.id.rate_seekbar);
button_done = (Button) dialog.findViewById(R.id.button_done);
seekbar_result = (TextView) dialog.findViewById(R.id.seekbar_result);
checkBox_seen = (CheckBox) dialog.findViewById(R.id.checkBox_seen);
button_done.setOnClickListener(EditActivity.this);
seekbar.setOnSeekBarChangeListener(EditActivity.this);
checkBox_seen.setOnCheckedChangeListener(EditActivity.this);
seekbar.setProgress(rate);
}
Listener:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e(TAG, isChecked+"");
if (isChecked) {
//seen = true;
checkBox_seen.setChecked(true);
Log.e(TAG, "Second time " + isChecked + "");
image_seen.setImageResource(R.drawable.seen);
} else {
// seen = false;
checkBox_seen.setChecked(false);
Log.e(TAG, "Third time" + isChecked + "");
image_seen.setVisibility(View.INVISIBLE);
}
}
change your code to
if (isChecked) {
//seen = true;
checkBox_seen.setChecked(true);
Log.e(TAG, "Second time " + isChecked + "");
image_seen.setVisibility(View.VISIBLE);
image_seen.setImageResource(R.drawable.seen);
} else {
// seen = false;
checkBox_seen.setChecked(false);
Log.e(TAG, "Third time" + isChecked + "");
image_seen.setVisibility(View.INVISIBLE);
}
In Else condition you change the visiblity to INVISIBLE so it become invisible when your if condition runs you do not change the Visiblity to visible so that's why you image not shows.
Problem is you are not changing the visibility in checked state setting the CheckBoxinstance checked again.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e(TAG, isChecked+"");
if (isChecked) {
//seen = true;
**checkBox_seen.setChecked(true);// remove this line**
Log.e(TAG, "Second time " + isChecked + "");
**image_seen.setVisibility(View.VISIBLE);//Add this line**
image_seen.setImageResource(R.drawable.seen);//Move this line to `ratedialog()`method.
} else {
// seen = false;
**checkBox_seen.setChecked(false);//Remove this line**
Log.e(TAG, "Third time" + isChecked + "");
image_seen.setVisibility(View.INVISIBLE);
}
}
The simple code will look like
if (isChecked) {
image_seen.setVisibility(View.VISIBLE);
} else {
image_seen.setVisibility(View.INVISIBLE);
}
I was wondering how could I detect if my own service is enabled. So I could check if my service is not enabled, then tell the user to enable it.
Below is the method to check if your accessibility service is enabled or not.
Note: Change value of YOURAccessibilityService with your Service.
// To check if service is enabled
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + YOURAccessibilityService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
if (accessibilityService.equalsIgnoreCase(service)) {
Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}
And to call this method:
if (!isAccessibilitySettingsOn(getApplicationContext())) {
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
}
This will check and launch accessibility settings if not enabled.
This is a modified version of Jakub Bláha's answer in java.
public boolean isAccessServiceEnabled(Context context, Class accessibilityServiceClass)
{
String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
return prefString!= null && prefString.contains(context.getPackageName() + "/" + accessibilityServiceClass.getName());
}
This is somehow a smaller version, but it is working.
fun isAccessServiceEnabled(context: Context): Boolean {
val prefString =
Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return prefString.contains("${context.packageName}/${context.packageName}.${context.getString(R.string.access_service_name)}")
}
Feel free to correct me if there is something missing.
1:Kotlin Based answer
2:Added a current package name check as well cos it will return true only if accessibility service will be Enabled for current package, Recently it was returning true if any package accessibility service enabled
private fun checkAccessibilityPermission(): Boolean {
var isAccessibilityEnabled = false
(requireActivity().getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager).apply {
installedAccessibilityServiceList.forEach { installedService ->
installedService.resolveInfo.serviceInfo.apply {
if (getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK).any { it.resolveInfo.serviceInfo.packageName == packageName && it.resolveInfo.serviceInfo.name == name && permission == Manifest.permission.BIND_ACCESSIBILITY_SERVICE && it.resolveInfo.serviceInfo.packageName == requireActivity().packageName })
isAccessibilityEnabled = true
}
}
}
if (isAccessibilityEnabled.not())
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
return isAccessibilityEnabled
}
Kotlin version based on answer of #Pankaj Kumar
inline fun <reified T> Context.isAccessibilityEnabled(): Boolean {
var enabled = 0
try {
enabled = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED)
} catch (e: SettingNotFoundException) {
Timber.e(e)
}
if (enabled == 1) {
val name = ComponentName(applicationContext, T::class.java)
val services = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return services?.contains(name.flattenToString()) ?: false
}
return false
}