我的新书《Android App开发入门与实战》已于2020年8月由人民邮电出版社出版,欢迎购买。点击进入详情
文章目录
简介
既然是伪协议,那么就有真协议,比如:https://blog.csdn.net/ddnosh,这个就是我们通常说的网址,其实也是一种URL Scheme格式。
因为https是一种超文本传输安全协议,默认被浏览器识别和解析。
如果我们也照葫芦画瓢定义一种这样的格式,能够通过识别解析为自身app所用,不也是一种很好的解决方案么。
URL Scheme定义
如果我们也定义一个这样的协议,我们称之为伪协议,那么我们需要先了解一下什么是URL Scheme。
标准的URL Scheme格式定义如下:
scheme + (😕/) + hostname + (😃 + port + path + (?) + query + (#) + fragment
比如:https://androidwind.com:8080/test/add?uid=100&uid=200#myfrag
需要注意的是:scheme、hostname是必须的。其它部分可以选择要或不要,但是顺序不能变动。
URL Scheme解析
android提供了一种处理Scheme的类:Uri,位置在android.net.Uri。
通过Uri可以操作伪协议,以https://androidwind.com:8080/test/add?uid=100&age=20#myfrag为例:
getScheme()获取scheme:https;
getHost()获取hostname:androidwind.com;
getPort()获取hostname:8080;
getPath()获取path:/test/add;
getQuery()获取query:uid=100&uid=200;
也可以通过getQueryParameter(key)获取指定的值,比如getQueryParameter(uid),值为100;
getFragment()获取fragment:myfrag
URL Scheme应用
配置
URL Scheme的配置有两种方式,一种是直接在AndroidMainfest.xml中配置好,通过Intent跳转;
还有一种是在代码中直接解析URL Scheme,通过获取到的字段,通过代码进行跳转。
我们定义一个伪协议:tiny://androidwind:8080/add
1. AndroidMainfest.xml配置
<activity android:name=".SchemeActivity">
<!--activity过滤器-->
<intent-filter>
<!--scheme配置-->
<data
android:host="androidwind"
android:path="/add"
android:port="8080"
android:scheme="tiny"
/>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
2. 代码解析
String url = "tiny://androidwind:8080/add";
Uri uri = Uri.parse(url);
String scheme = uri.getScheme();
String host = uri.getHost();
String path = uri.getPath();
//判断path是不是某个业务
if("add".equals(path)) {
// 跳转到对应的业务页面;
}
URL Scheme应用场景
1. app应用内页面跳转
String url = "tiny://androidwind:8080/add";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
这样就可以打开SchemeActivity这个页面;
2. app应用内页面跳转(通过代码解析)
String url = "tiny://androidwind:8080/add";
Uri uri = Uri.parse(url);
String scheme = uri.getScheme();
String host = uri.getHost();
String path = uri.getPath();
//判断path是不是某个业务
if ("/add".equals(path)) {
// 跳转到对应的业务页面;
Toast.makeText(this, "伪协议解析完成", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, TestActivity.class));
}
3. 通过app应用内的H5页面跳转到app指定页面
StringBuilder sb = new StringBuilder();
sb.append("<html>");
sb.append("<body>");
sb.append("<h1> <a href=\"" + url + "\">"
+ "通过app应用内的H5页面跳转到app指定页面</a></h1>");
sb.append("</body>");
sb.append("</html>");
wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
4. 外部app启动本app,并跳转到指定页面
外部app可以通过下面的代码打开另外一个app:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tiny://androidwind:8080/add"));
startActivity(intent);
如果出现闪退则说明手机里面没有目标app,应该增加try catch处理。
5. 通过浏览器启动本app。并跳转到指定页面
通过手机浏览器,打开一个网页,然后点击网页上的超链接,打开手机内的app。
比如通过手机访问:
http://htmlpreview.github.io/?https://github.com/ddnosh/android-demo-scheme/blob/master/scheme.html
点击"打开app",就可以打开手机内安装的指定应用。
另外说明下,github上传的html文件,如果需要预览效果,可以在http://htmlpreview.github.io/网站做一个转换即可。
6. 通过push消息跳转到指定页面
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(MainActivity.this, "default");
builder.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle("tiny")
.setContentText("click to test scheme")
.setTicker("您有新的消息,请注意查收!")
.setOngoing(false)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setAutoCancel(true);
//8.0 以后需要加上channelId 才能正常显示
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
String channelId = "default";
String channelName = "默认通知";
notifyManager.createNotificationChannel(new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT));
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notifyManager.notify(1, notification);
点击消息栏通知,可以跳转到目标app页面。