看到其他应用程序的通知 [英] See notification of other app

查看:91
本文介绍了看到其他应用程序的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用什么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!"?

推荐答案

您最好的选择是使用<一个href="https://developer.android.com/reference/android/service/notification/NotificationListenerService.html"相对=nofollow> 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"));

这篇关于看到其他应用程序的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆