【问题标题】:Why doesn't sdk version check work?为什么 sdk 版本检查不起作用?
【发布时间】:2016-05-09 02:55:31
【问题描述】:

我有这个代码sn-p:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;

            if (currentapiVersion < Build.VERSION_CODES.JELLY_BEAN) {
                Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
                ComponentName cName = new ComponentName("com.android.phone", "com.android.phone.Settings");
                intent.setComponent(cName);

            } else {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
                context.startActivity(intent);
            }

为什么这在我的安卓手机(api level 15)上不起作用,但是当我用下面的代码替换它时,它就可以正常工作了?

                Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
                ComponentName cName = new ComponentName("com.android.phone", "com.android.phone.Settings");
                intent.setComponent(cName);

【问题讨论】:

  • 请解释“这不起作用”是什么意思。另请注意,您只在else 块中调用startActivity(),而不是if 块。
  • 天哪!谢谢……太明显了,我看不到……

标签: android sdk android-api-levels


【解决方案1】:

if (currentapiVersion

            Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);

            ComponentName cName = new ComponentName("com.android.phone", "com.android.phone.Settings");

            intent.setComponent(cName); 

            context.startActivity(intent);


        } else {

            Intent intent = new Intent();

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

         intent.setAction(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
            context.startActivity(intent);
        }

【讨论】:

    【解决方案2】:

    你没有在 if Block 中调用 startActivity,这就是问题发生的原因

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多