一、AndroidManifest.xml再application裡加入以下內容:
<service
android:name="AccessibilityService檔案名稱,例.UN"
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/accessible_service_config" />
</service>
二、xml檔與AccessibilityService檔案內容,例.UN:
<?xml version="1.0" encoding="utf-8"?><accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:description="@string/accessibility_service_description" android:packageNames="com.example.hmlong.accessibilityservices" android:accessibilityEventTypes="typeViewClicked|typeViewFocused" android:accessibilityFeedbackType="feedbackSpoken" android:settingsActivity="com.example.android.apis.accessibility.TestBackActivity" android:notificationTimeout="100" android:canRetrieveWindowContent="true"/>
package com.example.fan.bg; import android.accessibilityservice.AccessibilityService; import android.accessibilityservice.AccessibilityServiceInfo; import android.view.accessibility.AccessibilityEvent; import android.widget.Toast; public class UN extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) { final int eventType=accessibilityEvent.getEventType(); String eventText = null; switch(eventType){ case AccessibilityEvent.TYPE_VIEW_CLICKED: //代表喀噠上的事件View一樣 Button,CompoundButton等等。 eventText="顯示:"; break; case AccessibilityEvent.TYPE_VIEW_FOCUSED: //表示設置a的輸入焦點事件View。 eventText="顯示2:"; break; } eventText=eventText + accessibilityEvent.getContentDescription(); Toast.makeText(getApplicationContext(),eventText,Toast.LENGTH_LONG).show(); } @Override public void onInterrupt() { //(required) 這個在系統想要中斷AccessibilityService返給的響應時會調用。在整個生命週期裡會被調用多次。 } @Override protected void onServiceConnected() { AccessibilityServiceInfo info =new AccessibilityServiceInfo(); info.eventTypes=AccessibilityEvent.TYPE_VIEW_CLICKED|AccessibilityEvent.TYPE_VIEW_FOCUSED; info.feedbackType=AccessibilityServiceInfo.FEEDBACK_SPOKEN; info.notificationTimeout=30; this.setServiceInfo(info); } //調用前台名稱 public void isForeground() { } }
三、layout內容
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.324" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="@string/right"←按了會顯示在Toast上 android:text="@string/right" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="232dp" tools:layout_editor_absoluteY="145dp" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="@string/left"←按了會顯示在Toast上 android:text="@string/left" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="88dp" tools:layout_editor_absoluteY="145dp" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
三、判斷輔助服務是否開啟的語法
String TAG="輔助功能"; @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openAccessibility(this);//判斷是否開啟 }
private boolean isAccessibilitySettingsOn(Context mContext) { int accessibilityEnabled = 0; // TestService为对应的服务 final String service = getPackageName() + "/" +[ AccessibilityService檔案內容,例.UN].class.getCanonicalName();;Log.i(TAG, "service:" + service); try { accessibilityEnabled = Settings.Secure.getInt(mContext.getApplicationContext().getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED); Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled); } catch (Settings.SettingNotFoundException e) { Log.e(TAG, "查找設置時出錯,默認可訪問性未找到: " + e.getMessage()); } TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':'); if (accessibilityEnabled == 1) { Log.v(TAG, "***可用性已啟用*** -----------------"); String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); if (settingValue != null) { mStringColonSplitter.setString(settingValue); while (mStringColonSplitter.hasNext()) { String accessibilityService = mStringColonSplitter.next(); Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service); if (accessibilityService.equalsIgnoreCase(service)) { Log.v(TAG, "我們找到了正確的設置 - 輔助功能已打開!"); return true; } } } } else { Log.v(TAG, "***可訪問性被禁用***"); } return false; } private void openAccessibility(Context context){ if (!isAccessibilitySettingsOn(context)) { Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent); } }
資料來源:https://developer.android.com/reference/android/accessibilityservice/AccessibilityService
https://developer.android.com/reference/android/view/accessibility/AccessibilityEventhttps://developer.android.com/training/accessibility/service
沒有留言:
張貼留言
影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。