http://developer.android.com/guide/components/intents-common.html
Intent允许你通过一个你想要执行的行为动作来启动一个其他app的Activity。这种启动方式叫做隐式意图以为你并没有指定一个组件去启动,而是指定要执行的具体动作并且提供一些数据。
当你使用startActivity() 或 startActivityForResult()并传入一个隐式意图,系统会决定这个intent去往那个app,并开启对应的activity。如果有大于一个app可以处理,就会弹出一个对话框来个你选择需要哪个app来处理。如果没有app可以处理程序会崩溃,可以resolveActivity()来判断是否有app可以处理,返回的不是空则有app可以处理。
接下来介绍一些常见的隐式intent。
Alarm Clock
- 创建一个 alarm
- Action
ACTION_SET_ALARM - Data URI
none - MIME Type
none - Extras
EXTRA_HOUR:闹钟的小时。
EXTRA_MINUTES:闹钟的分钟。
EXTRA_MESSAGE:闹钟的信息。
EXTRA_DAYS:一个ArrayList包含该重复的星期,必须声明为一个来自Calendar类中的整数如MONDAY,如果为单词闹钟,不要这个Extra。
EXTRA_RINGTONE:指定闹钟的铃声,为一个 content:的URI。不声明使用默认铃声。
EXTRA_VIBRATE:是否震动,布尔值。
EXTRA_SKIP_UI:是否跳过闹钟设置UI界面。布尔值。
- Action
public void createAlarm(String message, int hour, int minutes) { Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM) .putExtra(AlarmClock.EXTRA_MESSAGE, message) .putExtra(AlarmClock.EXTRA_HOUR, hour) .putExtra(AlarmClock.EXTRA_MINUTES, minutes); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
intent filter:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> <activity ...> <intent-filter> <action android:name="android.intent.action.SET_ALARM" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 创建一个计时器
- Action
ACTION_SET_TIMER - Data URI
none - MIME Type
none - Extras
EXTRA_LENGTH:倒计时的秒数。
EXTRA_MESSAGE:一个自定义消息。
EXTRA_SKIP_UI:是否跳过闹钟设置UI界面。如果是true值简单的启动计时器。
- Action
public void startTimer(String message, int seconds) { Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER) .putExtra(AlarmClock.EXTRA_MESSAGE, message) .putExtra(AlarmClock.EXTRA_LENGTH, seconds) .putExtra(AlarmClock.EXTRA_SKIP_UI, true); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
intent filter:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> <activity ...> <intent-filter> <action android:name="android.intent.action.SET_TIMER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 显示所有闹钟
- Action
ACTION_SHOW_ALARMS - Data URI
none - MIME Type
none
- Action
intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SHOW_ALARMS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 创建一个 alarm
Calendar
- 添加一个日历事件
- Action
ACTION_INSERT - Data URI
Events.CONTENT_URI - MIME Type
“vnd.android.cursor.dir/event” - Extras
EXTRA_EVENT_ALL_DAY:是否是一个全天事件,布尔值。
EXTRA_EVENT_BEGIN_TIME:事件开始时间,毫秒自从1970-01-01 00:00:00。
EXTRA_EVENT_END_TIME:事件结束时间(milliseconds since epoch)。
TITLE:事件标题。
DESCRIPTION:事件描述。
EVENT_LOCATION:事件坐标。
EXTRA_EMAIL:以逗号间隔表示被邀请者的电子邮件。
- Action
public void addEvent(String title, String location, Calendar begin, Calendar end) { Intent intent = new Intent(Intent.ACTION_INSERT) .setData(Events.CONTENT_URI) .putExtra(Events.TITLE, title) .putExtra(Events.EVENT_LOCATION, location) .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin) .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.INSERT" /> <data android:mimeType="vnd.android.cursor.dir/event" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 添加一个日历事件
Camera
- 得到一个图片或视频并返回
- Action
ACTION_IMAGE_CAPTURE
ACTION_VIDEO_CAPTURE - Data URI
None - MIME Type
None - Extras
EXTRA_OUTPUT:app要储存图片或视频的URI地址。传入Uri对象。
- Action
static final int REQUEST_IMAGE_CAPTURE = 1; static final Uri mLocationForPhotos; public void capturePhoto(String targetFilename) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.withAppendedPath(mLocationForPhotos, targetFilename); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bitmap thumbnail = data.getParcelable("data"); // 对被保存在mLocationForPhotos的原始图片做其他处理 ... } }
intent filter:
<activity ...> <intent-filter> <action android:name="android.media.action.IMAGE_CAPTURE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 使用静态图片模式、摄像模式开启相机app
- Action
INTENT_ACTION_STILL_IMAGE_CAMERA
INTENT_ACTION_VIDEO_CAMERA - Data URI
none - MIME Type
none - Extras
- none
- Action
public void capturePhoto() { Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent); } }
intent filter:
<activity ...> <intent-filter> <action android:name="android.media.action.STILL_IMAGE_CAMERA" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 得到一个图片或视频并返回
联系人 App
- 选择一个联系人
static final int REQUEST_SELECT_CONTACT = 1; public void selectContact() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_SELECT_CONTACT); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) { Uri contactUri = data.getData(); // Do something with the selected contact at contactUri ... } }
设置MIME Type
- CommonDataKinds.Phone.CONTENT_TYPE
显示号码的视图 - CommonDataKinds.Email.CONTENT_TYPE
显示电子邮件的视图 - CommonDataKinds.StructuredPostal.CONTENT_TYPE
显示地址的视图
static final int REQUEST_SELECT_PHONE_NUMBER = 1; public void selectContact() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(CommonDataKinds.Phone.CONTENT_TYPE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) { // 得到URI并在内容提供者中查询 Uri contactUri = data.getData(); String[] projection = new String[]{CommonDataKinds.Phone.NUMBER}; Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null); // 如果返回的游标有用,则得到电话号码 if (cursor != null && cursor.moveToFirst()) { int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER); String number = cursor.getString(numberIndex); // Do something with the phone number ... } } }
- 显示一个联系人
public void viewContact(Uri contactUri) { Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
编辑一个联系人
public void editContact(Uri contactUri, String email) { Intent intent = new Intent(Intent.ACTION_EDIT); intent.setData(contactUri); intent.putExtra(Intents.Insert.EMAIL, email); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
插入一个联系人
public void insertContact(String name, String email) { Intent intent = new Intent(Intent.ACTION_INSERT); intent.setType(Contacts.CONTENT_TYPE); intent.putExtra(Intents.Insert.NAME, name); intent.putExtra(Intents.Insert.EMAIL, email); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
File Storage
- 返回特定类型的文件
- Action
ACTION_GET_CONTENT - Data URI
None - MIME Type
MIME type为对应文件类型 - Extras
EXTRA_ALLOW_MULTIPLE:是否可以多选。
EXTRA_LOCAL_ONLY:返回的只能为本地文件。
- Action
static final int REQUEST_IMAGE_GET = 1; public void selectImage() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_IMAGE_GET); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) { Bitmap thumbnail = data.getParcelable("data"); Uri fullPhotoUri = data.getData(); // Do work with photo saved at fullPhotoUri ... } }
返回一张图片的intent filter
<activity ...> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <data android:type="image/*" /> <category android:name="android.intent.category.DEFAULT" /> <!-- CATEGORY_OPENABLE为返回可读的文件可以被openFileDescriptor()表现为文件流 --> <category android:name="android.intent.category.OPENABLE" /> </intent-filter> </activity>
- 返回特定类型的文件
Local Actions
- Maps
Music or Video
- 打开一个媒体文件
- Action
ACTION_VIEW - Data URI
file:
content:
http: - MIME Type
“audio/*”
“application/ogg”
“application/x-ogg”
“application/itunes”
或者其他需要的类型 - Extras
- Action
public void playMedia(Uri file) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(file); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.VIEW" /> <data android:type="audio/*" /> <data android:type="application/ogg" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 打开一个媒体文件
Phone
- 拨打电话
- Action
ACTION_DIAL:打开拨号界面。
ACTION_CALL:拨打一个电话 - Data URI
tel:< phone-number>
voicemail:< phone-number>
http: - MIME Type
None
- Action
public void dialPhoneNumber(String phoneNumber) { Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:" + phoneNumber)); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
- 拨打电话
Search
- 用特定的app查询
- Action
“com.google.android.gms.actions.SEARCH_ACTION”
支持从google查找 - Extras
QUERY:查找需要的搜索查询字符串
- Action
intent filter:
<activity android:name=".SearchActivity"> <intent-filter> <action android:name="com.google.android.gms.actions.SEARCH_ACTION"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
进行一个网络查询:
public void searchWeb(String query) { Intent intent = new Intent(Intent.ACTION_SEARCH); intent.putExtra(SearchManager.QUERY, query); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
- 用特定的app查询
Settings
- 打开特定设置界面
- Action
ACTION_SETTINGS
ACTION_WIRELESS_SETTINGS
ACTION_AIRPLANE_MODE_SETTINGS
ACTION_WIFI_SETTINGS
ACTION_APN_SETTINGS
ACTION_BLUETOOTH_SETTINGS
ACTION_DATE_SETTINGS
ACTION_LOCALE_SETTINGS
ACTION_INPUT_METHOD_SETTINGS
ACTION_DISPLAY_SETTINGS
ACTION_SECURITY_SETTINGS
ACTION_LOCATION_SOURCE_SETTINGS
ACTION_INTERNAL_STORAGE_SETTINGS
ACTION_MEMORY_CARD_SETTINGS
- Action
public void openWifiSettings() { Intent intent = new Intent(Intent.ACTION_WIFI_SETTINGS); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
- 打开特定设置界面
Text Messaging
Web Browser
- 载入一个web界面
- Action
ACTION_VIEW - Data URI
http:
https: - MIME Type
PLAIN_TEXT_TYPE (“text/plain”)
“text/html”
“application/xhtml+xml”
“application/vnd.wap.xhtml+xml”
- Action
public void openWebPage(String url) { Uri webpage = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, webpage); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
intent filter
<activity ...> <intent-filter> <action android:name="android.intent.action.VIEW" /> <!-- Include the host attribute if you want your app to respond only to URLs with your app's domain. --> <data android:scheme="http" android:host="www.example.com" /> <category android:name="android.intent.category.DEFAULT" /> <!-- The BROWSABLE category is required to get links from web pages. --> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity>
- 载入一个web界面