Hello friends, In this Tutorial. In this post, I will show you how to use Accessibility Service for Android.
Before that, we need to know What is Accessibility Service.
Don't forget to add the following permission in your manifest file.
Open MainActivity.java and replace it with the following code
Open MyService.java and replace it with the following code
Accessibility Service
Here, I am taking an example for creating Virtual Service Button instead of Default Back Button.Project Structure:
Create a new Project in Android Studio with the required Specifications.Codes:
AndroidManifest.xmlDon't forget to add the following permission in your manifest file.
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Don't forget to add the following Lines in your manifest file to create service for your application.
<service
android:name=".MyService"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
</service>
MainActivity.java
Open MainActivity.java and replace it with the following code
package example.backbutton;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Permission for Manifest.permission.SYSTEM_ALERT_WINDOW in Android M
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1);
}
}
// Code to start the Service
startService(new Intent(getApplication(), MyService.class));
}
}
MyService.javaOpen MyService.java and replace it with the following code
package example.backbutton;
import android.accessibilityservice.AccessibilityService;
import android.annotation.SuppressLint;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.widget.ImageView;
public class MyService extends AccessibilityService {
WindowManager windowManager;
ImageView back,home,notification,minimize;
WindowManager.LayoutParams params;
AccessibilityService service;
@SuppressLint("RtlHardcoded")
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
back = new ImageView(this);
home = new ImageView(this);
minimize = new ImageView(this);
notification = new ImageView(this);
back.setImageResource(R.mipmap.ic_back);
home.setImageResource(R.mipmap.ic_home);
minimize.setImageResource(R.mipmap.ic_min);
notification.setImageResource(R.mipmap.ic_notification);
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
params.x = 10;
params.y = 50;
windowManager.addView(home, params);
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
params.x = 10;
params.y = 100;
windowManager.addView(minimize, params);
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
params.x = 10;
params.y = 150;
windowManager.addView(notification, params);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
} catch (Exception e) {
e.printStackTrace();
}
}
});
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
} catch (Exception e) {
e.printStackTrace();
}
}
});
notification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
performGlobalAction(AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS);
} catch (Exception e) {
e.printStackTrace();
}
}
});
minimize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.d("TAG", "onServiceConnected");
}
}
Buttons are not clickable... nothing happens on touching any of the four buttons. Plz help
ReplyDeleteMake sure that you have turned on the service for your application
DeleteSettings --> Accessibility --> Your Application --> Turn ON
good example..if you had added some screenshots it would have been better to understand whats going on.
ReplyDeleteusing accessibility service i can read whats app package name and mobile number when new message coming but i unable to get a messages, how can i read that message.thanks in advance.
ReplyDeletehow can i send message on whats app thorough app without using send option inside whats app
ReplyDeleteError is coming unable to add window token null is not valid is your activity running?
ReplyDeleteSorry now my problem is solved it was a spelling mistake...
ReplyDeleteBtw your content creativity is awesome...
I love it.. make these kinds of things more
How can you write the test cases for MyService that extends AccessibilityService.java that covers OnServiceConnected.
ReplyDelete