java - 如何从 NotificationListener 获取大小图标
问题描述
我想从 Mobile 中发布的任何应用程序的通知中获取图标。
为此,我创建了 NotificationListener 服务并覆盖了 on notification Posted 方法。
我已经尝试了以下方法,但它们都不起作用。请有人帮助我是新的和更瘦的。提前致谢。
@Override
public void onNotificationPosted(StatusBarNotification sbn){
Intent intent = new Intent("com.example.notify");
intent.putExtra("Notification sbn", sbn);
sendBroadcast(intent);
}
public class ImageChangeBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
StatusBarNotification sbn = intent.getParcelableExtra("Notification sbn");
Bitmap bitmap = (Bitmap) sbn.getNotification().extras.get(Notification.EXTRA_LARGE_ICON);
image1.setImageBitmap(bitmap);
image2.setImageBitmap(sbn.getNotification().largeIcon);
Notification notification = sbn.getNotification();
notification.getLargeIcon();
image3.setImageIcon(notification.getLargeIcon());
}
}
解决方案
在我的情况下,我使用自定义图像女巫我进入服务器并以大视图或任何大小的需要显示,。
我的广播接收器是:
public class CustomNotificationBroadcast extends BroadcastReceiver {
private String channelID = "1";
private String channelName = "news";
private String channelDesc = "news description";
@Override
public void onReceive(final Context context, final Intent intent) {
mContext = context;
Bundle extra = intent.getExtras();
if (extra != null) {
getNotification(modelNotification, null);
}
}
public void getNotification(ModelNotification remoteMessage, Class<?> action_click) {
Notification notification;
NotificationManager notificationManager;
notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.e(TAG, "getNotification Notif Type: if ");
channelID = "1";
channelName = "news";
channelDesc = "news description";
NotificationChannel notificationChannel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(channelDesc);
notificationChannel.enableLights(true);
notificationChannel.setSound(null, null);
notificationChannel.setLightColor(Color.GREEN);
notificationManager.createNotificationChannel(notificationChannel);
}
RingtoneManager.getRingtone(mContext,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotifTitle())
.setContentText(remoteMessage.getNotifBody())
.setVibrate(new long[]{100, 500, 500, 500, 500})
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
if (remoteMessage.getDataImage() != null) {
Bitmap bitmap = getBitmapfromUrl(remoteMessage.getDataImage());
builder.setStyle(
new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null)
).setLargeIcon(bitmap);
}
notification = builder.build();
notificationManager.notify(notfiId, notification);
}
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (Exception e) {
Log.e("awesome", "Error in getting notification image: " +
e.getLocalizedMessage());
return null;
}
}
我使用此方法处理图像 getBitmapfromUrl()
快乐编码:)
推荐阅读
- python - 可以使用 Mypy 给 fold_left() 一个类型吗?
- react-native - 如何根据 AsynStorage 值动态更改 react-navigation 标题样式?
- python - ReportLab:无法打开资源“images/logo_.jpg”
- python - server.py 无法启动,路径中的 unicode 字符有问题
- jsp - 如何将最大日期设置为当前日期并在日期选择器中隐藏所有即将到来和过去的日期
- azure - 从公共 Internet 访问保护 Azure 存储
- c - 忽略文本文件中的命令行
- javascript - 使用 OpenCV 和 Javascript 在 Canvas 上显示像素数组中的图像
- python - 无法在网络驱动器中运行 jupyter notebook (Windows)
- xamarin.android - 如何删除数组中不需要的行