版權聲明:歡迎轉載,請注明沉默王二原創。 https://blog.csdn.net/qing_gee/article/details/50378561
Android 了解完activity的基本知識,接下來了解Android intent(據說是多個activity之間的媒介)。
學習的過程自然少不了參照,我選擇的參照是郭霖老師的《第一行代碼Android》。
學習是個循序漸進的過程,王二不是一個天賦異禀的同學,是以隻能每天多積積矽步。今天看到
專訪任玉剛:從菜鳥到資深工程師的進階之路 ,又一個移動端的大牛登上了CSDN的首頁資訊,王二我略感興奮,這興奮不是說我自己也想有朝一日如此(但隐隐約約有點,笑),而是心中又多了一個行進的标杆。一、顯式intent
建立second_layout.xml。
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2"
/>
建立SecondActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
}
注冊activity
<activity
android:name="com.mwq.activity.SecondActivity"
></activity>
在FirstActivity.java中增加顯示intent
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
});
- 通過new關鍵字建立intent對象,其構造方法的參數分别是FirstActivity這個context,以及SecondActivity這個class。
- 通過startActivity方法啟動intent。
- 關鍵的地方來了,如果首次見
一定會感興趣,類名.this就是匿名内部類通路外部類對象的一種方法,這是約定俗成的格式。至于說在上面這段代碼中,誰是匿名内部類,誰是外部類,答案是顯而易見的:FirstActivity.this
就是匿名内部類,而代碼所在的FirstActivity.java檔案對應的上下文就是外部類。new OnClickListener() {}
- 如果你對類名.this和類名.class感興趣的話,可以參閱 類名.this與類名.class 。
二、隐式intent
首先關于隐式intent的概念,我沒有很好的了解,無論是書中所說“相比于顯式intent,隐式intent則含蓄得多,它并不明确指出我們想要啟動哪一個活動,而指定了一系列更為抽象的action和category等資訊,然後交由系統分析這個intent,并幫助我們找到合适的活動去啟動”,還是
Android入門:隐式Intent文中所說
顧名思義,隐式意圖就是在不明确設定激活對象的前提下尋找最比對的元件,舉個例子,比如有5個人:
(1)A:170cm
(2)B:160cm
(3)C:180cm
(4)D:190cm
(5)E:200cm
如果是顯式意圖的話,如果我們要指明選擇A的話會說:”我選擇A.“,但是如果是隐式意圖,則會說:”我要選擇170cm的人“,雖然沒有指明要選A,但會尋找條件最比對的人。
我都不太認可這種說法,但我此刻也沒有好的說法,真是無奈。
AndroidManifest.xml中為SecondActivity增加action和category。
<activity
android:name="com.mwq.activity.SecondActivity">
<intent-filter>
<action android:name="com.mwq.activity.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
- 非常詫異的是,為什麼
要使用一個附帶包路徑的字元串,當然了去掉包路徑,使用“ACTION_START”也能啟動,隻要和intent中的參數比對就行,但因為我是初學者,先按照規矩來辦事吧。action android:name
- 至于說
為什麼是·category android:name
,我也不太清楚,不過去掉這個參數後,隐式intent就不能跳轉了,整個app會跳出。android.intent.category.DEFAULT
FirstActivity.java,把顯式intent改成隐式intent。
// Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Intent intent = new Intent("com.mwq.activity.ACTION_START");
- 這樣啟動程式後,發現也是可以跳轉的。
- 另外書中提到增加一個category,我就不再贅述了。
三、好玩的intent
①、打開網頁
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://blog.csdn.net/qing_gee"));
startActivity(intent);
Create an intent with a given action. All other fields (data, type, class) are null. Note that the action must be in a namespace because Intents are used globally in the system – for example the system VIEW action is android.intent.action.VIEW; an application’s custom action would be something like com.google.app.myapp.CUSTOM_ACTION.
關于Intent(String)的構造方法,上面的doc說得也夠仔細的。
嗨嗨,挺不錯,可以打開我的部落格。
請允許我再說幾句題外話。前幾日,我更新了自己的頭像,終于可以見到王二的真面目了,隻可惜,像素低了點,遺憾,以後可以再聚焦到頭部一些。以前有為叫momasp的朋友,噴我用女生李孝利的頭像賺取流量,我是十分的不解,我沒見到自己流量上增加了多少,真是愧對了李孝利這位美女。做一個沉默的程式員都挺難了,還得被說三道四,真是難上加難,哈哈。不過呢,經過反思和自我教育,我還是決定痛改前非,使用自己真實的頭像。
②、打電話
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
- dial的英文單詞意思就是撥打電話的意思。
- 有點遺憾的是我的安卓模拟器出來的内容和書中不一樣,顯然這也不是我想要的效果。
另外,還有其他的使用,可參見
Android 打開地圖并顯示指定的經緯度,發短信,顯示地理位置等等。
四、資料傳遞
①、向下傳遞
FirstActivity.java
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("name", "沉默王二");
SecondActivity.java
Intent intent = getIntent();
String name = intent.getStringExtra("name");
Log.d("SecondActivity", name);
方式呢,前後呼應,倒也挺好記憶的。
②、向上傳遞
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(intent, 1111);
- 使用startActivityForResult方法來啟動intent,傳遞的第二個參數是一個requestcode。
requestCode If >= 0, this code will be returned in onActivityResult() when the activity exits.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1111:
if (resultCode == RESULT_OK) {
String response = data.getStringExtra("name");
Log.d("FirstActivity", response);
}
break;
default:
break;
}
}
- 比對requestcode後,判斷resultcode是否為ok,然後輸出response。
SecondActivity.java中增加對應的處理
Button button = (Button)findViewById(R.id.button_2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("name", "沉默王三");
setResult(RESULT_OK, intent);
finish();
}
});
- 使用預設的構造方法建立intent。
- 傳遞參數name
- setResult 1 方法來向上一個activity傳遞資料
- finish主動結束活動
- 為什麼建立一個預設的intent就能向上一個活動傳遞參數,之前的intent都需要指定對應參數,這一次為什麼不呢?顯然這就需要下一次學習活動的生命周期了,據說是先進後出的棧。
- 至于說按菜單中的back鍵傳回,隻需要重寫SecondActivity.java的onBackPressed方法就行了。
目前學習到的intent的知識倒是不難,很容易了解,但要整理成文,的确還是花了不少時間。
相關文章
笑對現實的無奈,不能後退的時候,不再傍徨的時候,永遠向前 路一直都在──陳奕迅《路一直都在》
本文出自:【
沉默王二的部落格 】
- Call this to set the result that your activity will return to its caller. ↩