java - Android 通知生成器 setSmallIcon()

标签 java android notifications

我试图将小图标设置为网络速度,但应用程序每次都强制关闭。我不知道为什么会这样。这是我的通知方法。我在尝试设置 data_icon 时遇到错误。任何帮助将不胜感激。

public void showNotification(long receiveData) {
    List<String> connStatus = NetworkUtil.getConnectivityInfo(getApplicationContext());
    notificationManager = (NotificationManager) getSystemService("notification");
    Boolean notification_state = Boolean.valueOf(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("notification_state", true));
    String wifi_mobile_details = getWifiMobileData();
    String s = null;
    if (receiveData < PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID) {
        s = "b" + (((int) (receiveData / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID)) * 10);
    } else if (receiveData >= PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID && receiveData < 1048576) {
        s = "k" + (((int) receiveData) / 1024);
    } else if (receiveData >= 1048576 && receiveData < 10485760) {
        s = "m" + ((int) (((double) receiveData) / 104857.6d));
    } else if (receiveData >= 10485760 && receiveData <= 20971520) {
        s = "mm" + (((int) receiveData) / 1048576);
    } else if (receiveData > 20971520) {
        s = "mmm20";
    }
    data_icon = getResources().getIdentifier(s, "drawable", getPackageName());
    String network_name ;
    if (( connStatus.get(0)).equals("wifi_enabled")) {
        network_name = ( connStatus.get(1)) + " " + ( connStatus.get(2));
    } else if (( connStatus.get(0)).equals("mobile_enabled")) {
        network_name =  connStatus.get(1);
    } else {
        network_name = "";
    }
    DecimalFormat df = new DecimalFormat("#.##");
    String speed ;
    if (receiveData < PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID) {
        speed = "Speed " + ((int) receiveData) + " B/s" + " " + network_name;
    } else if (receiveData < 1048576) {
        speed = "Speed " + (((int) receiveData) / 1024) + " KB/s" + " " + network_name;
    } else {
        speed = "Speed " + df.format(((double) receiveData) / 1048576.0d) + " MB/s" + " " + network_name;
    }
    notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(data_icon)
            .setContentTitle(speed)
            .setContentText(wifi_mobile_details)
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
            .setAutoCancel(true);

    try{
        if (notification_state.booleanValue()) {
            notificationManager.notify(this.nid, notificationBuilder.build());
        } else {
            notificationManager.cancel(this.nid);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

这是我得到的错误:-

java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=com.techx.intenetspeedmeter/0x10900a1 vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)

最佳答案

问题在于 setSmallIcon(data_icon)。您正在使用 s 作为可绘制资源名称从 getResources().getIdentifier() 获取 data_icon。 s 最初为 null,然后您在 if...else 中为其赋值。可能没有满足任何条件,您的代码永远不会进入 if...else 并且您的变量 s 在那之后仍然为空。然后 getResources().getIdentifier() 方法将返回 0 然后 setSmallIcon(data_icon) 将抛出错误 Invalid notification (no valid small icon).

请确保您的变量 s 在 if...else 之后不为 null,如果它不为 null,则 data_icon 具有除 0 之外的有效 resource_id。

关于java - Android 通知生成器 setSmallIcon(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46442778/

相关文章:

java - 如果类都包含很多有用的类变量,是否会对性能产生影响?

java - 如何使用或运算符进行 HQL 查询

android - 如何避免回到之前的 Activity

android - 具体时间通知

android - 服务与 WindowManager 和通知

mysql - 将 MySQL 事件通知回 Delphi 应用程序

java - PoolingOptions 和 Cassandra - java

java - 循环遍历 csv 文件时出现奇怪的行

java - Firebase 数据库检索值,这可能吗

android - 如何使用 Retrofit 传递请求字符串参数进行文件上传?