2016-01-31 25 views
-2

我有這樣的代碼片段:爲什麼sdk版本檢查工作不正常?

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); 
      } 

爲什麼我的Android手機(API等級15)這方面的工作,但是當我用下面的代碼替換它,它工作正常?

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

請解釋什麼是「沒有按這項工作「的意思。還要注意,你只在你的'else'塊中調用'startActivity()',而不是'if'塊。 – CommonsWare

+0

哦,我的上帝!謝謝......很明顯我看不到它...... – alfakaiwmega

回答

0

如果(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); 

      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); 
     } 
0

üdidnt調用startActivity在if塊,這就是爲什麼問題是發生