intent(2、隐形intent)
1、intent可以通过intent-filter隐形调用,常用方法如下:
private final static String MY_ACTION = "com.example.ACTION.other"; Intent intent1 = new Intent(); intent1.setAction(MY_ACTION); startActivity(intent1);
同时配置manifest.xml:
<activity android:name="com.example.demo04.OtherActivity"> <intent-filter> <action android:name="com.example.ACTION.other" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
2、intent-filter里有几个参数要注意,它对应着intent的几个属性,包括
- Action:Action属性的值为一个字符串,它代表了系统中已经定义了一系列常用的动作。通过setAction()方法或在清单文件AndroidManifest.xml中设置。默认为:DEFAULT。
- Data:Data通常是URI格式定义的操作数据。例如:tel:// 。通过setData()方法设置。
- Category:Category属性用于指定当前动作(Action)被执行的环境。通过addCategory()方法或在清单文件AndroidManifest.xml中设置。默认为:CATEGORY_DEFAULT。
- Extras:Extras属性主要用于传递目标组件所需要的额外的数据。通过putExtras()方法设置。
3、Category的例子:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <!-- category android:name="android.intent.category.LAUNCHER"/--> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
此时,点击home键,该activity会提示用户是否选择,常用在桌面应用上。
4、几个系统intent的例子:
//浏览网页 Uri uri = Uri.parse("http://www.google.com"); Intent it = new Intent(Intent.ACTION_VIEW,uri); startActivity(it); //拨打电话 Uri uri = Uri.parse("tel:12321312312"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it); //查看图片 Intent i = new Intent(); i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(i, 11);