简体   繁体   English

添加 if 条件退出电话管理器中的 android 应用程序

[英]Adding the if condition quits the android app in telephony manager

I am trying to make a simple android app in which there is a toast message containing the number of the incoming call.我正在尝试制作一个简单的 android 应用程序,其中有一条包含来电号码的 toast 消息。 I have stored the number in a string variable.我已将数字存储在字符串变量中。 When I display just the toast, it works fine but before the toast, if I add a simple if condition comparing the number to another string, the app quits.当我只显示 toast 时,它工作正常,但在 toast 之前,如果我添加一个简单的 if 条件将数字与另一个字符串进行比较,应用程序将退出。 The required permissions are given.提供了所需的权限。 Can someone help me?有人能帮我吗?

This Works:这有效:

public void onReceive(Context context, Intent intent) {

        String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
    }

This Does not Work (App quits)这不起作用(应用程序退出)

public void onReceive(Context context, Intent intent) {

        String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
          if(incomingNumber.equals("+919999999999"))
           {
              Toast.makeText(context, "Call from Mom", Toast.LENGTH_LONG).show();
           }
           else
           {
              Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
           }

    }

It sounds a little silly but I just wrapped my "if" condition in a try-catch block and it solved the problem.这听起来有点傻,但我只是将我的“if”条件包装在 try-catch 块中,它解决了问题。 I am not used to programming in JAVA so it took me a lot to figure out this simple thing.我不习惯在 JAVA 中编程,所以我花了很多时间才弄清楚这个简单的事情。 Thank You All for providing me with suggestions:)谢谢大家为我提供建议:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM