Advertisement
What_Ever

Untitled

Jan 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. package com.example.notify.arduino.androidnotificationlistener;
  2.  
  3. import android.app.Notification;
  4. import android.content.Intent;
  5. import android.content.IntentFilter;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.service.notification.NotificationListenerService;
  9. import android.service.notification.StatusBarNotification;
  10. import android.util.Log;
  11.  
  12. /**
  13.  * Created by mohammed on 1/23/18.
  14.  */
  15.  
  16. public class ArduinoListenerService extends NotificationListenerService {
  17.     private String TAG = this.getClass().getSimpleName();
  18.  
  19.     protected static final String NOTIFICATION_POSTED_ACTION =
  20.             "com.example.notify.arduino.androidnotificationlistener" +
  21.                     ".NOTIFICATION_LISTENER_POSTED_ACTION";
  22.  
  23.     protected static final String NOTIFICATION_REMOVED_ACTION =
  24.             "com.example.notify.arduino.androidnotificationlistener" +
  25.                     ".NOTIFICATION_LISTENER_REMOVED_ACTION";
  26.  
  27.     @Override
  28.     public void onCreate() {
  29.         super.onCreate();
  30.         IntentFilter filter = new IntentFilter();
  31.         filter.addAction(NOTIFICATION_POSTED_ACTION);
  32.         filter.addAction(NOTIFICATION_REMOVED_ACTION);
  33.     }
  34.  
  35.     @Override
  36.     public void onDestroy() {
  37.         super.onDestroy();
  38.     }
  39.  
  40.     private String getTickerText(Notification notification) {
  41.         final String TITLE_KEY = "android.title";
  42.         final String TEXT_KEY = "android.text";
  43.         String ticker = "";
  44.  
  45.         if (notification != null) {
  46.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  47.                 try {
  48.                     Bundle extras = notification.extras;
  49.                     String extraTitle = extras.getString(TITLE_KEY);
  50.                     String extraText = null;
  51.                     Object extraTextExtra = extras.get(TEXT_KEY);
  52.                     if (extraTextExtra != null) extraText = extraTextExtra.toString();
  53.  
  54.                     if (extraTitle != null && extraText != null && !extraText.isEmpty()) {
  55.                         ticker = extraTitle + ": " + extraText;
  56.                     } else if (extraTitle != null) {
  57.                         ticker = extraTitle;
  58.                     } else if (extraText != null) {
  59.                         ticker = extraText;
  60.                     }
  61.                 } catch (Exception e) {
  62.                     Log.w("NotificationPlugin", "problem parsing notification extras for " + notification.tickerText);
  63.                     e.printStackTrace();
  64.                 }
  65.             }
  66.  
  67.             if (ticker.isEmpty()) {
  68.                 ticker = (notification.tickerText != null) ? notification.tickerText.toString() : "";
  69.             }
  70.         }
  71.  
  72.         return ticker;
  73.     }
  74.  
  75.     @Override
  76.     public void onNotificationPosted(StatusBarNotification sbn) {
  77.  
  78.         Log.i(TAG,"**********onNotificationPosted**********");
  79.         Log.i(TAG,"ID :" + sbn.getId() + "\t" + getTickerText(sbn.getNotification())
  80.                 + "\t" + sbn.getPackageName());
  81.  
  82.         Intent intent = new  Intent(NOTIFICATION_POSTED_ACTION);
  83.         intent.putExtra("package_name", sbn.getPackageName());
  84.         intent.putExtra("notification_text", getTickerText(sbn.getNotification()));
  85.         //intent.putExtra("notification_icon", sbn.getNotification().getSmallIcon());
  86.         intent.putExtra("status_bar_notification", sbn);
  87.         sendBroadcast(intent);
  88.     }
  89.  
  90.     @Override
  91.     public void onNotificationRemoved(StatusBarNotification sbn) {
  92.         Log.i(TAG,"**********onNotificationRemoved**********");
  93.         Log.i(TAG,"ID :" + sbn.getId() + "\t" + getTickerText(sbn.getNotification())
  94.                 +"\t" + sbn.getPackageName());
  95.  
  96.         Intent intent = new  Intent(NOTIFICATION_REMOVED_ACTION);
  97.         intent.putExtra("package_name", sbn.getPackageName());
  98.         intent.putExtra("notification_text", getTickerText(sbn.getNotification()));
  99.         //intent.putExtra("notification_icon", sbn.getNotification().getSmallIcon());
  100.         intent.putExtra("status_bar_notification", sbn);
  101.         sendBroadcast(intent);
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement