【问题标题】:If statment : read the "else" beforeif 语句:阅读之前的“else”
【发布时间】:2020-01-13 15:29:40
【问题描述】:

您好,我需要有关 if 语句的帮助,它总是在检查条件之前跳转到 else ! 我的代码:

@Override
public void onReceive(Context p1, Intent p2)
{
    if ("android.intent.action.BOOT_COMPLETED".equals(p2.getAction())) {
        Toast.makeText(p1, "Device is ON from Recever",Toast.LENGTH_LONG).show();
    Intent starServiceIntent = new Intent(p1,MyService.class);
   p1 .startService(starServiceIntent);

    }   @Override
public void onReceive(Context p1, Intent p2)
{
    if ("android.intent.action.BOOT_COMPLETED".equals(p2.getAction())) {
        Toast.makeText(p1, "Device is ON from Recever",Toast.LENGTH_LONG).show();
    Intent starServiceIntent = new Intent(p1,MyService.class);
   p1 .startService(starServiceIntent);

    } if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){

        SharedPreferences prefs = p1.getSharedPreferences("myPrefss", Context.MODE_PRIVATE);
        if(!prefs.contains(incomingNumber)){
            Toast.makeText(p1,"True",Toast.LENGTH_SHORT).show();
            DateFormat timeFormat = new SimpleDateFormat("dd-MM-yy HH:mm");
            timeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
            String curTime = timeFormat.format(new Date());


            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(incomingNumber, curTime);
            editor.commit();
            sendSMS(incomingNumber,p1);

        }else{
            getStored(p1,incomingNumber);
            Toast.makeText(p1,"False",Toast.LENGTH_SHORT).show();
        }


        } 

//先给我假再给真,即使条件满足

【问题讨论】:

  • 如果去else,则条件为假。仔细检查您的数据。
  • 先给我假然后真,但为什么先给我假!

标签: android service broadcastreceiver


【解决方案1】:

这很可能是因为您在访问它之前没有在SharedPreferences 中存储任何值,因此在您的情况下它返回false。如需更好的解释,请参阅 Kaze 的this answer。

【讨论】:

    猜你喜欢
    • 2017-08-14
    • 2022-11-30
    • 2021-03-26
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2016-05-04
    • 2017-02-05
    • 2015-07-06
    相关资源
    最近更新 更多