How to get notified when a notification is notified [duplicate]

不羁的心 提交于 2019-11-27 12:32:06

问题


This question already has an answer here:

  • Detect a new Android notification 2 answers

I want to read/access/log the notifications fired on notification bar by other applications.

I searched Intents and PendingIntents a lot, but couldn't get a solution.

Does my application need to be notified when any notification is fired?

Or Does android system provide something to read notifications by the user-level applications?


回答1:


Starting with api 18 (android 4.3) it's possible: http://developer.android.com/reference/android/service/notification/NotificationListenerService.html

Very easy to use, but the user have to give permission to the app manually.




回答2:


Finally got the answer.!!! Using 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);
     }
}

And my Manifest is:

<?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>

Enjoy Coding.!!! :)




回答3:


There is not any API for access any notification. At least it would be hole in security. Only thing you can is catching some events, that causes notifications (like sms).




回答4:


A Notification is raised by sending an Intent.

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

The NotificationReceiver catches the Intent and pops up a Notification.

It is perfectly possible to catch the same Intent in one of your applications using a BroadCastReceiver and filter on the Intent you want.



来源:https://stackoverflow.com/questions/11429084/how-to-get-notified-when-a-notification-is-notified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!