2014-09-02 31 views
2

雖然有一些Android的代碼工作,我碰到一個疑問來了(可能是愚蠢的),但我想清除...意圖參數


我嘗試:啓動每次在一個應用程序中都可以看到啓動畫面,該應用程序也會讀取某些共享首選項,並根據該共享首選項的值打開下一個活動。


我卡住了:當我無法弄清楚,以什麼作爲參數傳遞給意圖(見代碼的唯一評論)類型的activityToOpen

if(appStatus==0) 
{ 
    activityToOpen="LoginActivity"; 
} 
else if(appStatus==1) 
{ 
    activityToOpen="SecondActivity"; 
} 

int secondsDelayed = 1; 
    new Handler().postDelayed(new Runnable() { 
      public void run() { 
      Intent i=new Intent(MainSplashScreen.this,);//here how should be the second argument will be added 
      startActivity(i); 
        finish(); 
      } 
    }, secondsDelayed * 1000); 

String 。我不知道它是什麼類型的變量,.class

+1

新的意圖(MainSplashScreen.this,LoginActivity.class) ; 1參數從where 2到where – UMESH0492 2014-09-02 06:10:14

回答

2

使用通用類作爲

Class<?> cls = null; 

if(appStatus==0) 
{ 
    cls = LoginActivity.class; 
} 
else if(appStatus==1) 
{ 
    cls = SecondActivity.class; 
} 

又通cls參數作爲

Intent i=new Intent(MainSplashScreen.this,cls); 
+0

究竟是需要什麼....謝謝 – nobalG 2014-09-02 06:10:28

+0

@ DividebyZero樂於幫助,享受編碼。 – 2014-09-02 06:10:52

3

你可以用if statement輕鬆做到這一點,並使用你的appStatus來檢查你是否想要去LoginActivitySecondActivity並根據令牌實例化您的Intent構造函數。

樣本:

new Handler().postDelayed(new Runnable() { 
     public void run() { 
     Intent i; 
     if(appStatus==0) 
      i=new Intent(MainSplashScreen.this,LoginActivity.class); 
     else if(appStatus==1) 
      i=new Intent(MainSplashScreen.this,SecondActivity.class); 

     startActivity(i); 
       finish(); 
     } 
}, secondsDelayed * 1000); 
+0

是的,它會工作。但是沒有辦法設置某個變量的值並將它作爲第二個參數傳遞給intent? – nobalG 2014-09-02 06:05:37

+2

upvoted,但@Giru Bhai答案是我正在尋找 – nobalG 2014-09-02 06:09:10

+0

@DividebyZero我的意思是upvote答案,回答你的問題 – 2014-09-02 06:12:24

0

你讓使用循環類的實例,並把它作爲第二個參數。

例如:

讓每一個靜態字段爲public static Activity activity = this; 然後把它和傳遞對象作爲第二個參數

當然,你需要使用獲得活動condition

2

使用以上的if else下面這樣:...........

Intent i; 
if(appStatus==0) 
{ 
    i = new Intent(this,LoginActivity.class); 
} 
else if(appStatus==1) 
{ 
    i = new Intent(this,SecondActivity.class); 
} 
startActivity(i); 
finish();