业务场景

当需要从外部第三方网页中通过点击某个链接或按钮启动App应用程序。

实现

  • 新建demo工程,并实现一个Activity,用来接收从外部跳转传入的信息。代码如下:
    public class MainActivity extends Activity {    private TextView tv_content;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_content = (TextView)findViewById(R.id.tv_content);        Intent intent = getIntent();        if (intent != null){            Uri uri = intent.getData();            if (uri != null){                String dataString = intent.getDataString();                String scheme = uri.getScheme();                String host = uri.getHost();                String query = uri.getQuery();                tv_content.setText("dataString = " + dataString + " | scheme = " + scheme + " | host = " + host + " | query = " + query);            }        }    }
  • 修改AndroidManifest文件,设置Activity的接收Action的属性:
<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>            <intent-filter>                <action android:name="android.intent.action.VIEW">action>                <category android:name="android.intent.category.DEFAULT">category>                <category android:name="android.intent.category.BROWSABLE">category>                <data android:scheme="ice"                      android:host="start.app"/>            intent-filter>        activity>application>

如上所示,在data里设置了 scheme和host,则该Activity可以接收和处理类似于 “ice://start.app/XXX”的链接。

  • 接下来 就是要模拟一个第三方网页环境用于启动我们的app应用,我们可以新建一个包含WebView的工程2,用webview加载我们的测试html,html网页代码如下:
<html><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>titletitle>head><body><a href="ice://start.app/test?type=1&value=2">打开appa><br/>body>html>
  • 运行工程2,效果如下:

点击链接“打开app”,会启动demo应用,且demo应用接收到了网页传过来的参数信息,效果如下:

更多相关文章

  1. cocos2dx 在mac下开发ios和android游戏
  2. Android(安卓)WebView加载网页,实现前进、后退、刷新、超链接
  3. 将android launcher2导入eclipse中运行
  4. Flutter ------- WebView加载网页
  5. android 4.0系统webview加载网页出现白屏
  6. android 网络连接处理分析
  7. NDK 编译可执行程序
  8. 在android工程中导入layoutlib.jar使用internal api
  9. Android一些系统路径的使用

随机推荐

  1. Android界面设计中的.9.png的使用技巧
  2. Android应用程序注册广播接收器(registerR
  3. Android之ListView和ArrayAdapter的组合
  4. Android(安卓)播放Gif 动画
  5. Android(安卓)TextView使用HTML处理字体
  6. Android(安卓)推荐博客
  7. Android路在何方?
  8. Android(安卓)JUnit 入门指南
  9. Android使用AIDL实现进程间通信
  10. fir.im Weekly - iOS / Android(安卓)动