2011-05-10 32 views
9

是否有任何可能的方式的時候調用的意圖活動時調用一個方法來調用一個方法。當我使用intent調用從一個活動切換到另一個時,我只想顯示特定的方法。如何使用意圖

回答

12

使用extras束在Intent

Intent i = new Intent(...); 
i.putExtra("your_condition", int_condition); 

然後在的Intent

int_condition=getIntent.getIntExtra("your_condition"); 

的onCreate現在你可以使用這個

if(int_condition==0) 
{ 
//Call the method 
} 
else 
{ 
//method you want 
} 

再有你的另一種選擇,你可以通過方法名作爲參數您Intent,假設您正在發送mathod_name作爲extraBundle

String method_name=getIntent.getIntExtra("method_name"); 
Class<?> c = Class.forName("class name"); 
Method method = c.getDeclaredMethod (method_name, parameterTypes) 
method.invoke (objectToInvokeOn, params) 
-3

這似乎更像是一個設計問題。明智地使用你的意圖過濾器,它應該很容易實現。

+0

請給出一些明智使用的例子。謝謝。 – jasonflaherty 2013-09-26 01:47:57

-7

不完全相信你的要價......但這種意圖將所謂有人

例如

Intent i = new Intent("android.intent.action.CALL", 
ContentURI.create("tel:" + phone)); 
startActivity(i); 

你必須有

<uses-permission id="android.permission.CALL_PHONE" /> 

否則清楚地把你的萬阿英,蔣達清。我不能夠inderstand

+0

我認爲他想打電話從意圖,最有可能的另一個應用程序的方法的具體方法。這就是爲什麼我說取決於調用意圖應該在應用程序流程中採取另一個邏輯。這只是一個設計問題。 – 2011-05-10 06:59:56