首页 > 解决方案 > Switch Case 语句或其替代 If Condition 在 android 中不起作用

问题描述

我使用 switch case 进行条件检查。它不工作。这里我是如何实施的。

@Override
public void onReceiveNotification(String notificationType, String message, String flag) {
    super.onReceiveNotification(notificationType, message, flag);
    Log.d("tag", "==onReceive Notivation====1===" + notificationType);
    if (notificationType.equalsIgnoreCase("MULTIPART_TYPES"))
    {
        Log.d("tag", "==onReceive Notivation===2====" + notificationType);
    }
    switch (notificationType) {
        case "MULTIPART_TYPES":
            Log.d("tag", "==onReceive Notivation=======" + message);
            break;
    }
}

这是我的日志输出

==onReceive Notivation====1=== MULTIPART_TYPES

但是,它正在使用受尊重的字符串消息进入该方法。但它不是进入切换或if条件。

标签: javaandroidif-statementswitch-statement

解决方案


您的 notificationType 前面有一个空格。删除它或为您的支票添加修剪。像这样的东西。

if (notificationType.trim().equalsIgnoreCase("MULTIPART_TYPES"))
    {
        Log.d("tag", "==onReceive Notivation===2====" + notificationType);
    }

推荐阅读