a��a?�a?�a?�i?sNotificationListenerServicea�?kitkat

a��a?�4.3c�???� i??APIA�18i?�a?�a�?a?�NotificationListenerServicea��??�a��a?????a�??�?a?�a?�e�?e�?e?�e?�a???Z?a??a��

a��a?�4.4i??APIA�19i?�?��aS�a?�a?�a?�c�??��i??cZ�a????�a��a??a�?e�se?�notificationeZ�a?�?�?a�scs�a?????i??a�?a�?e?�e�?a??a��eZ�a?�a?�a?�?�????i?�a��

??�a��4.3a?�??�a��e?�e�??��a��NotificationListenerServicea�zcZ�a��a?�a???�???�i?sonNotificationPosted()A�a�?A�onNotificationRemoved()a��

public class SimpleKitkatNotificationListener extends NotificationListenerService {
        @Override
        public void onNotificationPosted(StatusBarNotification sbn) {
              //..............
        }

        @Override
        public void onNotificationRemoved(StatusBarNotification sbn) {
              //.............. 
        }
}

c��a?Z??�a��e?�e�?a??manifest?��a��a?�a?�??Ze?�a?????aS????e�?i?sBIND_NOTIFICATION_LISTENER_SERVICEi??a??intent-filtera?�a�sa?�SERVICE_INTERFACEA�A�actiona��

<service
      android:name="it.gmariotti.android.examples.
notificationlistener.SimpleKitkatNotificationListener"
      android:label="@string/service_name"
      android:debuggable="true"    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
      <intent-filter>
           <action android:name="android.service.
                 notification.NotificationListenerService" ></action>
      </intent-filter>
 </service>

??�a?Ze?�e�?a??c�????aS?a��?????�e?�a?�?�????aS????e��a?????cs�i??

e�?a?�a��a?�i?sA�”Settings”A�->A�”Security”A�->A�”NotificationA�access”a��

e?�e�?a??c�???�a�sa?�Intenti?s

Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
    startActivity(intent);

a��a?�4.4a?�a�?a?�?��cs�?��cs�Notification.extrasA�a��?��i??a��a?�a?�a?�a?�a?�a??a�??��??�i??metadatai?�e��a??i??Bundlei?�a��

c�?e��???a??c��a?�i?s

Notification mNotification=sbn.getNotification();
   Bundle extras = mNotification.extras;

e?�a??e��a??i??Bundlei?�a??a�?a?�a?�a??a�sa?????a��a?�a??a�?a??NotificationA�a?�?�?a?�a��a�?e��a��a��a?�a��i?s

/**
     * {@link #extras} key: this is the title of the notification,
     * as supplied to {@link Builder#setContentTitle(CharSequence)}.
     */
    public static final String EXTRA_TITLE = "android.title";

    /**
     * {@link #extras} key: this is the main text payload, as supplied to
     * {@link Builder#setContentText(CharSequence)}.
     */
    public static final String EXTRA_TEXT = "android.text";

    /**
     * {@link #extras} key: this is a third line of text, as supplied to
     * {@link Builder#setSubText(CharSequence)}.
     */
    public static final String EXTRA_SUB_TEXT = "android.subText";

    /**
     * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
     * notification payload, as
     * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
     */
    public static final String EXTRA_LARGE_ICON = "android.largeIcon";

a�ze??a?�a?�cZ?e?Si??Hangoutsi?�?�????i?s

Bundle[
     {android.title=Gabriele Mariotti, 
      android.subText=null, 
      android.showChronometer=false, 
      android.icon=2130838949, 
      android.text=Send a message with Hangouts,
      android.progress=0, 
      android.progressMax=0, 
      android.showWhen=true,     android.largeIcon=android.graphics.Bitmap@42075010, 
      android.infoText=null, 
      android.progressIndeterminate=false, 
      android.scoreModified=false}]

a???�?a??a??a�?a?�a?�a��a?�?��??�a��

String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
     int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON);
     Bitmap notificationLargeIcon = 
                  ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON));
     CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT);
     CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT);
Also you can display them... and yes, you can get also the BigPicture (if exists).

a?�a?�a?�e??a??a�?a��a��a��???c�?a�????a��a��???cs�i??e??a??a�?a?�a?�a��a�?i??a��?z???�cs�e??i?�a��

buying clomid online safe, zithromax online.

??�a�ze??a?�c��a�?e��a��i?s

Bundle[
     {android.title=Gabriele Mariotti,
      android.subText=email@xxxx.com, 
      android.showChronometer=false, 
      android.icon=2130837697, 
      android.text=A simple subject \n A simple text, 
      android.progress=0,
      android.progressMax=0,
      android.showWhen=true,
android.largeIcon=android.graphics.Bitmap@4208ba28,
      android.infoText=null,
      android.progressIndeterminate=false,
      android.scoreModified=false}
]

e�se?�e?�c�??�???�i??a?�a??a�?a?�a?�a??a�sa??4.3a?�e?�e�?a??a��???a?�a?�cs�a?????a��

a?�a??e??a?Ye?�e�??�??��e�?a?�c�?a?�NotificationA�Listenercs�appa��

a�?c�?a��a?�i?s

public class SimpleKitkatNotificationListener extends NotificationListenerService {
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Notification mNotification = sbn.getNotification();
        if (mNotification != null) {
            Bundle extras = mNotification.extras;
            Intent intent = new Intent(MainActivity.INTENT_ACTION_NOTIFICATION);
            intent.putExtras(mNotification.extras);
            sendBroadcast(intent);
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {}
}

public class MainActivity extends Activity {
    protected MyReceiver mReceiver = new MyReceiver();
    public static String INTENT_ACTION_NOTIFICATION = "it.gmariotti.notification";

    protected TextView title;
    protected TextView text;
    protected TextView subtext;
    protected ImageView largeIcon;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Retrieve ui elements
        title = (TextView) findViewById(R.id.nt_title);
        text = (TextView) findViewById(R.id.nt_text);
        subtext = (TextView) findViewById(R.id.nt_subtext);
        largeIcon = (ImageView) findViewById(R.id.nt_largeicon);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_autorize:
            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mReceiver == null)
            mReceiver = new MyReceiver();

        registerReceiver(mReceiver, new IntentFilter(INTENT_ACTION_NOTIFICATION));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                Bundle extras = intent.getExtras();
                String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
                Bitmap notificationLargeIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON));
                CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT);
                CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT);
                title.setText(notificationTitle);
                text.setText(notificationText);
                subtext.setText(notificationSubText);
                if (notificationLargeIcon != null) {
                    largeIcon.setImageBitmap(notificationLargeIcon);
                }
            }
        }
    }
}

a?Ya??a�?a??GitHuba?�c?�a?�e?�a?�a�?c�?a��

关于作者: 高俊阳

(新浪微博:@狐狸叔

查看高俊阳的更多文章 >>



可能感兴趣的文章

发表评论

Comment form

(*) 表示必填项

1 条评论

  1. leehoja 说道:

    感谢分享,用的上!!

    Thumb up 0 Thumb down 0

跳到底部
返回顶部