2013-09-01 94 views
0

假設系統沒有錯誤和警告,是否有這樣的:Android的意圖:檢索意圖參數

Wanted_LoginController.java:

Intent intent = new Intent(loginMVC.getLoginView(),ErrorView.class); 
intent.putExtra("errorMsg", errorMsg); 
loginMVC.getLoginView().startActivity(intent); 

Wanted_ErrorView.java:

Intent executedIntent = getIntent(); 
LoginMVC loginMVC = executedIntent.getParameter1(); //will store loginMVC context from previous intent data 
ErrorView errorView = executedIntent.getParameter2(); //will store errorview from previous intent data 

MY CUSTOM OBJECTS

ErrorView.java:

public class ErrorView extends Activity{ 
    ErrorView context; 
    Bundle extras; 

} 

LoginView.java:

public class LoginView extends ListActivity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview); 
    //check the device's connection to the internet 
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    if(mWifi.isConnected()){ 
     new LoginController(new LoginMVC(new LoginData(), this)); //builds a new controller for the login process  
    }else{ //blocks the user to proceed to login when internet connection is not available 
     finish(); 
     Intent intent = new Intent(this,ErrorView.class); 
     intent.putExtra("errorMsg", "Please enable Wi-Fi"); 
     startActivity(intent); 
    } 
    } 
} 

LoginMVC.java:

public class LoginMVC{ 
    LoginData loginModel; 
    LoginView loginView; 
    LoginController loginController; 

    public LoginMVC(LoginData pLoginModel, LoginView pLoginView, LoginController pLoginController){ 
    loginModel = pLoginModel; 
    loginView = pLoginView; 
    loginController = pLoginController; 
    } 

    public LoginMVC(LoginData pLoginModel, LoginView pLoginView){ 
    loginModel = pLoginModel; 
    loginView = pLoginView; 
    } 

    public LoginController getLoginController(){ 
    return loginController;} 
} 

謝謝!

+0

請參閱Intent.putExtra和Intent.getExtra –

+0

感謝您的答覆,但我需要的只是檢索參數本身,而不是意圖的額外。 –

+0

你的意思是活動名稱? – Mohit

回答

0

你的意思是檢索執行的意圖本身的參數?

截至目前,仍然沒有辦法做到這一點。

0

是的,您可以。 您可以將源代碼包傳遞給源活動的意圖,並獲取目標活動的意圖。

下面是一個例子,

//你的源活動代碼:

  Bundle bundle = new Bundle(); 
      String s = "Hello world"; 
      bundle.putString("alpha", s); 
      i = new Intent(this, DestinationActivity.class); 
      i.putExtras(bundle); 
      startActivity(i); 

//你的目的地活動代碼:

 Bundle b = this.getIntent().getExtras(); 

    if (b.containsKey("alpha")) { 

      String text = (String) getIntent().getSerializableExtra("alpha"); 
     // Now you can use text i.e you can show toast e.t.c 


    } 
+0

感謝您的回答。但我不需要使用bundle和extras,因爲它不能存儲我必須檢索的CLASS上下文,而不是String,char,boolean,int或floating類型。 –