Accessibility服务可以为所有的应用程,一组应用程序或单个应用程序提供这些增强功能。
-
新建一个类继承 AccessibilityService
public class InstallAccessibilityService extends AccessibilityService
-
AndroidManifest文件里注册
<service android:name=".InstallAccessibilityService" android:label="智能安装" 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.xml
AndroidManifest里添加<meta-data>标签,在标签里指出配置文件的位置<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:packageNames="com.android.packageinstaller" android:description="@string/accessibility_service_description" android:accessibilityEventTypes="typeAllMask" android:accessibilityFlags="flagDefault" android:accessibilityFeedbackType="feedbackGeneric" android:canRetrieveWindowContent="true" />
AccessibilityService 方法重载
onServiceConnected() - 可选。系统会在成功连接上你的服务的时候调用这个方法,在这个方法里你可以做一下初始化工作,例如设备的声音震动管理,也可以调用setServiceInfo()进行配置工作。
onAccessibilityEvent() - 必须。通过这个函数可以接收系统发送来的AccessibilityEvent,接收来的AccessibilityEvent是经过过滤的,过滤是在配置工作时设置的。
onInterrupt() - 必须。这个在系统想要中断AccessibilityService返给的响应时会调用。在整个生命周期里会被调用多次。
onUnbind() - 可选。在系统将要关闭这个AccessibilityService会被调用。在这个方法中进行一些释放资源的工作
-
一般在onServiceConnected()方法里进行
private void setServiceInfo(int feedbackType) { AccessibilityServiceInfo info = new AccessibilityServiceInfo(); info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK; info.feedbackType = feedbackType; info.notificationTimeout = EVENT_NOTIFICATION_TIMEOUT_MILLIS; info.packageNames = PACKAGE_NAMES; setServiceInfo(info); }
-
智能安装apk
private void installApk(String path) { if (!path.endsWith(".apk")) { Toast.makeText(this, "文件不可安装", Toast.LENGTH_SHORT).show(); } else { if (!isRoot()) { Toast.makeText(this, "没有ROOT权限,将进行智能安装", Toast.LENGTH_SHORT).show(); smartInstall(path); } else { silentInstall(path); } }} private void silentInstall(final String path) { new Thread(new Runnable() { @Override public void run() { SilentInstall installHelper = new SilentInstall(); final boolean result = installHelper.install(path); runOnUiThread(new Runnable() { @Override public void run() { if (result) { installBtn.setText("安装成功"); } else { installBtn.setText("安装失败"); } installBtn.setText("秒装"); } }); } }).start(); } private void smartInstall(String path) { Uri uri = Uri.fromFile(new File(path)); Intent localIntent = new Intent(Intent.ACTION_VIEW); localIntent.setDataAndType(uri, "application/vnd.android.package-archive"); try { startActivity(localIntent); } catch (Exception e) { e.printStackTrace(); }} public class SilentInstall { public boolean install(String path) { boolean result = false; DataOutputStream dataOutputStream = null; BufferedReader bufferedReader = null; try { Process process = Runtime.getRuntime().exec("su"); dataOutputStream = new DataOutputStream(process.getOutputStream()); String commmad = "pm instll -r " + path + "\n"; dataOutputStream.write(commmad.getBytes(Charset.forName("utf-8"))); dataOutputStream.flush(); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); process.waitFor(); bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); String msg = ""; String line = ""; while ((line = bufferedReader.readLine()) != null) { msg += line; } if (!msg.contains("Failure")) { result = true; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (dataOutputStream != null) { dataOutputStream.close(); } if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } } return result;
}}
7 开启辅助功能Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent);