可以使用Accessibility Service捕获 Messages/Notifications ,看看那个。
您可以扩展该类AccessibilityService
并覆盖该方法onAccessibilityEvent()
以实现如下内容:
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)
return; // event is not a notification
String sourcePackageName = (String) event.getPackageName();
Parcelable parcelable = event.getParcelableData();
if (parcelable instanceof Notification) {
// Statusbar Notification
}
else {
// something else, e.g. a Toast message
String log = "Message: " + event.getText().get(0)
+ " [Source: " + sourcePackageName + "]";
// write `log` to file...
}
}
注意:这对我在 Android 2.2 上不起作用,因为它似乎无法捕获 Toasts,但它在 Android 4.0+ 上有效。