关于android4.3中的NotificationListenerService的学习
2014-01-23 00:52
344 查看
是这么定义的:
NotificationListenerService :A service that receives calls from the system when new notifications are posted or removed.
它是一个抽象类:
如果我们要继承NotificationListenerService需要实现其两个方法:onNotificationPosted() 和 onNotificationRemoved()。
另外:
我们需要在manifest文件中声明这个服务权限:BIND_NOTIFICATION_LISTENER_SERVICE,在intent-filter中定义SERVICE_INTERFACE action。
最后,要启动服务,路径是这样的: ”Settings” -> ”Security” -> ”Notification access”。
Example:
MainActivity.java
NotificationListenerService :A service that receives calls from the system when new notifications are posted or removed.
它是一个抽象类:
如果我们要继承NotificationListenerService需要实现其两个方法:onNotificationPosted() 和 onNotificationRemoved()。
另外:
我们需要在manifest文件中声明这个服务权限:BIND_NOTIFICATION_LISTENER_SERVICE,在intent-filter中定义SERVICE_INTERFACE action。
最后,要启动服务,路径是这样的: ”Settings” -> ”Security” -> ”Notification access”。
Example:
SimpleKitkatNotificationListener.java import android.app.Notification;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.service.notification.NotificationListenerService;import android.service.notification.StatusBarNotification; public class SimpleKitkatNotificationListener extends NotificationListenerService { @Override public void onCreate() { super.onCreate(); //android.os.Debug.waitForDebugger(); } @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); Notification.Action[] mActions=mNotification.actions; if (mActions!=null){ for (Notification.Action mAction:mActions){ int icon=mAction.icon; CharSequence actionTitle=mAction.title; PendingIntent pendingIntent=mAction.actionIntent; } } } } @Override public void onNotificationRemoved(StatusBarNotification sbn) { }}
MainActivity.java
import android.app.Activity;import android.app.Notification;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.graphics.Bitmap;import android.os.Bundle;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.widget.ImageView;import android.widget.TextView; public class MainActivity extends Activity { protected MyReceiver mReceiver = new MyReceiver(); public static String INTENT_ACTION_NOTIFICATION = "it.sorry.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); 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); title.setText(notificationTitle); text.setText(notificationText); subtext.setText(notificationSubText); if (notificationLargeIcon != null) { largeIcon.setImageBitmap(notificationLargeIcon); } } } }}
相关文章推荐
- 关于NotificationListenerService监听时有失败的处理
- 关于NotificationListenerService监听时有失败的处理
- 关于Service的学习与使用
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android课程---关于Service的学习(后台运行)
- 关于错误:android.app.RemoteServiceException: Bad notification posted from package
- android的Service和Notification学习
- Android 通知使用权(NotificationListenerService)的使用
- 360卫士通知盒子和红包助手的实现原理(NotificationListenerService)
- 学习笔记(三)——关于Service的那些事
- 关于Notification的一些学习
- 添加第三方聊天软件QQ/微信/Twitter等桌面角标---NotificationListenerService
- 使用NotificationListenerService监听通知栏消息
- 关于ActivityManagerService的学习启动模式 7.23
- Android Api Demos学习之Notification Service Receiver 消息推广
- Java for Web学习笔记(七一):Service和Repository(6)在Spring框架中使用Listener
- [Android Pro] Android 4.3 NotificationListenerService使用详解
- iOS学习笔记6-关于NSNotificationCenter及同步异步
- Android开发学习之路-- 关于服务Service
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法