Listening for UI Notification
Listening for UI Notification
elements. This application could be running in the background but not in use
by the user. The purpose of a notification is to notify the user about a process
that was initiated in the application either by the user or the system. This
article could help someone who’s trying hard to create a notification for
developmental purposes.
1. Using NotificationListenerService
Example Implementation
1. Create a Notification Listener Service
java
CopyEdit
import
android.service.notification.NotificationListenerServic
e;
import
android.service.notification.StatusBarNotification;
import android.util.Log;
@Override
public void
onNotificationPosted(StatusBarNotification sbn) {
Log.d(TAG, "Notification received: " +
sbn.getPackageName());
}
@Override
public void
onNotificationRemoved(StatusBarNotification sbn) {
Log.d(TAG, "Notification removed: " +
sbn.getPackageName());
}
}
android:permission="android.permission.BIND_NOTIFICATIO
N_LISTENER_SERVICE">
<intent-filter>
<action
android:name="android.service.notification.Notification
ListenerService" />
</intent-filter>
</service>
java
CopyEdit
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent
intent) {
if
(intent.getAction().equals("android.provider.Telephony.
SMS_RECEIVED")) {
Log.d("SmsReceiver", "SMS received!");
}
}
}
Declare in AndroidManifest.xml:
xml
CopyEdit
<receiver android:name=".SmsReceiver">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"
/>
</intent-filter>
</receiver>