AndroidAccessibility (Auxiliary Function)

Source: Internet
Author: User
Android provides the Accessibility function and service to help users who are unable to conveniently use Android smartphones due to visual acuity, hearing, or other physical causes, including text-to-speech and tactile Inversion

Android provides the Accessibility function and service to help users who are unable to conveniently use Android smartphones due to visual acuity, hearing, or other physical causes, including text-to-speech and tactile Inversion

Android provides Accessibility for users who are unable to conveniently use Android smartphones due to visual acuity, hearing, or other physical causes.FunctionAnd services help these users operate devices more easily, including text-to-speech, tactile feedback, gesture operations, trackball and handle operations. Developers can build their own Accessibility services to enhance availability, such as sound prompts, physical feedback, and other optional operation modes.

Android AccessibilityFunctionMore and more powerful. Before Android 4.0, the systemAuxiliaryServiceFunctionSingle, only one-way window element information can be obtained, such as the user input content in the input box. After Android 4.1AuxiliaryThe service adds two-way interaction with window elements.AuxiliaryFunctionService Operation window elements, such as clicking a button.

Because the systemAuxiliaryThe service can obtain the window Element Information of the current application in real time, which may lead to the risk of privacy information leakage, such as obtaining the input content of a non-password input box. At the same timeAuxiliaryFunctionIt can also simulate the user's automatic click on the elements in the application, which also brings certain security risks.


This article implementsAuxiliaryThe service automatically installs, uninstalls, and forcibly stops applications.Function.


1. Configure the self-implemented MyAccessibilityService in the AndroidManifest. xml file.

 
     
      
      
      
      
                              
                                  
               
                  
              
                               
               
           
      
 

2. Configure parameters in res/xml/phone_accessibility.xml.

 

3. Launch the installation, uninstallation, and forced stop actions of MainActivity.

package com.jack.accessibility;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.Settings;import android.view.View;import android.app.Activity;import android.content.Intent;public class MainActivity extends Activity {    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.findViewById(R.id.activeButton).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent killIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);startActivity(killIntent);}});this.findViewById(R.id.installButton).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMyAccessibilityService.INVOKE_TYPE = MyAccessibilityService.TYPE_INSTALL_APP;String fileName = Environment.getExternalStorageDirectory() + "/test.apk"; File installFile = new File(fileName);if(installFile.exists()){installFile.delete();}try {installFile.createNewFile();FileOutputStream out = new FileOutputStream(installFile);byte[] buffer = new byte[512];InputStream in = MainActivity.this.getAssets().open("test.apk");int count;while((count= in.read(buffer))!=-1){out.write(buffer, 0, count);}in.close();out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive"); startActivity(intent);}});this.findViewById(R.id.uninstallButton).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMyAccessibilityService.INVOKE_TYPE = MyAccessibilityService.TYPE_UNINSTALL_APP;Uri packageURI = Uri.parse("package:com.example.test"); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); startActivity(uninstallIntent); }});this.findViewById(R.id.killAppButton).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMyAccessibilityService.INVOKE_TYPE = MyAccessibilityService.TYPE_KILL_APP;Intent killIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);Uri packageURI = Uri.parse("package:com.example.test"); killIntent.setData(packageURI);startActivity(killIntent);}});}}


4. In MyAccessibilityService, the application is installed, uninstalled, and forcibly stopped by means of automatic clicks.Function.

Package com. jack. accessibility; import java. util. list; import android. accessibilityservice. accessibilityService; import android. annotation. suppressLint; import android. util. log; import android. view. keyEvent; import android. view. accessibility. accessibilityEvent; import android. view. accessibility. accessibilityNodeInfo; @ SuppressLint ("NewApi") public class MyAccessibilityService extends AccessibilityService {p Ublic static int INVOKE_TYPE = 0; public static final int TYPE_KILL_APP = 1; public static final int TYPE_INSTALL_APP = 2; public static final int TYPE_UNINSTALL_APP = 3; public static void reset () {INVOKE_TYPE = 0 ;}@ Overridepublic void onAccessibilityEvent (AccessibilityEvent event) {// TODO Auto-generated method stubthis. processAccessibilityEnvent (event);} private void processAccessibilityEnvent (Access IbilityEvent) {Log. d ("test", event. eventTypeToString (event. getEventType (); if (event. getSource () = null) {Log. d ("test", "the source = null");} else {Log. d ("test", "event =" + event. toString (); switch (INVOKE_TYPE) {case TYPE_KILL_APP: processKillApplication (event); break; case TYPE_INSTALL_APP: processinstallApplication (event); break; case TYPE_UNINSTALL_APP: processUninstallApplication (event); B Reak; default: break ;}}@ Overrideprotected boolean onKeyEvent (KeyEvent event) {// TODO Auto-generated method stubreturn true;} @ Overridepublic void onInterrupt () {// TODO Auto-generated method stub} private void processUninstallApplication (AccessibilityEvent event) {if (event. getSource ()! = Null) {if (event. getPackageName (). equals ("com. android. packageinstaller ") {List OK _nodes = event. getSource (). findAccessibilityNodeInfosByText ("OK"); if (OK _nodes! = Null &&! OK _nodes.isEmpty () {AccessibilityNodeInfo node; for (int I = 0; I
 
  


Complete DEMO: http://download.csdn.net/detail/jiazhijun/8251277

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.