看到其他应用程序的通知应用程序、通知

2023-09-12 22:53:44 作者:北执

我应该使用什么API我的应用程序,看看我从Facebook收到了通知,例如,所以在一个文本框写脸谱!?

What API I should use for my app to see if I've received a notification from example from facebook, and so write in one textbox "Facebook!"?

推荐答案

您最好的选择是使用 NotificationListenerService ,其中加入的API级别18

Your best bet is to use NotificationListenerService, which was added in API level 18.

下面是一个例子:

public class FacebookNotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        final String packageName = sbn.getPackageName();
        if (!TextUtils.isEmpty(packageName) && packageName.equals("com.facebook.katana")) {
            // Do something
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // Nothing to do
    }

}

在AndroidManifest

<service
    android:name="your.path.to.FacebookNotificationListener"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

此外,用户将需要启用您的应用程序监听到被张贴在通知:

Also, your users will need to enable your app to listen for notifications to be posted under:

设置 - >安全 - >通知接入

不过你可以通过引导用户直有以下意图

But you can direct your users straight there by using the following Intent:

startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));