Intent隐式跳转及参数传递

来源:互联网 发布:ubuntu hadoop安装教程 编辑:程序博客网 时间:2024/05/06 22:25

简单来说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 0
原创粉丝点击