NotificationListenerService获取通知图标?

时间:2013-08-04 14:31:59

标签: android notifications android-4.3-jelly-bean

在扩展新版本(SDK18,JB-4.3)NotificationListenerService的服务中,我想获取通知的状态栏图标。

mStatusBarNotification.getNotification().icon返回状态栏drawable的资源ID,但该资源ID自然不在我的应用程序的范围/资源范围内。还有mStatusBarNotification.getNotification().largeIcon(返回Bitmap),但没有为所有通知设置并返回“错误”图标(展开的通知抽屉中的图像)。

2 个答案:

答案 0 :(得分:4)

使用getPackageName()上的StatusBarNotification查找发布Notification的应用。然后,您可以use createPackageContext()为该软件包获取Context,然后使用Context来检索图片(例如,通过getResources())。

答案 1 :(得分:0)

这是替代解决方法。

我们可以从import org.apache.spark.sql.{functions => F} // force the full dataframe into memory (could specify persistence // mechanism here to ensure that it's really being cached in RAM) df.cache() df.count() // calculate size of full dataframe val catalystPlan = df.queryExecution.logical val dfSizeBytes = spark.sessionState.executePlan(catalystPlan).optimizedPlan.stats.sizeInBytes for (col <- df.columns) { println("Working on " + col) // select all columns except this one: val subDf = df.select(df.columns.filter(_ != col).map(F.col): _*) // force subDf into RAM subDf.cache() subDf.count() // calculate size of subDf val catalystPlan = subDf.queryExecution.logical val subDfSizeBytes = spark.sessionState.executePlan(catalystPlan).optimizedPlan.stats.sizeInBytes // size of this column as a fraction of full dataframe val colSizeFrac = (dfSizeBytes - subDfSizeBytes).toDouble / dfSizeBytes.toDouble println("Column space fraction is " + colSizeFrac * 100.0 + "%") subDf.unpersist() } 获取可绘制对象,然后使用customview在通知中显示该可绘制对象。

这是使用android.icon值获取Drawable的方法:

sbn.getNotification().extras.getInt("android.icon")

我的 RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_push_notification); contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher); contentView.setTextViewText(R.id.title, notificationModel.getTitle()); contentView.setTextViewText(R.id.text, notificationModel.getBody()); try { //now get the context of other app and then get drawable from resoures Drawable drawable1 = context.createPackageContext(notificationModel.getPackageNmae(), CONTEXT_IGNORE_SECURITY).getDrawable(notificationModel.getIcon()); Bitmap bitmap = drawableToBitmap(drawable1); contentView.setImageViewBitmap(R.id.image, bitmap); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } 是:

notificationModel
相关问题