顯示Intent的使用
- 首先再定義一個(gè)SecondActivity.java文件,
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
- 在寫一個(gè)second_layout.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/but_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 2"/> </LinearLayout> - 修改FristActivity中的按鈕點(diǎn)擊事件
Button but_1 = (Button)findViewById(R.id.but_1); but_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(FristActivity.this,SecondActivity.class); startActivity(intent); } });- 首先構(gòu)造了一個(gè)Intent,傳入FrinstActivity.this作為上下文,然后傳入要去的布局,最后通過這個(gè)startActivity()方法來執(zhí)行
隱式Intent用法
- 它并不明確的指明想要啟動(dòng)哪一個(gè)程序,而是指定更為抽象的action和category等信息,然后交由系統(tǒng)分析這個(gè)Intent,并幫我們找到合適的活動(dòng)去啟動(dòng)
- 通過<activity>標(biāo)簽下的<intent-filter>的內(nèi)容,可以指定當(dāng)前活動(dòng)能夠響應(yīng)的action和category,打開這個(gè)AndrioidManifest.xml這個(gè)文件,進(jìn)行改寫,action標(biāo)簽中指明我們可以響應(yīng)com.example.activitytest.ACTION_ATART這個(gè)action,而category這個(gè)標(biāo)簽更加精準(zhǔn)的指明了能夠響應(yīng)的Intent還可能帶有category,只有這兩個(gè)標(biāo)簽同時(shí)匹配才能響應(yīng)
<activity android:name=".SecondActivity"> <intent-filter> <action android:name="com.example.activitytest.ACTION_ATART"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> - 改變鼠標(biāo)點(diǎn)擊事件,這個(gè)時(shí)候并沒有寫這個(gè)category這個(gè)標(biāo)簽,但是還是能夠響應(yīng),因?yàn)閍ndroid.intent.category.DEFAULT是一種默認(rèn)的category,會(huì)自動(dòng)的添加
Button but_1 = (Button)findViewById(R.id.but_1);
but_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 這里面是action的值
Intent intent = new Intent("com.example.activitytest.ACTION_ATART");
startActivity(intent);
}
});
更多隱式Intent的用法
- 使用隱式的Intent不僅可以啟動(dòng)自己程序內(nèi)的活動(dòng),還可以啟動(dòng)其他程序的活動(dòng),比如想要在程序中顯示一個(gè)網(wǎng)頁,這個(gè)時(shí)候就只要調(diào)用系統(tǒng)的網(wǎng)頁就可以了,修改FirstActivity.xml
Button but_1 = (Button)findViewById(R.id.but_1);
but_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
-
這里指定的action是Intent.ACTION_VIEW,這是一個(gè)內(nèi)置的,通過這個(gè)Uri.parse()方法可以把一個(gè)網(wǎng)址解析成一個(gè)Uri對(duì)象,再通過Intetn的setData方法把這個(gè)對(duì)象傳進(jìn)去,再startActivity打開,這個(gè)時(shí)候點(diǎn)擊這個(gè)按鈕,進(jìn)入的就是百度頁面
2018-03-12_16-45-14.png
- 還可以在<intent-filter>標(biāo)簽中再配置一個(gè)<data>標(biāo)簽,更加正確的指定當(dāng)前活動(dòng)可以響應(yīng)什么類型的數(shù)據(jù),<data>中標(biāo)簽只要可以配置的信息
- android:scheme 用于指定數(shù)據(jù)的協(xié)議部分,
- android:host 用于指定數(shù)據(jù)的主機(jī)名
- androdi:port 用于指定數(shù)據(jù)的端口部分
- android:path 用于指定主機(jī)名和端口之后的部分
只有data標(biāo)簽中指定的內(nèi)容和Intent攜帶的Data完全一致,當(dāng)前活動(dòng)才能夠響應(yīng)該Intetn
例如:FirstActivity.xml不用動(dòng),新建一個(gè)ThirdActivity(內(nèi)容不用動(dòng)),布局文件third_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/but_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3"/>
</LinearLayout>
AndroidManifest文件中
<activity android:name=".ThirdActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
-
這個(gè)時(shí)候我們通過<data android:scheme="http"/>指定了協(xié)議必須是http協(xié)議,這樣ThirdActivity就可以象瀏覽器一樣響應(yīng)一個(gè)打開網(wǎng)頁Intent了
2018-03-12_18-52-25.png
向下一個(gè)活動(dòng)傳遞數(shù)據(jù)
- 使用的是Intetn提供的putExtra()方法,可以把想要傳遞的數(shù)據(jù)暫存起來,然后到下一個(gè)活動(dòng)中取出來就可以了,修改FristActivity中的代碼
Button but_1 = (Button)findViewById(R.id.but_1);
but_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(FristActivity.this,SecondActivity.class);
String data = "我是要傳遞的數(shù)據(jù)";
intent.putExtra("data",data);
startActivity(intent);
}
});
- 點(diǎn)擊這個(gè)按鈕的時(shí)候,會(huì)到Second的布局中,這個(gè)時(shí)候調(diào)用了intent的putExtra()方法,傳入的是鍵,和值,還是使用的startActivity()的方式來啟動(dòng)
- 在SecondActivity中就可以獲取到存入的數(shù)據(jù)了
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent intent = getIntent();
String data = intent.getStringExtra("data");
Log.d("SecondActivity",data);
}
}
-
首先通過getIntent()方法獲取到用去啟動(dòng)SecondActivity的Intent,然后調(diào)用相應(yīng)的方法getStringExtra(),傳入鍵就可以了,若傳入的是整形,那就使用getIntExtra()方法,查看logcat打印的信息就可以看到了
2018-03-12_17-55-32.png
返回?cái)?shù)據(jù)給上一層
- 這個(gè)時(shí)候就必須的使用Activity中的startActivityforResult()方法來啟動(dòng)活動(dòng)了,這個(gè)方法期望在這個(gè)活動(dòng)摧毀的時(shí)候可以傳遞一個(gè)數(shù)據(jù)給上一個(gè)活動(dòng),這個(gè)方法里有兩個(gè)參數(shù),第一個(gè)還是Intent,第二個(gè)是請(qǐng)求碼,用于在之后的回調(diào)中判斷數(shù)據(jù)的來源,修改一下FirstActivity中按鈕的點(diǎn)擊事件
Button but_1 = (Button)findViewById(R.id.but_1);
but_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(FristActivity.this,SecondActivity.class);
// 這里啟動(dòng)活動(dòng)的方法變了
startActivityForResult(intent,1);
}
});
- 這里使用了 startActivityForResult()方法啟動(dòng)活動(dòng),請(qǐng)求碼必須得唯一
- 在SecondActivity中給按鈕注冊(cè)點(diǎn)擊事件
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Button but_2 = (Button)findViewById(R.id.but_2);
but_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("data_return","我是向上傳遞的數(shù)據(jù)");
setResult(RESULT_OK,intent);
finish();
}
});
}
}
- 首先還是構(gòu)建了一個(gè)Intent,調(diào)用Intent的putExtra()方法存入數(shù)據(jù),
- 然后調(diào)用了setResult()方法,這個(gè)方法非常重要,用于向上返回?cái)?shù)據(jù),第一個(gè)參數(shù)是用于向上一個(gè)活動(dòng)返回處理結(jié)果集,一般就只有RESULT_OK或者RESULT_CANCELED這兩個(gè)參數(shù),第二個(gè)參數(shù)是把帶有數(shù)據(jù)的intent返回
- 最后調(diào)用finish()方法銷毀當(dāng)前的活動(dòng)
- 由于是使用startActivityForResult()方法啟動(dòng)SecondActivity,那么在SecondActivity銷毀的之后就會(huì)回調(diào)上一個(gè)活動(dòng)的onActivityResult()方法,所以在FristActivity中重寫這個(gè)方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case 1:
if(resultCode == RESULT_OK){
String returnData = data.getStringExtra("data_return");
Log.d("FristActivity",returnData);
}
break;
default:
}
}
- 第一個(gè)參數(shù)就是在啟動(dòng)活動(dòng)的時(shí)候傳入的請(qǐng)求碼(唯一)
- 第二個(gè)參數(shù)是返回?cái)?shù)據(jù)的時(shí)候傳入的處理結(jié)果
-
第三個(gè)參數(shù)是攜帶數(shù)據(jù)的intent
2018-03-12_18-29-51.png
- 這個(gè)時(shí)候如果沒有在SecondActivity界面點(diǎn)擊按鈕,而是直接使用Back鍵返回的,這樣的話數(shù)據(jù)就返不回來了,所以在SecondActivity中重寫這個(gè)onBackPressed()
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("data_return","我是通過返回鍵向上傳遞的數(shù)據(jù)");
setResult(RESULT_OK,intent);
finish();
}
-
當(dāng)按到Back鍵的時(shí)候,就會(huì)執(zhí)行這個(gè)方法onBackPressed()
2018-03-12_18-35-38.png




