Last active
February 8, 2022 12:34
-
-
Save daichan4649/770e250adaaf685e07b3 to your computer and use it in GitHub Desktop.
各種通知(notification, toast) をアプリ側で検知する (for Android)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="daichan4649.notification" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="15" | |
android:targetSdkVersion="20" /> | |
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<service | |
android:name="NotificationWatcher" | |
android:label="Notification Watcher" | |
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/notification4649" /> | |
</service> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
findViewById(R.id.show_notification).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
showNotification(); | |
} | |
}); | |
findViewById(R.id.show_toast).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
showToast(); | |
} | |
}); | |
} | |
private void showNotification() { | |
System.out.println("show notification"); | |
NotificationCompat.Builder builder = new Builder(getApplicationContext()); | |
builder.setSmallIcon(R.drawable.ic_launcher); | |
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), resourceId)); | |
builder.setContentTitle("title"); | |
builder.setContentText("text"); | |
builder.setTicker("ticker"); | |
long[] vibratePattern = new long[] { 500, 500, 500 }; | |
builder.setVibrate(vibratePattern); | |
Notification notification = builder.build(); | |
int notificationId = 100; | |
NotificationManagerCompat nm = NotificationManagerCompat.from(getApplicationContext()); | |
nm.notify(notificationId, notification); | |
} | |
private void showToast() { | |
System.out.println("show toast"); | |
String text = "toast"; | |
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" | |
android:accessibilityEventTypes="typeAllMask" | |
android:accessibilityFeedbackType="feedbackAllMask" | |
android:accessibilityFlags="flagDefault" | |
android:canRetrieveWindowContent="true" | |
android:description="@string/app_name" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NotificationWatcher extends AccessibilityService { | |
@Override | |
public void onAccessibilityEvent(AccessibilityEvent event) { | |
switch (event.getEventType()) { | |
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: | |
System.out.println("[onAccessibilityEvent]TYPE_NOTIFICATION_STATE_CHANGED: " + event.getText()); | |
captureNotification(event.getParcelableData()); | |
break; | |
} | |
} | |
@Override | |
public void onInterrupt() { | |
System.out.println("[onInterrupt]"); | |
} | |
private static void captureNotification(Parcelable parcelable) { | |
if (parcelable instanceof Notification) { | |
System.out.println(String.format("[captureNotification]%s", ((Notification) parcelable).tickerText)); | |
} else if (parcelable instanceof Toast) { | |
System.out.println("[captureNotification]toast"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment