android - 如何动态设置小图标通知

标签 android

我想设置从服务器接收到的小图标通知。我的应用程序下载了一个图标,我想用这个图标显示通知。

我看到了这个问题,但找不到答案。

File myFile = new File(myPicAddress);
Notification noti = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setContentTitle("UPDATE APP")
                .setContentText(myNotificationText)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.ic_launcher)//set myFile??
                .setTicker("force android update!")
                .setWhen(triggerTime)
                .build();

我的应用已连接到服务器并下载我的图标,然后我想显示带有从服务器下载的图标的通知。

最佳答案

首先从url下载位图,然后将其设置为图标,如下所示

Bitmap bitmap = getBitmapFromURL("Your URL");


public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

然后将位图设置为小图标

Notification noti = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setContentTitle("UPDATE APP")
                .setContentText(myNotificationText)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(bitmap)//set myFile??
                .setTicker("force android update!")
                .setWhen(triggerTime)
                .build();

已针对 API 23 及更高版本进行更新,如下所示

notificationBuilder.setSmallIcon(Icon.createWithBitmap(yourDownloadedBitmap));

关于android - 如何动态设置小图标通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47495052/

相关文章:

Android 如何从 DialogFragment 正按钮单击调用 fragment 方法

android - MaterialDatePicker 仅在 2 个日期之间选择日期

java - 使 onPreExecute 和 onProgressUpdate 对所有 Asynctask 类通用

java - 使用 OData WCF 的 Android 应用程序 - Android 应用程序不断崩溃

android - 如何调用 startActivityForResult 方法从另一个应用程序获取结果

android - 如果启用了硬件加速(Android 3.0+),WebView "flashing"带有白色背景

android - 将 motionEvent 从一个 scrollView 传递到另一个

java - 使用 Mapbox 查找左下角 + 右上角(纬度、经度) - [Android]

android - 为什么我收到表名称的列不存在错误

java - 从两个不同的数据源中选择排序的数据