Android使用URI启动应用的学习

在Android中使用Activity标签下的data标签来描述该Activity可打开的资源类型,根据官方文档的说明其语法如下:

<data android:host="string"
      android:mimeType="string"
      android:path="string"
      android:pathPattern="string"
      android:pathPrefix="string"
      android:port="string"
      android:scheme="string" />
当URI符合scheme://host:port/path的形式或者满足pathPrefix或pathPattern属性的描述时就会启动相应的Activity。

假设有http://mush.com/test/a.html

则应设置

host=“mush.com”

scheme="http"


使用path匹配,则需将path设为/test/a.html
使用pathPrefix匹配,则需将pathPrefix设为/test
使用pathPattern匹配,则需将pathParrern设为.*\\html


示例:

新建一个android HelloWorld,修改mainctivity的onCreate方法:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
		Intent intent = getIntent();
		Uri uri = intent.getData();
		
        setContentView(R.layout.activity_main);
        this.txt_uri = (TextView) this.findViewById(R.id.txt_uri);
        
        if(uri!=null){
        	this.txt_uri.setText(uri.toString());
        }
        
    }
修改其manifest

<activity
            android:name="com.mush.uritest.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="android.intent.action.VIEW" >
                </action>

                <category android:name="android.intent.category.DEFAULT" >
                </category>
                <data
                    android:host="mush.com"
                    android:pathPrefix="/test"
                    android:scheme="http" >
                </data>
            </intent-filter>
        </activity>

使用zxing扫面下面的条形码

则会弹出 Chooser Dialog


打开后效果如下:




源码:http://download.csdn.net/detail/btyh17mxy/5285927

参考:https://developer.android.com/guide/topics/manifest/data-element.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值