Android学习心得(25) --- Intent启动方式研究

来源:互联网 发布:战舰世界亚利桑那数据 编辑:程序博客网 时间:2024/06/07 02:16

Activity Flag

intent.addFlags(Intent.XXX)FLAG_ACTIVITY_NEW_TASK  singleTaskFLAG_ACTIVITY_SINGLE_TOP  singleTopFLAG_ACTIVITY_CLEAR_TOP  在此之上的所有的都要出栈FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS  拥有这个标记的Activity不会出现在历史Activity列表中

已知包名

包名与类名:

Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER);     // android默认给加上一个CATEGORY_DEFAULTComponentName cn = new ComponentName(packageName, className);  // 包名类名   intent.setComponent(cn); startActivity(intent); 

单独知道包名:

PackageManager packageManager = getPackageManager();           Intent intent=new Intent();           intent =packageManager.getLaunchIntentForPackage("pkgName");    // 获取Intent        if(intent==null){              System.out.println("APP not found!");         }          startActivity(intent);             } 

未知包名

a. Filter Rules

使用IntentFilter来限定应用处理的通用操作动作、类型、数据、定义类型action category data匹配规则如下:一个IntentFilter对应一个Intent必须匹配三个条件:1. action&category必须匹配 2. data(data type & data scheme authority path)匹配 3. Action匹配如果没有action,则IntentFilter只会匹配没有action的Intentaction匹配规则:必须相同,如果没有action,则带有action intent匹配失败category匹配规则:只要有,必须全部匹配data匹配规则:与Action类似,两部分组成 mineType&&URIURI: <scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]必须包含data,必须完全匹配某一个data注意:同时设置data&type必须使用setDataAndType,不然会覆盖开发的时候,可以通过PackageManager.resolveActivity || Intent.resolveActivity判断返回值是否为nul来判断是否匹配PackageManager.queryIntentActivities可以返回所有的成功匹配的Activity

Data Type matches if any of the given values match the Intent type. The Intent type is determined by calling resolveType(ContentResolver). A wildcard can be used for the MIME sub-type, in both the Intent and IntentFilter, so that the type “audio/” will match “audio/mpeg”, “audio/aiff”, “audio/“, etc. Note that MIME type matching here is case sensitive, unlike formal RFC MIME types! You should thus always use lower case letters for your MIME types.
Data Scheme matches if any of the given values match the Intent data’s scheme. The Intent scheme is determined by calling getData() and getScheme() on that URI. Note that scheme matching here is case sensitive, unlike formal RFC schemes! You should thus always use lower case letters for your schemes.
Data Scheme Specific Part matches if any of the given values match the Intent’s data scheme specific part and one of the data schemes in the filter has matched the Intent, or no scheme specific parts were supplied in the filter. The Intent scheme specific part is determined by calling getData() and getSchemeSpecificPart() on that URI. Note that scheme specific part matching is case sensitive.
Data Authority matches if any of the given values match the Intent’s data authority and one of the data schemes in the filter has matched the Intent, orno authories were supplied in the filter. The Intent authority is determined by calling getData() and getAuthority() on that URI. Note that authority matching here is case sensitive, unlike formal RFC host names! You should thus always use lower case letters for your authority.
Data Path matches if any of the given values match the Intent’s data path and both a scheme and authority in the filter has matched against the Intent, or no paths were supplied in the filter. The Intent authority is determined by calling getData() and getPath() on that URI.
Categories match if all of the categories in the Intent match categories given in the filter. Extra categories in the filter that are not in the Intent will not cause the match to fail. Note that unlike the action, an IntentFilter with no categories will only match an Intent that does not have any categories.

b. 主要方法:通过Action启动

1、系统Action2、自定义Action

URI调用系统应用:

1,调web浏览器

Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri); 

2,地图

Uri mapUri = Uri.parse(“geo:38.899533,-77.036476”);
returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

3,调拨打电话界面

Uri telUri = Uri.parse("tel:100861"); returnIt = new Intent(Intent.ACTION_DIAL, telUri); 

4,直接拨打电话

Uri callUri = Uri.parse("tel:100861"); returnIt = new Intent(Intent.ACTION_CALL, callUri); 

5,卸载

Uri uninstallUri = Uri.fromParts("package", "xxx", null); returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri); 

6,安装

Uri installUri = Uri.fromParts("package", "xxx", null); returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); 

7,播放

Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3"); returnIt = new Intent(Intent.ACTION_VIEW, playUri); 

8,调用发邮件

Uri emailUri = Uri.parse("mailto:xxxx@gmail.com"); returnIt = new Intent(Intent.ACTION_SENDTO, emailUri); 

9,发邮件

returnIt = new Intent(Intent.ACTION_SEND); String[] tos = { "xxxx@gmail.com" }; String[] ccs = { "xxxx@gmail.com" }; returnIt.putExtra(Intent.EXTRA_EMAIL, tos); returnIt.putExtra(Intent.EXTRA_CC, ccs); returnIt.putExtra(Intent.EXTRA_TEXT, "body"); returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject"); returnIt.setType("message/rfc882"); Intent.createChooser(returnIt, "Choose Email Client"); 

10,发短信

Uri smsUri = Uri.parse("tel:100861"); returnIt = new Intent(Intent.ACTION_VIEW, smsUri); returnIt.putExtra("sms_body", "yyyy"); returnIt.setType("vnd.android-dir/mms-sms"); 

11,直接发

Uri smsToUri = Uri.parse("smsto://100861"); returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri); returnIt.putExtra("sms_body", "yyyy"); 发彩信 Uri mmsUri = Uri.parse("content://media/external/images/media/23"); returnIt = new Intent(Intent.ACTION_SEND); returnIt.putExtra("sms_body", "yyyy"); returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri); returnIt.setType("image/png");