显示Intent与隐式Intent
1.***显示Intent:***通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的。
一般情况下,一个Android应用程序中需要多个屏幕,即是多个Activity类,并且在这些Activity之间进行切换通过Intent机制来实现的。在同一个应用程序中切换Activity时,我们通常都知道要启动的Activity具体是哪一个,因此常用显式的Intent来实现的。
//实现支付宝扫一扫
Button bton2 = findViewById(R.id.button2);
bton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//利用显示Intent打开支付宝
Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} catch (Exception e) {
//若无法正常跳转,在此进行错误处理
Toast.makeText(FirstActivity.this,"打开失败,请检查是否安装了支付宝", Toast.LENGTH_SHORT).show();
}
}
});
布局layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
app:layout_marginLeftPercent="3%"
app:layout_marginTopPercent="20%"
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_widthPercent="30%"
android:text="1\n微信\n扫一扫"
android:layout_below="@+id/clock2"
android:textSize="25dp"
/>
<Button
app:layout_marginLeftPercent="33%"
app:layout_marginTopPercent="20%"
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2\n支付宝\n扫一扫"
android:textSize="25dp"
android:layout_toRightOf="@+id/button1"
android:layout_below="@+id/clock2"
app:layout_widthPercent="30%" />
</androidx.percentlayout.widget.PercentFrameLayout>
2.***隐式Intent:***通过Intent Filter来实现的,它一般用在没有明确指出目标组件名称的前提下,一般是用于在不同应用程序之间。
隐式,即不是像显式的那样直接指定需要调用的Activity,隐式不明确指定启动哪个Activity,而是设置Action、Data、Category,让系统来筛选出合适的Activity。
Button bton0 = findViewById(R.id.button0);
picture = findViewById(R.id.picture);
bton0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initPhotoError();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
creatDir();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File
(Environment.getExternalStorageDirectory().getAbsolutePath()+"download/drt", "android"+String.valueOf(timeStamp)+".jpg")));
//存放位置为sdcard卡上download/drt文件夹,文件名android+时间戳.jpg为格式
Log.v("det","android"+String.valueOf(timeStamp)+".jpg");
startActivityForResult(intent, 0);
}
});
布局layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/button8"
android:layout_toRightOf="@id/button10"
android:text="0\n待定\n功能"
android:textSize="25dp"
app:layout_marginLeftPercent="33%"
app:layout_marginTopPercent="62%"
app:layout_widthPercent="30%" />
</androidx.percentlayout.widget.PercentFrameLayout>
对于显示Intent,Android不需要再去做解析,因为目标组件很明确。Android需要解析的是隐式Intent,通过解析,将Intent映射给可以处理该Intent的Activity,Service等。Intent的解析机制主要是通过查找已经注册在AndroidManifest.xml中的所有IntentFilter以及其中定义的Intent,最终找到匹配的Intent。
本文介绍了Android中Intent的两种主要类型:显示Intent和隐式Intent。显示Intent通常用于在同一应用内启动已知的Activity,例如在示例中通过Intent启动支付宝。而隐式Intent则不指定具体组件,依赖IntentFilter匹配,常用于跨应用交互,如使用ACTION_IMAGE_CAPTURE捕获图片。在处理隐式Intent时,Android系统会解析Intent并找到合适的组件来响应。
2253

被折叠的 条评论
为什么被折叠?



