本地应用manifest的设置
Manifest中 Data的语法:
<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 or pathPrefix or pathPattern
如果scheme没有指定,那其它的属性均无效;
如果host没有指定,那么port,path,pathPrefix,pathPattern均无效;
如果在manifest里这样写:<data android:scheme="something" android:host="project.example.com" />
那么Uri uri = Uri.parse("something://project.example.com"); 才可以匹配
再如:
<data android:scheme="something" android:host="project.example.com" android:port="80"/>
等同于这样写:
<data android:scheme="something"/>
<data android:host="project.example.com"/>
<data android:port="80"/>
那么Uri uri = Uri.parse("something://project.example.com:80"); 才可以匹配
可以有多个data,只需匹配其中一个即可
<activity android:name=".MyActivityTwo" android:label="@string/activityTwo">
<intent-filter>
<action android:name="android.intent.action.leo"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<data android:scheme="x-id"/>
<data android:scheme="something"/>
</intent-filter>
</activity>
启动方式
android中第三方应用启动本地应用的方式有三种:1、通过包名启动 2、通过scheme启动 3、通过webview启动
包名启动
方式一:
Intent mIntent =new Intent();
mIntent.setClassName("包名", "要启动的activity(全称)");
startActivity(mIntent);
注意:要启动的Activity需要设置 android:exported="true"的属性,支持外部应用打开。而通过scheme启动则不需要。
注意:要启动的Activity需要设置 android:exported="true"的属性,支持外部应用打开。而通过scheme启动则不需要。
方式二:
Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(LaunchIntent);
这个方式我感觉类似于双击home键之后选中应用,从activity栈里面把之前的页面展示到前台栈中。
通过scheme启动
跳转到我们系统中的一些标准功能都是通过scheme跳转实现的,比如跳转到打电话,跳转到短信等。
Intent dialIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneNumber));//直接拨打电话
startActivity(dialIntent);
跳转到我们本地应用:
Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("warehouse://?where_shotcut="));
context.startActivity(it);
格式含义:
scheme://host:8080/path?query1=1&query2=true
● scheme代表Scheme协议名称
● host代表Scheme作用的地址域
● 8080代表改路径的端口号
● path代表的是指定页面(路径)
● query1和query2代表传递的两个参数
通过webview启动
String url ="ntclound://nthost?data1=1&data2=2";
mWebView.loadUrl(url);
//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
if (!url.startsWith("http")) {
try {
// 以下固定写法,表示跳转到第三方应用
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getApplication().startActivity(intent);
} catch (Exception e) {
// 防止没有安装的情况
e.printStackTrace();
}
return true;
}else{
view.loadUrl(url);
}
return true;
}
....
}
本地应用获取scheme启动带来的参数
// 启动代码
webview.loadUrl("scheme://host?key1=参数1&key2=参数2");
// 本地应用解析
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 key1 = uri.getQueryParameter("key1");
String key2 = uri.getQueryParameter("key2");
}