关于android4.3中的NotificationListenerService的学习
来源:互联网 发布:查看tomcat端口 编辑:程序博客网 时间:2024/06/17 18:52
是这么定义的:
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.javaSimpleKitkatNotificationListener.javaimport 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 {@Overridepublic void onCreate() {super.onCreate();//android.os.Debug.waitForDebugger();}@Overridepublic 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;}}}}@Overridepublic void onNotificationRemoved(StatusBarNotification sbn) {}}
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 elementstitle = (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);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.main, menu);return super.onCreateOptionsMenu(menu);}@Overridepublic 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);}@Overrideprotected void onResume() {super.onResume();if (mReceiver == null) mReceiver = new MyReceiver();registerReceiver(mReceiver, new IntentFilter(INTENT_ACTION_NOTIFICATION));}@Overrideprotected void onPause() {super.onPause();unregisterReceiver(mReceiver);}public class MyReceiver extends BroadcastReceiver {@Overridepublic 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);}}}}}
0 0
- 关于android4.3中的NotificationListenerService的学习
- 关于NotificationListenerService监听时有失败的处理
- NotificationListenerService
- 通知使用权(NotificationListenerService)的使用
- NotificationListenerService失效的两种情况
- 关于Android4.4的Keyguard
- Android4.1 关于Rotation相关的Configuration整体分析3
- Android4.1 关于Rotation相关的Configuration整体分析3
- android4.2.2设置选项中的关于设备或者关于平板电脑的字段内容由来之/proc/version
- 关于Android4.0的Alertdialog对话框
- 关于android4.0的IDC文件
- 关于Android4.0的Alertdialog对话框
- 关于android4.0 cts的一些总结
- 关于Android4.0的Alertdialog对话框
- 关于android4.0.4中彩蛋的实现
- 关于android4.0.4中彩蛋的实现
- 关于android4.0 EditText的边框
- 关于Android4.4的Strict Mode
- UVALive - 4356 Fire-Control System
- java单例类
- ruby 中的Block,lambda,Proc
- 枚举类型的定义
- 5. Android使用意图传递数据之全局变量传递-Application类
- 关于android4.3中的NotificationListenerService的学习
- shell抓取音频文件
- 用Python和Pygame写游戏-从入门到精通(11)
- 编辑文章 - 博客频道 - CSDN.NET
- 线程学习之-创建线程
- 用Python和Pygame写游戏-从入门到精通(13)
- ios7下,NSURLSessionUploadTask和NSURLSessionDownloadTask的奇怪行为
- Ruby之self,上下文
- How to create custom snippet in Visual studio