Accessibility服務(wù)可以為所有的應(yīng)用程谦趣,一組應(yīng)用程序或單個應(yīng)用程序提供這些增強(qiáng)功能。
-
新建一個類繼承 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>標(biāo)簽一罩,在標(biāo)簽里指出配置文件的位置<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() - 可選廊驼。系統(tǒng)會在成功連接上你的服務(wù)的時候調(diào)用這個方法扰付,在這個方法里你可以做一下初始化工作此叠,例如設(shè)備的聲音震動管理纯续,也可以調(diào)用setServiceInfo()進(jìn)行配置工作。
onAccessibilityEvent() - 必須灭袁。通過這個函數(shù)可以接收系統(tǒng)發(fā)送來的AccessibilityEvent猬错,接收來的AccessibilityEvent是經(jīng)過過濾的,過濾是在配置工作時設(shè)置的茸歧。
onInterrupt() - 必須倦炒。這個在系統(tǒng)想要中斷AccessibilityService返給的響應(yīng)時會調(diào)用。在整個生命周期里會被調(diào)用多次软瞎。
onUnbind() - 可選逢唤。在系統(tǒng)將要關(guān)閉這個AccessibilityService會被調(diào)用。在這個方法中進(jìn)行一些釋放資源的工作
-
一般在onServiceConnected()方法里進(jìn)行
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權(quán)限涤浇,將進(jìn)行智能安裝", 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);