Android アプリケーションのアクセシビリティ サービスを使用して、ネットワーク オペレータからのポップアップを読み取ります。私のアプリケーションだけでアクセシビリティサービスがオンになっていれば、問題なく動作します。他のAndroidアプリでオンにすると、onAccessibilityEvent()
まったく機能しません。
マニフェスト コード:
<service
android:name=".services.NotificationAccessibilityService"
android:exported="false"
android:label="@string/app_name"
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/accessibilityservice" />
</service>
アクセシビリティ クラス:
public class NotificationAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getClassName() != null && event.getClassName().toString().equalsIgnoreCase(
"android.app.AlertDialog") && event.getPackageName() != null
&& event.getPackageName().toString().equalsIgnoreCase("com.android.phone"))
{
Logger.d(TAG,"source info"+ " " + event.getClassName().toString());
Logger.d(TAG, String.format("onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s",
getEventType(event), event.getClassName(), event.getPackageName(),
event.getEventTime(), getEventText(event)));
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
return super.bindService(service, conn, flags);
}
@Override
public void onInterrupt() {
}
@Override
public void onServiceConnected()
{
Intent iLaunch = new Intent(NotificationAccessibilityService.this, StatusAct.class);
iLaunch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(iLaunch);
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
setServiceInfo(info);
}
private String getEventType(AccessibilityEvent event) {
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
return "TYPE_NOTIFICATION_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_CLICKED:
return "TYPE_VIEW_CLICKED";
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
return "TYPE_VIEW_FOCUSED";
case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
return "TYPE_VIEW_LONG_CLICKED";
case AccessibilityEvent.TYPE_VIEW_SELECTED:
return "TYPE_VIEW_SELECTED";
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
return "TYPE_WINDOW_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
return "TYPE_VIEW_TEXT_CHANGED";
}
return "default";
}
private String getEventText(AccessibilityEvent event) {
StringBuilder sb = new StringBuilder();
for (CharSequence s : event.getText()) {
sb.append(s);
}
return sb.toString();
}
}