
- 作 用
- 解决Android应用各项组件的通讯
- 提供信息
- 组件互相调用的相关信息
- 实现目的
- 调用者与被调用者之间的解耦
- 中文名
- 意图
- 性 质
- 媒体中介
- 外文名
- intent
-
2019-01-20 00:18:05
1.Intent的作用?都有哪些属性?
Intent是应用程序种各个组件联系的桥梁,通信的载体,负责应用程序中数据的传递(运输大队长)
- 启动一个Acitivity:
Context.this.startActivity(intent);
- 启动一个Sercvie
Context.this.startService(intent);
- 停止一个Service
Context.this.stopService(intent);
- 注册一个广播接收器
Context.this.registerReceiver(receiver, filter);
- 注销一个广播接收器
Context.this.unregisterReceiver(receiver);
- 发送一个广播
Context.this.sendBroadcast(intent);
- 1.1 Component:要请求的目标组件
- 1.2 Action:被请求的组件所要完成的动作
- 1.3 Category:为Action增加额外的附加类别信息,通常和Action配合使用
- 1.4 Data:为Action提供所要处理的数据,通常是一个Uri对象【schema、host、port、path】
- 1.5 Extras:要携带的数据
- 1.6 Flags:设置Activity的启动模式(四种启动模式)
2.Intent的分类
显示的Intent:明确指定要启动的组件,
隐式的Intent:通过IntentFilter来指定要启动的组件
一般在同一个应用中使用显示的Intent,如果跨应用则使用隐式的Intent
如果需要隐式意图启动一个Activity则必须配置CATEGORY_DEFAULT
Android5.0以后不能使用隐式的Intent来启动一个Service,也就是Service组件不能配置IntentFilter
如果通过隐式意图启动的组件不存在,应用将崩溃if(Intent.resolveActivity(getPackageManager())!=null){ startActivity(sendIntent); }
如果每次启动都希望选择则:
Intent sendIntent = new Intent(Intent.ACTION_SEND); String title = getResources().getString(R.string.chooser_title); Intent chooser = Intent.createChooser(sendIntent,title); if(sendIntent.resolveActivity(getPackageManager()) != null){ startActivity(chooser); }
3.Pending Intent作用?主要使用场合有哪些?
主要用于Intent的延时启动,使用场合有
3.1 包装一个Notification的Intent
3.2 包装一个App Widger的Intent(按Home键启动的Activity)
3.3 包装一个延时启动的Activity(AlarmManager)
通过PendingIntent.getActivity()启动一个activity;
通过PendingIntent.getService()启动一个Service;
通过PendingIntent.getBroadcast()启动一个BroadcastReceiver;4.常用的隐式Intent都有哪些?
Intent的真正强大之处在于它的隐式Intent,隐式Intent需要配合Intent-filters使用。隐式Intent足够强大,以至于系统提供了大量的Intent方便开发者启动系统应用程序
- 4.1 应用程序设置
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, APP_SETTING);
- 4.2 GPS设置
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, GPS_SETTING);
- 4.3 APP安装
Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.setDataAndType(Uri.fromFile(newFile("/mnt/sdcard/mobilesafe72_2.apk")),"application/vnd.android.package-archive"); startActivity(intent);
<intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="content"/>// content从内容提供者中获取数据 <data android:scheme="file"/>// file:从文件中获取数据 <data android:mimeType="application/vnd.android.package-archive"/> </intent-filter>
- 4.4 打电话
Intent intent=new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:13621310260"));
<activity android:name="OutgoingCallBroadcaster" android:configChanges="orientation|keyboardHidden" android:permission="android.permission.CALL_PHONE" android:theme="@android:style/Theme.NoDisplay"> <intent-filter> <action android:name="android.intent.action.CALL"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="tel"/> </intent-filter> <intent-filter android:icon="@drawable/ic_launcher_sip_call"> <action android:name="android.intent.action.CALL"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="sip"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.CALL"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="voicemail"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.CALL"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="vnd.android.cursor.item/phone"/> <data android:mimeType="vnd.android.cursor.item/phone_v2"/> <data android:mimeType="vnd.android.cursor.item/person"/> </intent-filter> </activity>
- 4.5 发短信
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.putExtra("sms_body",content); intent.setType("vnd.android-dir/mms-sms"); Intent intent=new Intent(); intent.setAction(Intent.ACTION_SENDTO); intent.setData(Uri.parse("smsto://0800000132")); intent.putExtra("sms_body","The SMM text");
<activity android:name=".ui.ComposeMessageActivity" android:configChanges="orientation|keyboardHidden" android:launchMode="singleTop" android:windowSoftInputMode="stateHidden"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="vnd.android-dir/mms-sms"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW"/> <action android:name="android.intent.action.SENDTO"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="sms"/> <data android:scheme="smsto"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW"/> <action android:name="android.intent.action.SENDTO"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="mms"/> <data android:scheme="mmsto"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="image/*"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="video/*"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND_MULTIPLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="*/*"/> </intent-filter> </activity>
- 4.6 拍照
Intent intent = new Intent();//intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(activity, "com.zhijiexing.travel.fileprovider", file)); activity.startActivityForResult(intent, requestcode);
- AndroidMinifest.xml文件下:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.zhijiexing.travel.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider>
- provider_paths.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <paths> <external-path name="download" path=""/> <external-path name="Download" path=""/> <external-path name="external_files" path="."/> </paths> </resources>
5. intent.setFlags()方法中的参数值详解?
- 5.1 FLAG_ACTIVITY_CLEAR_TOP
例如现在的栈情况为:A B C D 。D此时通过intent跳转到B,如果这个intent添加FLAG_ACTIVITY_CLEAR_TOP标记,则栈情况变为:A B。如果没有添加这个标记,则栈情况将会变成:A B C D B。也就是说,如果添加了FLAG_ACTIVITY_CLEAR_TOP标记,并且目标Activity在栈中已经存在,则将会把位于该目标activity之上的activity从栈中弹出销毁。这跟上面把B的Launch mode设置成singleTask类似。简而言之,跳转到的activity若已在栈中存在,则将其上的activity都销掉。
- 5.2 FLAG_ACTIVITY_NEW_TASK
例如现在栈1的情况是:A B C。C通过intent跳转到D,并且这个intent添加了FLAG_ACTIVITY_NEW_TASK标记,如果D这个Activity在Manifest.xml中的声明中添加了Task affinity,系统首先会查找有没有和D的Task affinity相同的task栈存在,如果有存在,将D压入那个栈,如果不存在则会新建一个D的affinity的栈将其压入。如果D的Task affinity默认没有设置,则会把其压入栈1,变成:A B C D,这样就和不加FLAG_ACTIVITY_NEW_TASK标记效果是一样的了。注意如果试图从非activity的非正常途径启动一个activity(例见下文“intent.setFlags()方法中参数的用例”),比如从一个service中启动一个activity,则intent比如要添加FLAG_ACTIVITY_NEW_TASK标记(编者按:activity要存在于activity的栈中,而非activity的途径启动activity时必然不存在一个activity的栈,所以要新起一个栈装入启动的activity)。简而言之,跳转到的activity根据情况,可能压在一个新建的栈中。
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);- 5.3 FLAG_ACTIVITY_NO_HISTORY
例如现在栈情况为:A B C。C通过intent跳转到D,这个intent添加FLAG_ACTIVITY_NO_HISTORY标志,则此时界面显示D的内容,但是它并不会压入栈中。如果按返回键,返回到C,栈的情况还是:A B C。如果此时D中又跳转到E,栈的情况变为:A B C E,此时按返回键会回到C,因为D根本就没有被压入栈中。简而言之,跳转到的activity不压在栈中。
- 5.4 FLAG_ACTIVITY_SINGLE_TOP
和Activity的Launch mode的singleTop类似。如果某个intent添加了这个标志,并且这个intent的目标activity就是栈顶的activity,那么将不会新建一个实例压入栈中。简而言之,目标activity已在栈顶则跳转过去,不在栈顶则在栈顶新建activity。
- 5.5 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
例如现在栈情况为:A B C,现在要再次启动B,并且设置为该模式,则栈的变化为:A C B
如果这个activity已经启动了,就不产生新的activity,而只是把这个activity实例加到栈顶来就可以了
更多相关内容 - 启动一个Acitivity:
-
常用Intent合集 Android
2021-03-24 10:32:34Android常用Intent集合 //1.拨打电话 // 给移动客服10086拨打电话 Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_DIAL, uri); startActivity(intent);常用Intent集合
//1.拨打电话
给移动客服10086拨打电话
Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_DIAL, uri); startActivity(intent);
2.发送短信
// 给10086发送内容为“Hello”的短信
Uri uri = Uri.parse("smsto:10086"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", "Hello"); startActivity(intent);
3.发送彩信(相当于发送带附件的短信)
Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra("sms_body", "Hello"); Uri uri = Uri.parse("content://media/external/images/media/23"); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType("image/png"); startActivity(intent);
4.打开浏览器:
// 打开百度主页
Uri uri = Uri.parse("http://www.baidu.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
5.发送电子邮件:(阉割了Google服务的没戏!!!)
// 给someone@domain.com发邮件 Uri uri = Uri.parse("mailto:someone@domain.com"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); startActivity(intent); // 给someone@domain.com发邮件发送内容为“Hello”的邮件 Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_EMAIL, "someone@domain.com"); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); intent.putExtra(Intent.EXTRA_TEXT, "Hello"); intent.setType("text/plain"); startActivity(intent); // 给多人发邮件 Intent intent=new Intent(Intent.ACTION_SEND); String[] tos = {"1@abc.com", "2@abc.com"}; // 收件人 String[] ccs = {"3@abc.com", "4@abc.com"}; // 抄送 String[] bccs = {"5@abc.com", "6@abc.com"}; // 密送 intent.putExtra(Intent.EXTRA_EMAIL, tos); intent.putExtra(Intent.EXTRA_CC, ccs); intent.putExtra(Intent.EXTRA_BCC, bccs); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); intent.putExtra(Intent.EXTRA_TEXT, "Hello"); intent.setType("message/rfc822"); startActivity(intent);
6.显示地图:
// 打开Google地图中国北京位置(北纬39.9,东经116.3) Uri uri = Uri.parse("geo:39.9,116.3"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
7.路径规划
// 路径规划:从北京某地(北纬39.9,东经116.3)到上海某地(北纬31.2,东经121.4) Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
8.多媒体播放:
Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///sdcard/foo.mp3"); intent.setDataAndType(uri, "audio/mp3"); startActivity(intent); //获取SD卡下所有音频文件,然后播放第一首=-= Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
9.打开摄像头拍照:
// 打开拍照程序 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); // 取出照片数据 Bundle extras = intent.getExtras(); Bitmap bitmap = (Bitmap) extras.get("data"); //另一种: //调用系统相机应用程序,并存储拍下来的照片 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); time = Calendar.getInstance().getTimeInMillis(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory().getAbsolutePath()+"/tucue", time + ".jpg"))); startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE);
10.获取并剪切图片
// 获取并剪切图片 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.putExtra("crop", "true"); // 开启剪切 intent.putExtra("aspectX", 1); // 剪切的宽高比为1:2 intent.putExtra("aspectY", 2); intent.putExtra("outputX", 20); // 保存图片的宽和高 intent.putExtra("outputY", 40); intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路径 intent.putExtra("outputFormat", "JPEG");// 返回格式 startActivityForResult(intent, 0); // 剪切特定图片 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setClassName("com.android.camera", "com.android.camera.CropImage"); intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp"))); intent.putExtra("outputX", 1); // 剪切的宽高比为1:2 intent.putExtra("outputY", 2); intent.putExtra("aspectX", 20); // 保存图片的宽和高 intent.putExtra("aspectY", 40); intent.putExtra("scale", true); intent.putExtra("noFaceDetection", true); intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp")); startActivityForResult(intent, 0);
11.打开Google Market
// 打开Google Market直接进入该程序的详细页面 Uri uri = Uri.parse("market://details?id=" + "com.demo.app"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
12.进入手机设置界面:
// 进入无线网络设置界面(其它可以举一反三) Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); startActivityForResult(intent, 0);
13.安装apk:
Uri installUri = Uri.fromParts("package", "xxx", null); returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
14.卸载apk:
Uri uri = Uri.fromParts("package", strPackageName, null); Intent it = new Intent(Intent.ACTION_DELETE, uri); startActivity(it);
15.发送附件:
Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3"); sendIntent.setType("audio/mp3"); startActivity(Intent.createChooser(it, "Choose Email Client"));
16.进入联系人页面:
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(People.CONTENT_URI); startActivity(intent);
17.查看指定联系人:
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id联系人ID Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(personUri); startActivity(intent);
18.调用系统编辑添加联系人(高版本SDK有效):
Intent it = newIntent(Intent.ACTION_INSERT_OR_EDIT); it.setType("vnd.android.cursor.item/contact"); //it.setType(Contacts.CONTENT_ITEM_TYPE); it.putExtra("name","myName"); it.putExtra(android.provider.Contacts.Intents.Insert.COMPANY, "organization"); it.putExtra(android.provider.Contacts.Intents.Insert.EMAIL,"email"); it.putExtra(android.provider.Contacts.Intents.Insert.PHONE,"homePhone"); it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE,"mobilePhone"); it.putExtra( android.provider.Contacts.Intents.Insert.TERTIARY_PHONE,"workPhone"); it.putExtra(android.provider.Contacts.Intents.Insert.JOB_TITLE,"title"); startActivity(it);
19.调用系统编辑添加联系人(全有效):
Intent intent = newIntent(Intent.ACTION_INSERT_OR_EDIT); intent.setType(People.CONTENT_ITEM_TYPE); intent.putExtra(Contacts.Intents.Insert.NAME, "My Name"); intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890"); intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,Contacts.PhonesColumns.TYPE_MOBILE); intent.putExtra(Contacts.Intents.Insert.EMAIL, "com@com.com"); intent.putExtra(Contacts.Intents.Insert.EMAIL_TYPE, Contacts.ContactMethodsColumns.TYPE_WORK); startActivity(intent);
20.打开另一程序
Intent i = new Intent(); ComponentName cn = new ComponentName("com.example.jay.test", "com.example.jay.test.MainActivity"); i.setComponent(cn); i.setAction("android.intent.action.MAIN"); startActivityForResult(i, RESULT_OK);
21.打开录音机
Intent mi = new Intent(Media.RECORD_SOUND_ACTION); startActivity(mi);
22.从google搜索内容
Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY,"searchString") startActivity(intent);
-
安卓开发之Intent使用介绍(显式Intent和隐式Intent)
2020-11-19 23:09:22Intent Intent是 Android程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据。Intent一般可被用于启动活动、启动服务以及发送广播等场景,这次我们运用...Android
01: Android Studio目录结构介绍, 安卓开发入门
02: Android中的日志工具类Log详细介绍
03: 添加Button元素, 并且在活动中使用Toast和Menu
04: 安卓开发之Intent使用介绍(显式Intent和隐式Intent)
05: 安卓广播机制讲解(标准广播和有序广播)
06: 安卓广播实现强制下线功能(Kotlin语言实现)
1. 显示Intent
Intent是 Android程序中各组件之间进行交互的一种重要方式, 它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据。 Intent一般可被用于启动活动、启动服务以及发送广播等场景, 这次我们运用在启动活动上面。 Intent大致可以分为两种:显式Intent和隐式Intent, 我们先来看一下显式Intent如何使用。 首先我创建了2个活动:
1.FirstActivity(第一个活动)
package activitytest.example.administrator.activitytest; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class FirstActivity extends AppCompatActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add_item: Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show(); break; case R.id.remove_item: Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show(); break; default: } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main,menu); return true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); Button button1 = (Button) findViewById(R.id.button_1); button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this,SecondActivity.class); startActivity(intent); } }); } }
第一个活动的配置文件(first_layout.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" android:orientation="vertical"> <Button android:id="@+id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout>
2.SecondActivity(第二个活动)
package activitytest.example.administrator.activitytest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_layout); } }
第二个活动的配置文件(second_layout.xml):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/second_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="activitytest.example.administrator.activitytest.SecondActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 2"/> </LinearLayout> </RelativeLayout>
总的配置文件(AndroidManifest.xml):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="activitytest.example.administrator.activitytest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".FirstActivity" android:label="This is FirstActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity"></activity> </application> </manifest>
Intent有多个构造函数的重载, 其中一个是Intent(Context packageContext,Class<?>cls)。 这个构造函数接收两个参数,第一个参数Context要求提供一个启动活动的上下文, 第二个参数class 则是指定想要启动的目标活动, 通过这个构造函数就可以构建出Intent的”意图”。 然后我们应该怎么使用这个Intent呢? Activity 类中提供了一个startActivity()方法, 这个方法是专门用于启动活动的,它接收一个Intent参数, 这里我们将构建好的Intent传入startActivity()方法就可以启动目标活动了。 FirstActivity 中按钮的点击事件的代码如下所示:
@Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this,SecondActivity.class); startActivity(intent); }
我们首先构建出了一个Intent,传入 FirstActivity.this 作为上下文, 传入SecondActivity.class 作为目标活动,这样我们的“意图”就非常明显了, 即在FirstActivity这个活动的基础上打开SecondActivity这个活动。 然后通过startActivity()方法来执行这个Intent.
运行结果:
这时候已经切换到了第二个应用.
可以看到,我们已经成功启动SecondActivity这个活动了。 如果你想要回到上一个活动怎么办呢?很简单,按下Back键就可以销毁当前活动, 从而回到上一个活动了。 使用这种方式来启动活动,Intent的“意图”非常明显, 因此我们称之为: 显式 Intent。
2. 隐式Intent
相比于显式Intent,隐式Intent则含蓄了许多, 它并不明确指出我们想要启动哪一个活动, 而是指定了一系列更为抽象的action和 category 等信息, 然后交由系统去分析这个Intent,并帮我们找出合适的活动去启动。 什么叫作合适的活动呢?简单来说就是可以响应我们这个隐式Intent的活动, 那么目前SecondActivity可以响应什么样的隐式Intent呢? 额,现在好像还什么都响应不了,不过很快就会有了。 通过在<activity>标签下配置<intent-filter>的内容, 可以指定当前活动能够响应的action和 category, 打开AndroidManifest.xml,添加如下代码:
<activity android:name=".SecondActivity"> <intent-filter> <action android:name="activitytest.example.administrator.activitytest.ACTION_START"></action> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
!这里的activitytest.example.administrator.activitytest是活动所在的地址
在<action>标签中我们指明了当前活动可以响应activitytest.example.administrator.activitytest.ACTION_START这个action, 而<category>标签则包含了一些附加信息, 更精确地指明了当前的活动能够响应的Intent中还可能带有的category。 只有<action>和<category>中的内容同时能够匹配上 Intent中指定的action和 category时,这个活动才能响应该Intent。 修改FirstActivity 中按钮的点击事件,代码如下所示:
@Override public void onClick(View v) { Intent intent = new Intent("activitytest.example.administrator.activitytest.ACTION_START"); startActivity(intent); }
可以看到,我们使用了Intent 的另一个构造函数,直接将action的字符串传了进去, 表明我们想要启动能够响应 activitytest.example.administrator.activitytest.ACTION_START这个action的活动。 那前面不是说要<action>和<category>同时匹配上才能响应的吗? 怎么没看到哪里有指定category呢? 这是因为android.intent.category.DEFAULT是一种默认的category, 在调用startActivity()方法的时候会自动将这个category添加到 Intent中。 重新运行程序,在FirstActivity 的界面点击一下按钮, 你同样成功启动SecondActivity了。不同的是, 这次你是使用了隐式Intent 的方式来启动的, 说明我们在<activity>标签下配置的action和 category 的内容已经生效了!
运行结果:
每个Intent中只能指定一个action,但却能指定多个category。 目前我们的Intent中只有一个默认的category,那么现在再来增加一个吧。 修改FirstActivity中按钮的点击事件,代码如下所示:
public void onClick(View v) { Intent intent = new Intent("activitytest.example.administrator.activitytest.ACTION_START"); intent.addCategory("activitytest.example.administrator.activitytest.MY_CATEGORY"); startActivity(intent); } });
现在我们在AndroidManifest.xml的<intent-filter>中再添加一个category的声明,如下所示:
<activity android:name=".SecondActivity"> <intent-filter> <action android:name="activitytest.example.administrator.activitytest.ACTION_START"></action> <category android:name="android.intent.category.DEFAULT" /> <category android:name="activitytest.example.administrator.activitytest.MY_CATEGORY"/> </intent-filter> </activity>
再次重新运行程序,你就会发现一切都正常了。
-
Android页面跳转(Intent)
2021-10-12 15:06:50Android 意图的使用(Intent)显式四种跳转方式一二三四布局+代码效果隐式意图和隐式意图的跳转Intent概述Action属性 显式四种跳转方式 一 Intent intent = new Intent(MainActivity.this,HomeActivity.class); ...Android 意图的使用(Intent)
显式四种跳转方式
一
Intent intent = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent);
二
Intent intent = new Intent(); intent.setClass(MainActivity.this,HomeActivity.class); startActivity(intent);
三
Intent intent = new Intent(); ComponentName componentName = new ComponentName(MainActivity.this,HomeActivity.class); intent.setComponent(componentName); startActivity(intent);
四
startActivity(new Intent(MainActivity.this,HomeActivity.class));
布局+代码
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <Button android:id="@+id/bt_intentmain" android:layout_width="wrap_content" android:layout_height="40dp" android:text="跳过去" /> <Button android:id="@+id/bt_intentOne" android:layout_width="wrap_content" android:layout_height="40dp" android:text="按钮一" /> <Button android:id="@+id/bt_intentTwo" android:layout_width="wrap_content" android:layout_height="40dp" android:text="按钮二" /> <Button android:id="@+id/bt_intentThree" android:layout_width="wrap_content" android:layout_height="40dp" android:text="按钮三" /> </LinearLayout>
代码
import androidx.appcompat.app.AppCompatActivity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mBtIntent,mBtIntentOne,mBtIntentTwo,mBtIntentThree; @Override protected void onCreate(Bundle savedInstanceState) { Log.d("TAG","onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); mBtIntent.setOnClickListener(this); mBtIntentOne.setOnClickListener(this); mBtIntentTwo.setOnClickListener(this); mBtIntentThree.setOnClickListener(this); setTitle("页面A"); } private void initView(){ mBtIntent = findViewById(R.id.bt_intentmain); mBtIntentOne = findViewById(R.id.bt_intentOne); mBtIntentTwo = findViewById(R.id.bt_intentTwo); mBtIntentThree = findViewById(R.id.bt_intentThree); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_intentmain: IntentOne(); break; case R.id.bt_intentOne: IntentTwo(); break; case R.id.bt_intentTwo: IntentThree(); break; case R.id.bt_intentThree: IntentFour(); break; default: break; } } private void IntentOne(){ Intent intent = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent); } private void IntentTwo(){ Intent intent = new Intent(); intent.setClass(MainActivity.this,HomeActivity.class); startActivity(intent); } private void IntentThree(){ Intent intent = new Intent(); ComponentName componentName = new ComponentName(MainActivity.this,HomeActivity.class); intent.setComponent(componentName); startActivity(intent); } private void IntentFour(){ startActivity(new Intent(MainActivity.this,HomeActivity.class)); } }
效果
隐式意图和隐式意图的跳转
隐式意图
没有明确指定组件名的Intent为隐式意图,系统会根据隐式意图中设置的动作(action)、类别(category)、数据UIL等来匹配最合适的组件。首先在清单文件中使用意图过滤器设置活动的名字
< action android:name=“HomeActivity” />
< category android:name=“android.intent.category.DEFAULT” /><activity android:name=".HomeActivity" android:launchMode="singleInstance" > <intent-filter> <action android:name="HomeActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
跳转
Intent intent = new Intent(); intent.setAction("HomeActivity"); startActivity(intent);
Intent概述
Intent是Android的核心组件,利用消息实现应用程序间的交互机制,这种消息描述了应用中一次操作的动作、数据以及附加数据,系统通过该Intent的描述负责找到对应的组件,并将Intent传递给调用的组件,完成组件的调用。
Intent由动作、数据、分类、类型、组件、扩展信息、标记等内容组成,每个组成都由相应的属性进行表示,并提供设置和获取相应属性的方法。
Action属性
• Action属性用于描述Intent要完成的动作,对要执行的动作进行一个简要描述。Intent类定义了一系列Action属性常量,用来标识一套标准动作,如ACTION_CALL(打电话)等。
• 通常与Data一般匹配使用
例如 短信发送
一、
Uri uri= Uri.parse(“tel:10086”);
Intent intent= new Intent(Intent.ACTION_CALL, uri);
//intent.setData(uri));//设置数据
startActivity(intent);
二、
Manifest里需要添加CALL_PHONE权限
三、
危险权限需要动态申请。Data属性
Action和Data一般匹配使用,不同的Action由不同的Data数据指定
Category属性
• Category属性指明一个执行Action的分类
• Intent中定义了一系列Category属性常量
按home键时启动自己做的应用
<intent-filter> <category android:name="android.intent.category.HOME"/> <category android:name="android.intent.category.DEFAULT"/> <action android:name="android.intent.action.MAIN"/> </intent-filter>
Component属性
一、Component属性用于指明Intent的目标组件的类名称。
二、通常Android会根据Intent中包含的其他属性的信息,比如Action、Data/Type、Category进行查找,最终找到一个与之匹配的目标组件。但是,如果指定了Component这个属性,Intent则会直接根据组件名查找到相应的组件,而不再执行上述查找过程。指定Component属性后,Intent的其他属性都是可选的。
三、根据Intent寻找目标组件时所采用的方式不同,可以将Intent分为两类:
显式Intent,这种方式通过直接指定组件名称Component来实现;Intent intent = new Intent(); ComponentName name = new ComponentName(IntentActivity.this,MainActivity.class); intent.setComponent(name); startActivity(intent);
**隐式Intent,**这种方式通过Intent Filter过滤实现,过滤时通常根据Action、Data和Category属性进行匹配查找。
Intent intent = new Intent(); intent.setClassName(IntentActivity.this,"com.ugrow.day02.MainActivity");
显式Intent通过**setComponent()、setClassName()或setClass()**设置组件名
Intent intent = new Intent(); intent.setClass(IntentActivity.this,MainActivity.class);
Extra属性(重点)
通过使用Intent对象的putExtra()方法来添加附加信息、和信息传递
信息添加 方式类似于键值对Intent intent= new Intent(); intent.putExtra("name","zhangsan");
信息取出 另一个页面
通过使用Intent对象的getXXXExtra()方法可以获取附加信息。
例如,将上面代码存入Intent对象中的人名获取出来,
因存入的是字符串,所以可以使用getStringExtra()方法获取数据Intent intent = getIntent(); String name=intent.getStringExtra("name");
Bundle
打包 当一个页面的数据会向多个页面传递的时候并且不是每个页面都会用到这些数据
比如A页面传到B页面 B页面不需要用数据 但需要将数据传递给C页面
进行多次使用 如果数据过多那么将会导致每一个页面都有大量的get方法 页面不整洁
因此使用Bundle打包
存Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("user",user.getText().toString()); bundle.putString("pwd",pwd.getText().toString()); intent.putExtras(bundle);
取
Bundle extras = getIntent().getExtras(); String user = extras.getString("user"); String pwd = extras.getString("pwd");
传递序列化对象
Javabean对象序列化后可以直接使用Extra方法传递对象
一、Javabean对象需要实现Serializable接口
CommentBean implements Serializable
二、传数据
intent.putExtra("bean",commentBean);
三、取数据
CommentBean commentBean = new CommentBean(); Intent intent=getIntent(); commentBean = (CommentBean) intent.getSerializableExtra("bean");
Type属性
Intent intent = new Intent(); Uri uri = Uri.parse("file:///data/vivo.mp4"); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(uri,"video/*"); startActivity(intent);
Flag属性
Flag属性用来设定Activity的启动模式,与清单文件中的设置launchMode属性值相同
Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent.FLAG_ACTIVITY_CLEAR_TOP = singleTask
Intent.FLAG_ACTIVITY_SINGLE_TOP = singleTop
Intent.FLAG_ACTIVITY_NEW_TASK = singleInstance返回值
一、A页面传值给B页面
startActivity() (putExtra,Bundle)
二、B页面传值给A页面
startActivityForRestult()
setResult(resultCode,Intent)
onActivityResultA页面跳转B页面 如果转账成功则返回过江 失败则返回有鬼
页面A
Intent intent = new Intent(MainActivity.this,HomeActivity.class); intent.putExtra("money","转账100元"); //有返回值的跳转 /* * 第一个参数Intent对象 * 第二个参数 RequestCode */ startActivityForResult(intent,REQUSET_CODE);
@Override //第一个参数 是不是我要的返回结果 第二个参数 是谁返回给我的 第三个参数 返回的附加信息 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUSET_CODE && resultCode == HomeActivity.RESULT_CODE){ String msg = data.getStringExtra("msg"); Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show(); } }
页面B
Intent intent = new Intent(); Intent getIn = getIntent(); String money = getIn.getStringExtra("money"); if(TextUtils.isEmpty(money)){ intent.putExtra("msg","有鬼!"); }else{ intent.putExtra("msg","过江!"); } setResult(RESULT_CODE,intent); //关闭页面 finish();
-
Intent及其七大属性及intent-filter设置
2019-06-12 17:53:35一、知识点回顾:Activity (一)、如何实现Activity页面...Intent intent = new Intent(MainActivity.this,NextActivity.class); startActivity(intent); //第二种方式: Intent intent = new Intent(); intent.... -
【android编程】第八讲-Intent和BroadcastReceiver
2020-03-18 17:52:16第八讲Intent和BroadcastReceiver 本讲介绍了Android应用程序中最为重要Intent和四大组件之一 文章目录第八讲Intent和BroadcastReceiverIntentIntent对象构成Component nameActionDataCategoryExtrasFlagsURI和... -
Intent 和 Intent 过滤器
2017-12-04 14:56:17Intent 和 Intent 过滤器 本文内容 Intent 类型构建 Intent 显式 Intent 示例隐式 Intent 示例强制使用应用选择器 接收隐式 Intent 过滤器示例 使用待定 IntentIntent 解析 操作测试类别... -
kotlin使用intent传值
2022-04-20 09:37:30val intent = Intent(this,VideoPlayer().javaClass) //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) var bundle = Bundle() bundle.putString("path",list.get(i).absoluteFile.toString()) intent.putExtras... -
Android Intent详解
2018-12-28 11:19:05什么是Intent 翻译就是意图,就是你想做的事情,比如我想成为资深攻城狮,具体的就是在Activity中我想跳转到下一个Activity中,或者我想开启服务,我想发送广播,都是需要用到Intent(意图)的。 ... -
Intent详解及其用法
2019-03-30 14:55:23Intent 概述 翻译过来为“意图”,它是一种运行时绑定(run-time binding)机制,可以应用于两个应用间的通讯交互,也能够应用于在同一个应用下不同组件的交互(activity、service、broadcast receiver) 看下面... -
第四章:Android好东西之Intent
2022-03-14 18:46:02Intent 概述 •Intent是Android的核心组件,利用消息实现应用程序间的交互机制,这种消息描述了应用中一次操作的动作、数据以及附加数据,系统通过该Intent的描述负责找到对应的组件,并将Intent传递给调用的组件,... -
Android Intent Action 大全
2018-11-02 14:15:55分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴! ... 1.Intent的用法 -
android 中intent跳转是灰色的,没有效果,显示intent = null
2022-01-19 13:20:281.没有设置startActivity(intent) startActivity(intent); 这一行代码应该放在整个功能结束前的一行,如下: //登录验证代码 @Override public void onClick(View v) { Intent intent = null; switch (v.getId... -
kotlin应用内简单构建intent
2022-04-20 09:49:43val intent = Intent(this,VideoPlayer::class.java) 或者: val intent = Intent(this,VideoPlayer().javaClass) 以前java代码是这样的: Intent intent = new Intent(this,VideoPlayer.class); -
Intent的基本使用
2018-06-19 05:54:54Intent(意图),Android通信的桥梁,比如我们可以通过: startActivity(Intent)/startActivityForResult(Intent):来启动一个Activity startService(Intent)/bindService(Intent):来启动一个Service ... -
Android系统应用跳转路径(通过Intent进行跳转)
2019-03-09 14:35:531、跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));...Intent intent = newIntent(Intent.ACTION_DI... -
记录:Intent、Intent过滤器和通用Intent
2017-03-13 23:11:30现在可以看中文的部分文档,可是内容真心不好记。看过之后就无名的又忘记了。还是摘抄一道的方式去加深记忆和当做一次笔记方便后面自己快速查找。记录一下,Intent,Intent的过滤和 常用的通用 IntentIntent . . . -
详解显式intent和隐式intent
2018-07-11 18:35:43Intent是Android程序中各组件之间进行交互的一种重要方式。它不仅可以指明activity想要执行的动作,也可以在不同组件之间传输数据。 Intent分为两种,显式intent和隐式intent,以下分别进行描述: 一、显式intent ... -
Android Intent的详细解析以及用法
2019-01-23 11:40:16Intent的详细解析以及用法 Android的四大组件分别为Activity 、Service、BroadcastReceiver(广播接收器)、ContentProvider(内容提供者),这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个... -
intent-filter属性介绍
2018-10-31 17:39:31intent-filter详细属性的介绍 <intent-filter/>是每一个Activity对应的过滤器标签节点。每一个过滤器里面的元素可以有: 0个或多个<action.../> 0个或多个<category.../>... -
intent跳转和转场动画
2022-04-22 13:47:50自测在activity1里的fragment1...Intent intent=new Intent(getActivity(), MainActivity2.class); startActivity(intent); getActivity().overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_t -
Intent.ACTION_VIEW无法跳转问题排查
2022-04-09 14:02:17项目中文本链接(包括网址和邮箱)点击以后使用的是Intent.ACTION_VIEW打开,但是有用户反馈在三星Galaxy Tab S7上点击以后无法跳转,于是做了问题的排查,... final Intent intent = new Intent(Intent.ACTION_VIEW, U -
Android Studio中Intent的使用方法3-1
2021-11-27 11:01:391 Intent简介 Android Studio中的Intent类可以翻译为“意图”,是对是要完成操作的抽象描述。Intent主要用在多个启动的Activity(活动)中间,用于活动之间传递数据。 2 Intent组成 Intent主要由action(动作)和... -
Intent.parseUri()详解
2019-04-16 11:26:18Intent.parseUri()详解 由于经常会有类似下面的控制启动界面代码: this.startActivity(Uri.parse(url)); this.startActivity(Intent.parseUri(url)); 注意两者区别 第一种写法只能使用类似 http(s)/file等 协议开头... -
Intent 简介与详解
2017-12-30 23:31:181.Intent作用Intent是一个将要执行的动作的抽象的描述,解决Android应用的各项组件之间的通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的... -
Android组件(二)Intent
2022-03-29 11:15:33Android的三个基本组件——Activity,Service和Broadcast Receiver——都是通过Intent机制激活的,不同类型的组件有不同的传递Intent方式 -
Intent跳转时使用intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)闪屏...
2022-03-09 16:48:29Intent跳转时使用intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)闪屏问题 在values中的styles中添加 <style name="App_tiaozhuan" parent="AppTheme"> <item name=... -
Intent的基本知识
2018-09-27 12:31:41我们知道,Intent 是一个消息传递对象,使用它可以向其他Android组件请求操作。Intent的基本用途主要包括:启动 Activity、启动服务、传递广播。Intent分为显式Intent和隐式Intent。下面我通过启动Activity来讲解... -
intent的四种启动模式和七大属性
2019-02-12 09:59:42一、Activity (一)、如何实现...Intent intent = new Intent(MainActivity.this,NextActivity.class); startActivity(intent); //第二种方式: Intent intent = new Intent(); intent.setClass(MainActi...