Langsung ke konten utama

How Create a Accessibility Service in Android

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.

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.xml
Don'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.java
Open 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");
}

}

Important Note

Finally go to Settings --> Accessibility --> Your Application --> Turn ON

Komentar

Postingan populer dari blog ini

Download file using Okio in Android

Okio is a library that complements java.io and java.nio to make it much easier to access, store, and process your data. Simply Okio is a modern I/O API for Java.  In this post, we will see how to download image or any file using Okio. Okio is component for OkHttp Coding Part Create a new project in Android Studio. Add following dependencies to your  app-level  build.gradle  file. compile 'com.squareup.okhttp3:okhttp:3.6.0' Don't forget to add the following permission in your AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> Implementation Paste the following code in your Activity and Here, I have kept as MainActivity.java public void downloadImg(View view) { try { Request request = new Request.Builder() .url(imageLink) .build(); new OkHttpClient().newCall(request).enqueue(new Callback() { @Override public void onFail...

QR-Code Generator - Library

In this Post, I introduce my new Gradle Library. This Library is used to Generate QR Code Automatically for our specified input. How to Import the Library: Gradle: compile 'androidmads.library.qrgenearator:QRGenearator:1.0.0' Permission: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> How to use this Library: After importing this library, use the following lines to use this library. The following lines are used to generated the QR Code // Initializing the QR Encoder with your value to be encoded, type you required and Dimension QRGEncoder qrgEncoder = new QRGEncoder(inputValue, null, QRGContents.Type.TEXT, smallerDimension); try { // Getting QR-Code as Bitmap bitmap = qrgEncoder.encodeAsBitmap(); // Setting Bitmap to ImageView qrImage.setImageBitmap(bitmap); } catch (WriterException e) { Log.v(TAG, e.toString()); } Save QR Code as Image // Save with location, value, bitmap returned and type of Image(JPG/PNG). QRGSaver.save(s...