Android Read Status Bar Notification | Android Read all incoming notification | Android Read Incoming Sms | Android Read Incoming Call

Hello Friends,
Today, I am going to share a small tutorial on android “NotificationListenerService
which help us to read the all app notifications. This tutorial covers following
points.

 


Feature:
1. We can easily read the notification of other android application or read all status bar
notification,
2. Read|Listen incoming whatsapp messages
3. Read all incoming SMS
4. Read all incoming calls
5. Battery Low

Step1 : Create a Notification Service Class :
This class extends NotificationListenerService class which contains
two method:
a) onNotificationPosted(StatusBarNotification sbn) :
Implement this method to learn about new notifications as they are
posted by apps.
 b) onNotificationRemoved(StatusBarNotification sbn) :
Implement this method to learn when notifications are removed.

Note: The StatusBarNotifcation object contains extras, app package name and
ticker name

1. NotificationService.java

  1. package android.notifications;
  2. import android.app.Notification;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.os.Bundle;
  7. import android.service.notification.NotificationListenerService;
  8. import android.service.notification.StatusBarNotification;
  9. import android.util.Log;
  10. import android.support.v4.content.LocalBroadcastManager;
  11. import java.io.ByteArrayOutputStream;
  12. /**
  13.  * Created by mukesh on 19/5/15.
  14.  */
  15. public class NotificationService extends NotificationListenerService {
  16.     Context context;
  17.     @Override
  18.     public void onCreate() {
  19.         super.onCreate();
  20.         context = getApplicationContext();
  21.     }
  22.     @Override
  23.     public void onNotificationPosted(StatusBarNotification sbn) {
  24.         String pack = sbn.getPackageName();
  25.         String ticker =“”;
  26.         if(sbn.getNotification().tickerText !=null) {
  27.             ticker = sbn.getNotification().tickerText.toString();
  28.         }
  29.         Bundle extras = sbn.getNotification().extras;
  30.         String title = extras.getString(“android.title”);
  31.         String text = extras.getCharSequence(“android.text”).toString();
  32.         int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
  33.         Bitmap id = sbn.getNotification().largeIcon;
  34.         Log.i(“Package”,pack);
  35.         Log.i(“Ticker”,ticker);
  36.         Log.i(“Title”,title);
  37.         Log.i(“Text”,text);
  38.         Intent msgrcv = new Intent(“Msg”);
  39.         msgrcv.putExtra(“package”, pack);
  40.         msgrcv.putExtra(“ticker”, ticker);
  41.         msgrcv.putExtra(“title”, title);
  42.         msgrcv.putExtra(“text”, text);
  43.         if(id != null) {
  44.             ByteArrayOutputStream stream = new ByteArrayOutputStream();
  45.             id.compress(Bitmap.CompressFormat.PNG, 100, stream);
  46.             byte[] byteArray = stream.toByteArray();
  47.             msgrcv.putExtra(“icon”,byteArray);
  48.         }
  49.         LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
  50.     }
  51.     @Override
  52.     public void onNotificationRemoved(StatusBarNotification sbn) {
  53.         Log.i(“Msg”,“Notification Removed”);
  54.     }
  55. }

2.MainActivity.java

  1. package android.notifications;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.os.Bundle;
  10. import android.support.v4.content.LocalBroadcastManager;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13. import android.widget.ListView;
  14. import java.util.ArrayList;
  15. /**
  16.  * Created by mukesh on 19/5/15.
  17.  */
  18. public class MainActivity extends Activity {
  19.     ListView list;
  20.     CustomListAdapter adapter;
  21.     ArrayList<model> modelList;
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         modelList = new ArrayList<model>();
  27.         adapter = new CustomListAdapter(getApplicationContext(), modelList);
  28.         list=(ListView)findViewById(R.id.list);
  29.         list.setAdapter(adapter);
  30.         LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter(“Msg”));
  31.     }
  32.     @Override
  33.     public boolean onCreateOptionsMenu(Menu menu) {
  34.         getMenuInflater().inflate(R.menu.menu_main, menu);//Menu Resource, Menu
  35.         return true;
  36.     }
  37.     @Override
  38.     public boolean onOptionsItemSelected(MenuItem item) {
  39.         switch (item.getItemId()) {
  40.             case R.id.action_settings:
  41.                 Intent intent = new Intent(
  42.                         “android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS”);
  43.                 startActivity(intent);
  44.                 return true;
  45.             default:
  46.                 return super.onOptionsItemSelected(item);
  47.         }
  48.     }
  49.     private BroadcastReceiver onNotice= new BroadcastReceiver() {
  50.         @Override
  51.         public void onReceive(Context context, Intent intent) {
  52.            // String pack = intent.getStringExtra(“package”);
  53.             String title = intent.getStringExtra(“title”);
  54.             String text = intent.getStringExtra(“text”);
  55.             //int id = intent.getIntExtra(“icon”,0);
  56.             Context remotePackageContext = null;
  57.             try {
  58. //                remotePackageContext = getApplicationContext().createPackageContext(pack, 0);
  59. //                Drawable icon = remotePackageContext.getResources().getDrawable(id);
  60. //                if(icon !=null) {
  61. //                    ((ImageView) findViewById(R.id.imageView)).setBackground(icon);
  62. //                }
  63.                 byte[] byteArray =intent.getByteArrayExtra(“icon”);
  64.                 Bitmap bmp = null;
  65.                 if(byteArray !=null) {
  66.                     bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
  67.                 }
  68.                 Model model = new Model();
  69.                 model.setName(title +” “ +text);
  70.                 model.setImage(bmp);
  71.                 if(modelList !=null) {
  72.                     modelList.add(model);
  73.                     adapter.notifyDataSetChanged();
  74.                 }else {
  75.                     modelList = new ArrayList<model>();
  76.                     modelList.add(model);
  77.                     adapter = new CustomListAdapter(getApplicationContext(), modelList);
  78.                     list=(ListView)findViewById(R.id.list);
  79.                     list.setAdapter(adapter);
  80.                 }
  81.             } catch (Exception e) {
  82.                 e.printStackTrace();
  83.             }
  84.         }
  85.     };
  86. }
  87. </model></model></model>

3. AndroidManifest.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <manifest xmlns:android=http://schemas.android.com/apk/res/android&#8221;
  3.     package=“android.notifications”>
  4.     <uses-permission android:name=“android.permission.RECEIVE_SMS” />
  5.     <uses-permission android:name=“android.permission.READ_SMS” />
  6.     <uses-permission android:name=“android.permission.READ_PHONE_STATE”/>
  7.     <application
  8.         android:allowBackup=“true”
  9.         android:icon=“@drawable/ic_launcher”
  10.         android:label=“@string/app_name”
  11.         android:theme=“@style/AppTheme”>
  12.         <activity
  13.             android:name=“android.notifications.MainActivity”
  14.             android:configChanges=“keyboardHidden|orientation|screenSize”
  15.             android:label=“@string/app_name”>
  16.             <intent-filter>
  17.                 <action android:name=“android.intent.action.MAIN” />
  18.                 <category android:name=“android.intent.category.LAUNCHER” />
  19.             </intent-filter>
  20.         </activity>
  21.         <service
  22.             android:name=“android.notifications.NotificationService”
  23.             android:label=“@string/app_name”
  24.             android:permission=“android.permission.BIND_NOTIFICATION_LISTENER_SER                          VICE”>
  25.             <intent-filter>
  26.                 <action android:name=“android.service.notification.NotificationLi                                  stenerService” />
  27.             </intent-filter>
  28.         </service>
  29.         <receiver android:name=“android.notifications.IncomingSms”>
  30.            <intent-filter>
  31.                <action android:name=“android.provider.Telephony.SMS_RECEIVED” />
  32.            </intent-filter>
  33.         </receiver>
  34.         <receiver android:name=“.ServiceReceiver” >
  35.             <intent-filter>
  36.                 <action android:name=“android.intent.action.PHONE_STATE” />
  37.             </intent-filter>
  38.         </receiver>
  39.     </application>
  40. </manifest>

4. Android check Notification Access Setting is enable or not:

  1. //check notification access setting is enabled or not
  2. public static boolean checkNotificationEnabled() {
  3.          try{
  4.        if(Settings.Secure.getString(MainActivity.mActivity.getContentResolver(),
  5.                  “enabled_notification_listeners”).contains(App.getContext().getPackageName()))
  6.       {
  7.     return true;
  8.       } else {
  9.          return false;
  10.       }
  11.  }catch(Exception e) {
  12.   e.printStackTrace();
  13.  }
  14.  return false;
  15. }

Download Complete Code From Here NotificationListener

Hope, this will helps someone.
Enjoy Coding …. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s