前言
相信大家在生活中会发现很多网页都有这样的功能,前一秒还在网页呢 后一秒莫名其妙的就进入了他们的APP,那么这种效果到底是如何做的呢?
经过本屌丝认真学习后发现是使用安卓中android:scheme属性来完成功能的,这里说一下我个人看法,我觉得android:scheme属性很想Intent的隐式匹配,或者说他就是隐式的一种,言归正传 接下来我们来看一下到底咋用
实践部分
首先我们先新建一个项目并且在AndroidManifest.xml
中加入android:scheme属性
<!--增加如下属性,其中crack是唤醒程序的action-->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="crack"/>
</intent-filter>
现在我们在MainActivity中获取网页转过来的参数
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text_ht);
Intent intent = new Intent();
//获取参数详情
Uri uri = intent.getData();
if(uri != null){
textView.setText(uri.getPath());
String host = uri.getHost();
String dataString = intent.getDataString();
String id = uri.getQueryParameter("d");
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);
}
}
}
现在咱们APP该做的视频已经做完了,现在来搞网页的
新建一个ht.html
<html>
<head>
<meta http-equiv=content-type content="text/html;charset=utf-8">
<title>网页唤醒APP</title>
</head>
<body>
<input id="txt"/>
<input type="button" value="提交" onclick="sub()">
</body>
<script>
//获取内容
function sub(){
var content = document.getElementById("txt");
//此处注意其中crack替换为你APP中设置的名字
var str = 'crack://crack.gm:80/'+content.value+'?p=91&d=15';
openUrl(str);
}
//唤醒APP
function openUrl(url){
window.open(url);
}
</script>
</html>
接下来 我们来运行一下看看效果