Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android NotificationListenerService:如何知道用户是点击了通知(打开相关应用)还是只是删除了通知_Java_Android_Android Notifications - Fatal编程技术网

Java Android NotificationListenerService:如何知道用户是点击了通知(打开相关应用)还是只是删除了通知

Java Android NotificationListenerService:如何知道用户是点击了通知(打开相关应用)还是只是删除了通知,java,android,android-notifications,Java,Android,Android Notifications,我正在使用Android设备(Android 5.1.1)上的每个应用程序(不仅仅是我的)生成的通知。 通过扩展NotificationListenerService,我能够知道何时发布推送通知(覆盖“onNotificationPosted”方法)以及何时删除通知(覆盖“onNotificationRemoved”方法) 问题是,我想知道通知是如何删除的: a) 通过单击它(打开应用程序) 或 b) 通过键入它(因此它仅被删除) ? 有可能知道吗? 提前谢谢你 最好的方法是获取所有正在运行的进

我正在使用Android设备(Android 5.1.1)上的每个应用程序(不仅仅是我的)生成的通知。 通过扩展NotificationListenerService,我能够知道何时发布推送通知(覆盖“onNotificationPosted”方法)以及何时删除通知(覆盖“onNotificationRemoved”方法)

问题是,我想知道通知是如何删除的: a) 通过单击它(打开应用程序) 或 b) 通过键入它(因此它仅被删除) ?

有可能知道吗?
提前谢谢你

最好的方法是获取所有正在运行的进程的列表! 因此,在onNotificationRemoved方法中,我们可以: 1.使用库获取正在运行的进程的列表 2.将每个进程名称与packageName进行比较 3.如果比较返回真值,我们将检查进程是否在前台

public void onNotificationRemoved(StatusBarNotification sbn) {
    String packageName = sbn.getPackageName();
    try {
        List<AndroidAppProcess> processes = ProcessManager.getRunningAppProcesses();
        if (processes != null) {
            for (AndroidAppProcess process : processes) {
                String processName = process.name;
                if (processName.equals(packageName)) {
                    if (process.foreground ==true)
                    {
                        //user clicked on notification
                    }
                    else
                    {
                        //user swipe notification
                    }
                }
            }
        }
    }
    catch (Exception e)
    {
        String error = e.toString();
    }
}
已删除通知上的公共作废(状态通知sbn){
字符串packageName=sbn.getPackageName();
试一试{
列表进程=ProcessManager.getRunningAppProcesses();
if(进程!=null){
for(AndroidAppProcess流程:流程){
字符串processName=process.name;
if(processName.equals(packageName)){
if(process.foreground==true)
{
//用户单击通知
}
其他的
{
//用户刷卡通知
}
}
}
}
}
捕获(例外e)
{
字符串错误=e.toString();
}
}