Android Notification Builder setSmallIcon()

时间:2017-09-27 08:17:49

标签: java android notifications

我正在尝试将小图标设置为网络速度,但App力每次都会关闭。我不知道为什么会这样。这是我的通知方法。我在尝试设置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)

1 个答案:

答案 0 :(得分:1)

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

你能否确保你的变量s在if ... else之后不为空,如果它不为null,那么data_icon的有效resource_id除了0之外。