如何从另一个应用程序的通知中获取图像? [英] How to get an image from another app's notification?

查看:53
本文介绍了如何从另一个应用程序的通知中获取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个通知管理应用程序,我想获取其他应用程序显示的通知内容.目前我使用这样的代码:

I'm creating a notification management app and I want to get the contents of notifications which other apps show. currently I use codes like this :

statusBarNotification.getNotification().extras.getString(Notification.EXTRA_TITLE);

还有这个:

statusBarNotification.getNotification().extras.getString(Notification.EXTRA_TEXT);

阅读通知的标题和文本.但是几个小时后,我找不到获取通知文本附带的图像的方法.例如,Whatsapp 通知中显示的个人资料图片.我知道不是小图标或大图标,我检查了几次.

to read the title and text of notifications. but after a few hours I couldn't find a way to get the image which comes along with the notification's text. for example a profile picture which is showed in Whatsapp's notification. I know it's not the Small or Large icons, I checked a few times.

如果有人能以任何方式提供帮助,我们将不胜感激

So if anyone could help in any way, it would be much appreciated

推荐答案

我假设你使用 NotificationListenerService 监听来自其他应用的通知.

I assume you use NotificationListenerService to listen to notification from other app.

在您的NotificationService 类中,提取额外Notification.EXTRA_SMALL_ICON 中的图标资源id 并访问其他应用程序包资源以获取Drawable.

In you NotificationService class, extract icon ressource id in extra Notification.EXTRA_SMALL_ICON and access the other app package ressources to get the Drawable.

Notification.EXTRA_PICTURE 包含通知中发送的大图:

Notification.EXTRA_PICTURE contains the large image sent in the notification :

public class NotificationService extends NotificationListenerService {

    Context context;

    @Override

    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

    }

    @Override
    public void onNotificationPosted(StatusBarNotification statusBarNotification) {

        // a notification is posted

        String pack = statusBarNotification.getPackageName();

        Bundle extras = statusBarNotification.getNotification().extras;

        int iconId = extras.getInt(Notification.EXTRA_SMALL_ICON);

        try {
            PackageManager manager = getPackageManager();
            Resources resources = manager.getResourcesForApplication(pack);

            Drawable icon = resources.getDrawable(iconId);

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (extras.containsKey(Notification.EXTRA_PICTURE)) {
            // this bitmap contain the picture attachment
            Bitmap bmp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);
        }

    }

    @Override

    public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
        //call when notification is removed
    }
}

这篇关于如何从另一个应用程序的通知中获取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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