Intent
Intent是Android程序組件之間交互的一種重要方式弄慰,可以指明當(dāng)前組件要執(zhí)行的動(dòng)作,還可以在組件間傳遞數(shù)據(jù)。
他被用于啟動(dòng)活動(dòng)胃碾,啟動(dòng)服務(wù)和發(fā)送廣播等場景,Intent有多個(gè)構(gòu)造函數(shù)的重載筋搏。
使用方式
- 顯示Intent
- 隱式Intent
使用顯示的Intent
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
Intent(Context context, Class<?>cls):是構(gòu)造函數(shù)其中的一個(gè)重載仆百。
第一個(gè)參數(shù)是啟動(dòng)活動(dòng)的上下文,第二個(gè)是想要啟動(dòng)的目標(biāo)活動(dòng)奔脐。
startActivity(Intent intent):是啟動(dòng)活動(dòng)的方法俄周。
此種方式啟動(dòng)活動(dòng)的吁讨、,意圖非常明顯峦朗,所以稱顯示Intent建丧。
使用隱式的Intent
隱式intent通過在AndroidManifest.xml中的<activity>標(biāo)簽配置<action>標(biāo)簽和<category>標(biāo)簽等信息,然后系統(tǒng)自動(dòng)分析使用波势。
<activity android:name=".chapter02.IntentImplicitActivity">
<intent-filter>
<action android:name="com.bl.blandroidpro.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.bl.blandroidpro.MY_CATEGORY" />
</intent-filter>
</activity>
Intent intent = new Intent("com.bl.blandroidpro.ACTION_START");
intent.addCategory("com.bl.blandroidpro.MY_CATEGORY");
Intent(String action):是構(gòu)造函數(shù)的另一個(gè)重載翎朱,以一個(gè)action字符串作為參數(shù)。
intent.addCategory(String category):通過此函數(shù)添加新的類別尺铣,別忘了在activity 標(biāo)簽中進(jìn)行配置拴曲,否則程序崩潰。
使用Intent打開網(wǎng)頁
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
Intent.ACTION_VIEW:是安卓系統(tǒng)的內(nèi)置動(dòng)作凛忿,其常量值為android.intent.action.VIEW澈灼。
intent.setData(Uri uri):將字符串解析成Uri,再通過setData方法傳遞進(jìn)去侄非。
<data>標(biāo)簽:用于更精確的指定當(dāng)前活動(dòng)能夠響應(yīng)什么類型的數(shù)據(jù)蕉汪。
- android:scheme 指定協(xié)議。
- android:host 指定主機(jī)名逞怨。
- android:port 指定端口者疤。
- android:path 指定路徑。
- android:mimeType 指定可以處理的數(shù)據(jù)類型叠赦。
<activity android:name=".chapter02.IntentOpenWebActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" />
</intent-filter>
</activity>
使用Intent打電話
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
使用Intent傳遞數(shù)據(jù)給下一個(gè)活動(dòng)
putExtra(String key, Object value):通過使用此方法進(jìn)行設(shè)置
// 第一個(gè)活動(dòng)
Intent intent = new Intent(FristActivity.this, SecondActivity.class);
intent.putExtra("transmit_data_1", "Hello");
intent.putExtra("transmit_data_2", new String[]{"World!"});
intent.putExtra("transmit_data_3", 333});
startActivity(intent);
從Intent中獲取數(shù)據(jù)
// 第二個(gè)活動(dòng)
Intent intent = getIntent();
String data_1 = intent.getStringExtra("transmit_data_1");
String[] data_2 = intent.getStringArrayExtra("transmit_data_2");
int data_3 = intent.getIntExtra("transmit_data_3");
getIntent():獲取當(dāng)前intent驹马。
getXXXExtra():獲取對應(yīng)類型的數(shù)據(jù)。
返回?cái)?shù)據(jù)給上一個(gè)活動(dòng)
// 第一個(gè)Activity
Intent intent = new Intent(FristActivity.this, SecondActivity.class);
startActivityForResult(intent, 1);
// 接收返回的數(shù)據(jù)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String[] res = data.getStringArrayExtra("feedback_data");
tv_intent_res.setText(res[0] + res[1]);
}
break;
default:
break;
}
}
// 第二個(gè)Activity
Intent intent = new Intent();
intent.putExtra("key1", "測試數(shù)據(jù)");
setResult(RESULT_OK, 1);
finish();
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("feedback_data", new String[]{"Yes, I", " konw!"});
setResult(RESULT_OK, intent);
finish();
}
startActivityForResult():使用此方法啟動(dòng)activity表示此活動(dòng)要接收返回的數(shù)據(jù)除秀。
onActivityResult():重寫此方法接收返回的Intent中的數(shù)據(jù)糯累。
- requestCode:啟動(dòng)活動(dòng)時(shí)的請求碼。
- resultCode:返回?cái)?shù)據(jù)時(shí)的處理結(jié)果册踩。
- data:返回?cái)?shù)據(jù)的intent泳姐。
setResult():將intent設(shè)置到result中。
onBackPressed():重寫此方法暂吉,用戶點(diǎn)返回鍵時(shí)調(diào)用此方法胖秒。
代碼示例
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bl.blandroidpro">
<application
android:name=".chapter13.BLApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".chapter02.FirstActivity" />
<activity android:name=".chapter02.SecondActivity">
<intent-filter>
<action android:name="com.bl.blandroidpro.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.bl.blandroidpro.MY_CATEGORY" />
</intent-filter>
</activity>
<activity android:name=".chapter02.IntentOpenWebActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" />
</intent-filter>
</activity>
</application>
activity_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/my_nav" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_intent_explicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用顯示Intent" />
<Button
android:id="@+id/btn_intent_implicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用隱式Intent" />
<Button
android:id="@+id/btn_intent_web"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打開網(wǎng)頁 百度" />
<Button
android:id="@+id/btn_intent_tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打電話 10086" />
<Button
android:id="@+id/btn_intent_transmit_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用 Intent 傳遞數(shù)據(jù)" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="反饋回來的數(shù)據(jù)"/>
<TextView
android:id="@+id/tv_intent_res"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<WebView
android:id="@+id/wv_intent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:padding="0dp"
android:background="#666"></WebView>
</LinearLayout>
</ScrollView>
</LinearLayout>
FirstActivity.java
public class FirstActivity extends MyBaseActivity {
TextView tv_intent_res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
// 使用 顯示Intent
Button btn_intent_show = findViewById(R.id.btn_intent_explicit);
btn_intent_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent 有很多構(gòu)造函數(shù),像這種意圖很明顯慕的,稱為顯示 Intent
Intent intent = new Intent(IntentActivity.this, IntentExplicitActivity.class);
startActivity(intent);
}
});
// 使用 隱式Intent
Button btn_intent_implicit = findViewById(R.id.btn_intent_implicit);
btn_intent_implicit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 在 AndroidManifest.xml 中 activity 標(biāo)簽中設(shè)置 action 及 category
// 在 action 和 category 同時(shí)匹配下才可執(zhí)行
// 這種通過系統(tǒng)分析你要做什么的形式稱為 隱式 Intent
// 此處添加 action 字符串
Intent intent = new Intent("com.bl.blandroidpro.ACTION_START");
// 此處添加自定義的 category阎肝,default 為默認(rèn)的,不需要添加
intent.addCategory("com.bl.blandroidpro.MY_CATEGORY");
startActivity(intent);
}
});
// 打開 網(wǎng)頁
Button btn_intent_web = findViewById(R.id.btn_intent_web);
btn_intent_web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
});
// 打電話
Button btn_intent_tel = findViewById(R.id.btn_intent_tel);
btn_intent_tel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
});
// Activity 之間 使用 Intent 傳遞數(shù)據(jù)
Button btn_intent_transmit_data = findViewById(R.id.btn_intent_transmit_data);
btn_intent_transmit_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(IntentActivity.this, IntentTransmitDataActivity.class);
intent.putExtra("transmit_data_1", "Hello");
intent.putExtra("transmit_data_2", new String[]{"World!"});
startActivityForResult(intent, 1);
}
});
// 顯示結(jié)果
tv_intent_res = findViewById(R.id.tv_intent_res);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String[] res = data.getStringArrayExtra("feedback_data");
tv_intent_res.setText(res[0] + res[1]);
}
break;
default:
}
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
TextView tv_transmit_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String data_1 = intent.getStringExtra("transmit_data_1");
String[] data_2 = intent.getStringArrayExtra("transmit_data_2");
tv_transmit_data = findViewById(R.id.tv_transmit_data);
tv_transmit_data.setText(data_1 + " " + data_2[0]);
Button btn_feedback = findViewById(R.id.btn_feedback);
btn_feedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("feedback_data", new String[]{"Yes, I", " konw!"});
setResult(RESULT_OK, intent);
finish();
}
});
}
/**
* 點(diǎn)擊返回按鈕時(shí) 反饋數(shù)據(jù)回去
*/
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("feedback_data", new String[]{"Yes, I", " konw!"});
setResult(RESULT_OK, intent);
finish();
}
}