正在从通知栏中获取所有应用 android
Fetching all apps from the notification bar android
我需要获取通知栏中已存在的所有应用通知。为此,我使用 NotificationListenerService
和 BroadcastReceiver 来获取某些参数,例如 Notification.EXTRA_TITLE
、Notification.EXTRA_SUB_TEXT
等。但是 NotificationListenerService
面临的问题是我可以无法获取面板中的现有通知,其中我可以获得应用程序在前台时收到的未来通知。
请帮助我解决这个问题是否有 属性 来获取通知栏中已经显示的所有应用程序通知。我也发布了我正在使用的代码供您参考。
SimpleKitkatNotificationListener.java
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();
Log.i("notification",mNotification.tickerText.toString());
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) {
}
}
并且在 Activity class 中通过 BroadcastReceiver
接收回调如下
MainActivity.java
public class 主要Activity 扩展 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);
int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap notificationLargeIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON));
Bitmap notificationSmallIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_SMALL_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);
}else if(notificationSmallIcon !=null){
largeIcon.setImageBitmap(notificationSmallIcon);
}
}
}
}
以上如有不明之处敬请见谅。任何一段代码和 hind 都会对我很有帮助。提前致谢。
尝试阅读文档:
http://developer.android.com/reference/android/service/notification/NotificationListenerService.html
那里有一个示例,说明您如何定义服务来处理与 NLS 的绑定。这将允许您获取 NLS 实例并使用
我需要获取通知栏中已存在的所有应用通知。为此,我使用 NotificationListenerService
和 BroadcastReceiver 来获取某些参数,例如 Notification.EXTRA_TITLE
、Notification.EXTRA_SUB_TEXT
等。但是 NotificationListenerService
面临的问题是我可以无法获取面板中的现有通知,其中我可以获得应用程序在前台时收到的未来通知。
请帮助我解决这个问题是否有 属性 来获取通知栏中已经显示的所有应用程序通知。我也发布了我正在使用的代码供您参考。
SimpleKitkatNotificationListener.java
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();
Log.i("notification",mNotification.tickerText.toString());
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) {
}
}
并且在 Activity class 中通过 BroadcastReceiver
接收回调如下
MainActivity.java
public class 主要Activity 扩展 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);
int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap notificationLargeIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON));
Bitmap notificationSmallIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_SMALL_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);
}else if(notificationSmallIcon !=null){
largeIcon.setImageBitmap(notificationSmallIcon);
}
}
}
}
以上如有不明之处敬请见谅。任何一段代码和 hind 都会对我很有帮助。提前致谢。
尝试阅读文档:
http://developer.android.com/reference/android/service/notification/NotificationListenerService.html
那里有一个示例,说明您如何定义服务来处理与 NLS 的绑定。这将允许您获取 NLS 实例并使用