上次给大家介绍了如何实现一个app内A-B跳转方法,这次我给大家介绍如何使用intent的显式意图访问系统app.
1:Intent的介绍
intent的数据传递分为显示意图和隐式意图,显示意图是指有具体的Activity可以跳转,而隐式意图是没有具体的Activity,需要通过设置动作的字符串来进行跳转,系统的每一个app都有不同的字符串来标示。在访问系统系统app之前先来做个例子了解下原理
2:首先创建2个安卓工程,在工程2中的清单文件其中一个Activity配置一个过滤器,代码如下
<activity android:name="com.example.activity02.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- 这是我们自己定义过滤条件 --> <intent-filter > <action android:name="org.second.Action"/> <data android:scheme="http"/> <data android:host="www.baidu.com"/> <data android:mimeType="p_w_picpath/jpg"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
下面那个intent-filter是我们自己定义的过滤条件(这里以网站为例子)
首先是name,这个字符串可以随便写。然后是scheme这是我们网站的http协议的开头,然后host是我们的网站名字。最后一个是类型,这里注意类型格式一定是xxx/xxx,我定义类型为jpg格式图片。注意这个过滤器只写一个所以必须要满足里面所有的过滤条件(这里有4个条件)否则无法访问到第二个app,还有如果是访问activity的话要加上<categoryandroid:name="android.intent.category.DEFAULT" />如上面代码所示
3:写好了第二个app条件,我们回到第一个app,布局放一个按钮,设置按钮的点击事件。将所有的过滤条件写上,然后执行跳转,具体代码如下
public void onClick(View v) {
//新建一个intent对象,由于是隐式传递所以不需要指定传入参数
Intent intent = new Intent();
//设置过滤的名字
intent.setAction("org.second.Action");
//如果过滤的条件没有Type就可以使用下面这个方法
//intent.setData(Uri.parse("http://www.baidu.com"));
//如果过滤条件有type就必须使用下面方法,分别传入对应Scheme,host以及Type
intent.setDataAndType
(Uri.parse("http://www.baidu.com"), "p_w_picpath/jpg");
startActivity(intent );
}
这样就可以通过隐式意图从第一个app的访问到第二个app
4:大家可以去网上搜安卓系统app的包(由于压缩包过大所以无法上传)包里面有安卓全部系统app的过滤条件(在清单文件里面找),这里我找了一个电话拨打界面的系统app过滤器,如下图
<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>
这里的过滤器就4个,过滤器之间的关系是或的关系,所以只需要满足其中一个即可,这里我在新建一个项目,将最上面的过滤器条件加入到intent类中访问电话界面,具体代码如下(这里Action由于系统有这个常量所以使用,用上面代码的name也可以实现)
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:12345"));
startActivity(intent);
}
由于是访问系统app所以别忘了加权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
5:打工告成结果如下
然后点击按钮跳转到电话界面,电话直接写好了,拨打就可以了
以上就是使用intent隐式意图访问其他app方法,下次我将给大家介绍安卓第2大组件-Service,这次程序的源代码我会上传到我的上传名字为博客标题
转载于:https://blog.51cto.com/7735447/1284575