隐式Intent使用uri进行交互

例如我要在浏览器中打开一个网址,我们就可以这样写一个Intent。当手机上的应用有声明这个Action为android.intent.action.VIEW并且data的声明的scheme为http的活动时,就会被相应的激活。

Button button5 = (Button)findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);//android.intent.action.VIEW
        intent.setData(Uri.parse("http://www.chicai.group"));//设置一个uri
        startActivity(intent);
    }
});


intent.setData对应的声明就是data标签写的属性 :

android:scheme用于指定数据的协议部分,如http

android:host用于指定数据的主机名部分,如www.chicai.group

android:port用于指定数据的端口部分

android:path用于指定主机名和端口之后的部分

android:mimeType用于指定可以处理的数据类型,允许使用通配符的方式进行指定。

<intent-filter>  
<action android:name="android.intent.action.VIEW"/>  
<category android:name="android.intent.category.DEFAULT" />  
<category android:name="android.intent.category.BROWSABLE" />  
<data android:scheme="http" android:host="主机名" android:path="/open"/>  
</intent-filter>


再例如打电话:

Button button6 = (Button)findViewById(R.id.button6);
button6.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:10086"));
        startActivity(intent);
    }
});

首页 我的博客
粤ICP备17103704号