【问题标题】:How to get notified when a notification is notified [duplicate]通知通知时如何获得通知[重复]
【发布时间】:2012-07-10 20:38:56
【问题描述】:

我想读取/访问/记录其他应用程序在通知栏上触发的通知。

我搜索了很多IntentsPendingIntents,但找不到解决方案。

当任何通知被触发时是否需要通知我的应用程序?

或者android系统是否提供了一些东西来读取用户级应用程序的通知?

【问题讨论】:

    标签: android android-intent notifications android-pendingintent accessibilityservice


    【解决方案1】:

    从 api 18 (android 4.3) 开始,可以: http://developer.android.com/reference/android/service/notification/NotificationListenerService.html

    非常易于使用,但用户必须手动授予应用权限。

    【讨论】:

      【解决方案2】:

      终于有答案了!!!使用AccessibilityService

      public class NotificationService extends AccessibilityService {
      
      @Override
      public void onAccessibilityEvent(AccessibilityEvent event) {
          // TODO Auto-generated method stub.
      //Code when the event is caught 
         }
      @Override
      public void onInterrupt() {
          // TODO Auto-generated method stub.
      
      }
      
      @Override
      protected void onServiceConnected() {
          AccessibilityServiceInfo info = new AccessibilityServiceInfo();
            info.feedbackType = 1;
                info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
            info.notificationTimeout = 100; 
            setServiceInfo(info);
           }
      }
      

      我的清单是:

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.test.notify"
      android:versionCode="1"
      android:versionName="1.0" >
      <uses-sdk android:minSdkVersion="8" />
      <application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" >
           <service android:name=".NotificationService" android:enabled="true">
                  <intent-filter >
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
                 <action android:name="android.accessibilityservice.AccessibilityService"/>
              </intent-filter>
           </service>
         </application>
       </manifest>
      

      享受编码。!!! :)

      【讨论】:

      • 该代码仅用于捕获与通知相关的事件。在更改info.eventTypes = 时,我们可以捕捉到不同类型的事件。
      • 我正在尝试登录服务连接方法,但失败了,是否需要任何权限
      • @Calvin :您需要从设置(设置 -> 辅助功能)手动打开辅助功能服务
      【解决方案3】:

      没有任何 API 可以访问任何通知。至少这将是安全漏洞。你唯一能做的就是捕捉一些导致通知(如短信)的事件。

      【讨论】:

        【解决方案4】:

        通过发送Intent 来引发Notification

        Intent intent = new Intent(this, NotificationReceiver.class);
        

        NotificationReceiver 捕捉到Intent 并弹出一个Notification

        完全有可能使用BroadCastReceiver 在您的一个应用程序中捕获相同的Intent 并过滤您想要的Intent

        【讨论】:

        • 没有“通知栏状态更改”或“通知已触发”的意图。
        • 确实如此,但通知是使用 Intent 触发的。并且可以使用 BroadCastReceiver 捕获 Intent。当然,这一切都取决于你想访问什么样的通知。
        • 其实我想记录所有的通知。所以我正在寻找一种通用方法来捕获所有通知。
        • @MartijnVanMierloo :这不是真的。通知被添加到 notificationManager,它使用系统服务,而不是广播。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        • 1970-01-01
        • 2018-03-25
        • 2018-11-14
        相关资源
        最近更新 更多