浏览器打开android应用APP

    浏览器打开android系统的应用使用的是隐式的Intent进行设置的,隐式的Intent可以比显示的Intent的优势很大,不仅可以跨应用调APP,还可以通过浏览器打开本地的APP,下面我们看如何进行设置的。

首先建立一个android应用,创建一个Activity和布局:

LauchLocalActivity:

package com.example.firstday;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.EditText;

public class LauchLocalActivity extends Activity {
	private EditText editText2,editText1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_local);
		//获取浏览器传递的参数
		Uri uri = getIntent().getData();
		System.out.println(uri);
		String name = uri.getQueryParameter("name");
		String content = uri.getQueryParameter("content");
		editText2 = (EditText) findViewById(R.id.editText2);
		editText1 = (EditText) findViewById(R.id.editText1);
		editText1.setText(name);
		editText2.setText(content);
	}

}
布局文件activity_local.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是通过浏览器启动的" />
    
     <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交" />
</LinearLayout>
找到AndroidManifest.xml文件

修改如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstday"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/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>
        </activity>
        <!-- 配置通过浏览器访问打开应用配置 -->
         <activity  
            android:name=".LauchLocalActivity"  
            android:label="@string/local">  
           <intent-filter>
             <category android:name="android.intent.category.BROWSABLE" />
             <category android:name="android.intent.category.DEFAULT" />
             <action android:name="android.intent.action.VIEW"/>
             <data android:scheme="local"  android:host="first.app" android:pathPrefix="/first"/>
          </intent-filter>
        </activity> 
    </application>

</manifest>

到此android已经完成了,下面我们写一个简单的html页面来启动我们的APP:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<a href="local://first.app/first?name=gaoxuxu&content=分享的第一个浏览器打开的应用">启动一个APP应用</a>

</body>
</html>

要使用android系统上面的浏览器访问(务必谨记:android系统上面的localhost我们换成10.0.2.2才可以进行访问端口还是自己设置的端口默认80)

点击启动之后的效果:



我们传递的参数也获取到了。至此,浏览器启动androidAPP应用完毕,希望可以帮助大家。




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值