如何从外部浏览开启Android App
程序员文章站
2022-06-27 09:04:27
这里主要用的是第三方的东西,就是魔窗中的mlink功能。想了解魔窗的朋友就到官网去看看吧。在这里我说一下我通过魔窗是怎么实现的。首先我们看一下浏览器上面的代码,这个就是我们从该页面上跳转打开app。&...
这里主要用的是第三方的东西,就是魔窗中的mlink功能。想了解魔窗的朋友就到官网去看看吧。在这里我说一下我通过魔窗是怎么实现的。
首先我们看一下浏览器上面的代码,这个就是我们从该页面上跳转打开app。
<html> <head> <title>浏览器打开app测试</title> <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.js"></script> <script src="https://static.mlinks.cc/scripts/dist/mlink.min.js"></script> </head> <body> <a id="btnopenapp">打开app</a> <script> new mlink( { mlink: "aa2f", button: document.queryselector('a#btnopenapp'), autolaunchapp : false, autoredirecttodownloadurl: true, downloadwhenuniversallinkfailed: false, inapp : true, params: { storyboardkey:'detailsactivity', storyboardid:'productdetail', name:'twoactivity', productid:'1454456156' } }) </script> </body> </html>
mlink: "aa2f",这个aa2f就是我们在魔窗上配置的一个短链接最后面的mlink,比如,我的短链接是:http://a.mlinks.cc/aa2f;
button: document.queryselector('a#btnopenapp'),
autolaunchapp : false,
autoredirecttodownloadurl: true,
downloadwhenuniversallinkfailed: false,
inapp : true,
这些在官网上都有解释,我就不解释了;params就是我们要传递的参数。
好了,现在到了关键时刻了,就是我们app上面的配置了,先看我的项目目录:
先把魔窗这个包倒进来,然后在gradle上进行配置,我的是这样配置的:
apply plugin: 'com.android.application' apply plugin: 'me.tatarka.retrolambda' android { compilesdkversion 24 buildtoolsversion "24.0.0" defaultconfig { applicationid "com.wingsofts.magicwindowdemo" minsdkversion 15 targetsdkversion 24 versioncode 1 versionname "1.0" } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } compileoptions { targetcompatibility 1.8 sourcecompatibility 1.8 } } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) testcompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.1.1' compile "io.reactivex:rxandroid:1.2.0" compile "io.reactivex:rxjava:1.1.7" compile(name: 'magicwindowsdk', ext: 'aar') } repositories { flatdir { dirs 'libs' } }
然后在看看myapp页面,我的是这样的:
package com.wingsofts.magicwindowdemo; import android.app.application; import com.zxinsight.session; public class myapp extends application { @override public void oncreate() { super.oncreate(); session.setautosession(this); } }
这里的session的作用是获取到活动。
mainactivity页面是这样的:
package com.wingsofts.magicwindowdemo; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void onclick(view v) { startactivity(new intent(this, detailsactivity.class)); } }
activity_main:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.wingsofts.magicwindowdemo.mainactivity" > <button android:layout_centerinparent="true" android:onclick="onclick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点我去详情页" /> </relativelayout>
上面这两个页面就没什么好介绍的了,很平常的两个页面。
detailsactivity页面:
package com.wingsofts.magicwindowdemo; import android.os.bundle; import android.support.v7.app.appcompatactivity; public class detailsactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_details); } }
activity_details布局:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.wingsofts.magicwindowdemo.detailsactivity" > <textview android:text="我是详情页" android:textsize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </relativelayout>
twoactivity页面:
package com.wingsofts.magicwindowdemo; import android.support.v7.app.appcompatactivity; import android.os.bundle; public class twoactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_two); } }
对应得布局是activity_two:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.wingsofts.magicwindowdemo.twoactivity"> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="我是第二个页面" android:textsize="40sp" /> </relativelayout>
下面这一个页面很重要,基本上所有的重要代码都集中在这里:
package com.wingsofts.magicwindowdemo; import android.content.context; import android.content.intent; import android.net.uri; import android.os.bundle; import android.support.v7.app.appcompatactivity; import com.zxinsight.mlink; import com.zxinsight.mwconfiguration; import com.zxinsight.magicwindowsdk; import com.zxinsight.mlink.mlinkcallback; import com.zxinsight.mlink.mlinkintentbuilder; import java.util.iterator; import java.util.map; import java.util.concurrent.timeunit; import rx.observable; import rx.android.schedulers.androidschedulers; public class splashactivity extends appcompatactivity { private string demoname = "com.wingsofts.magicwindowdemo"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_splash); initsdk();//初始化sdk registerlinks(this);//注册sdk initmlink(); } public void initmlink() { intent intent = getintent(); uri mlink = intent.getdata(); //如果从浏览器传来 则进行路由操作 if (mlink != null) { mlink.getinstance(this).router(this, mlink); finish(); } else { //否则执行原本操作 go2mainactivity(); } } //注册sdk public void registerlinks(context context) { mlink.getinstance(context).registerdefault(new mlinkcallback() { @override public void execute(map parammap, uri uri, context context) { //默认的路由 如果没有匹配则转跳到 mainactivity 为你的首页 mlinkintentbuilder.buildintent(parammap, context, mainactivity.class); } }); // testkey: mlink 的 key, mlink的唯一标识,用于进行路由操作 mlink.getinstance(context).register("productdetail", new mlinkcallback() { public void execute(map parammap, uri uri, context context) { //!!!!!!!!注意 此处有坑,如果你的splashactivity转跳有延迟,那么在此处转跳的延迟必须大于前者转跳时间 observable.timer(1050, timeunit.milliseconds) .observeon(androidschedulers.mainthread()) .subscribe(avoid -> { //mlinkintentbuilder.buildintent(parammap, context, class.forname(name)); string name = (string) parammap.get("name"); intent intent = new intent(); intent.setclassname(context, demoname + "." + name); startactivity(intent); }); } }); } //初始化魔窗sdk public void initsdk() { mwconfiguration config = new mwconfiguration(this); config.setdebugmodel(true) //带有fragment的页面。具体查看2.2.2 .setpagetrackwithfragment(true) //设置分享方式,如果之前有集成sharesdk,可在此开启 .setshareplatform(mwconfiguration.original); magicwindowsdk.initsdk(config); } public void go2mainactivity() { //延迟1秒转跳 observable.timer(1, timeunit.seconds) .observeon(androidschedulers.mainthread()) .subscribe(along -> { startactivity(new intent(this, mainactivity.class)); finish(); }); } private void startactivity(map parammap, context context, class<?> clazz) { intent intent = new intent(context, clazz); intent.addflags(intent.flag_activity_new_task | intent.flag_activity_clear_top); if (parammap != null) { iterator iter = parammap.entryset().iterator(); while (iter.hasnext()) { map.entry entry = (map.entry) iter.next(); string key = (string) entry.getkey(); string val = (string) entry.getvalue(); intent.putextra(key, val); } } context.startactivity(intent); } }
布局:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.wingsofts.magicwindowdemo.splashactivity" > <textview android:textsize="30sp" android:text="我是引导页!!" android:layout_centerinparent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </relativelayout>
代码上面的我都有注释,在这里需要注意的是,传递参数的话一定要配置好,否则会出现错误。
以上就是如何从外部浏览开启android app的详细内容,更多关于从外部浏览开启android app的资料请关注其它相关文章!