Intent隐式跳转及参数传递

简单来说intent隐式跳转就是用action打开activity,server,broadcast。intent传递参数的方式有两种,一种是大家熟悉的extra,键值对的形式,直接传递参数;另一种就是uri的方式,String str = "content://com.android.test?username=merlin&password=123456"; Uri uri = Uri.parse(str); Intent intent = new Intent(Intent.ACTION_VIEW,uri); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent); intent构造方法里的两个参数一个action,一个uri 。还要设置category。好了这些跳转的工作做完了那么就好看看如何接受了。首先接受方要在manifest文件里设置过滤器,来判断是不是要发给我的intent。 <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="com.android.test" android:scheme="content" /> </intent-filter>我们来看一下这个过滤器 首先
action category就是之前说过的传递的时候设置的 host就是str里冒号双斜杠前边的那个内容,host呢就是就是scheme和?中间的那部分当然还有port和path,这里就不说了 android.developer.com里会有详细介绍。在activity里获取uri然后判断host和scheme是不是我们想要的然后获取username和password具体获取方法是 String username = uri.getQueryParameter("username");获取host直接可以调用 uri.getHost()方法。获取完成之后可以执行想要做的操作。
String str = "meixian://com.meixian.shopkeeper?action=login&phone=1234&from=meixian_crm";
Uri uri = Uri.parse(str);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);

这是接收的源码

Uri uri = getIntent().getData();
if (uri !=null ){
    String host = uri.getHost();
    String scheme  = uri.getScheme();

    String param = uri.getQueryParameter("action");
    if ("login".equals(param)){
        String phone  = uri.getQueryParameter("phone");
        String from = uri.getQueryParameter("from");

        textView.setText(phone+from);
        startActivity(new Intent(this,MainActivity.class).putExtra("name",phone+from));
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值