android 通过sheme打开activity

android 提供了通过sheme打开activity,例如我们在webview点击链接可以打开打电话,发短信等,也可以通过Intent开打开activity

一,通过用手机的浏览器(内置,第三方都可)访问一个网页,实现点击一个链接启动自己的应用,并传递数据。

首先在Mainifest文件里面对要启动的Activity添加一个过滤器。

	<activity
            android:name="com.example.helloworld.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"/>  
                <categoryandroid:name="android.intent.category.DEFAULT"/> 
                <category android:name="android.intent.categoryBROWSABLE"/>
		<data android:scheme="znn"/>
            </intent-filter>
        </activity>


在MainActivity接收数据:

public class MainActivity extends Activity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = getIntent();
        String scheme = intent.getScheme();
        Uri uri = intent.getData();
        System.out.println("scheme:"+scheme);
        if (uri != null) {
            String host = uri.getHost();
            String dataString = intent.getDataString();
            String id = uri.getQueryParameter("id");
            String path = uri.getPath();
            String path1 = uri.getEncodedPath();
            String queryString = uri.getQuery();
            System.out.println("host:"+host);
            System.out.println("dataString:"+dataString);
            System.out.println("id:"+id);
            System.out.println("path:"+path);
            System.out.println("path1:"+path1);
            System.out.println("queryString:"+queryString);
        }
    }
}


写一个最简单的网页:

<a href=”znn://aa.bb:80/test?p=12&d=1″>test</a>

测试地址:www.erdian.net/m.html

测试结果:

scheme:znn

host:aa.bb

dataString:znn://aa.bb:80/test?p=12&id=1

id:1

path:/test

path1:/test

queryString:p=12&d=1

二, 利用如下Intent调用Activity

<activity
            android:name=".SecondActivity"
            android:label="@string/app_name">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="dadabus"  android:host="page" android:path="/b" />

            </intent-filter>

        </activity>
        <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="dadabus"  android:host="page" android:path="/c"/>

            </intent-filter>

        </activity>

try {
            MYIntent intent = new MYIntent(Intent.ACTION_VIEW, Uri.parse("dadabus://page/b?id=9"));
            System.out.println("len intent class"+intent.hashCode());
            MainActivity.this.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("dadabus://"));
            intent.addCategory("dadabus_first");
        }
上面两个例子可以说明怎么用sheme,其实就是自定义Uri,格式是scheme://host/path?key=value&key2=value2很熟悉是不是。

在设置intent和intent-filter是有三个常用参数data,action,category是要特别注意的,会有新的文章来说明。

有个使用注意事项也附上http://blog.csdn.net/csxwc/article/details/10222913


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值