您的位置:首页 > 移动开发

浏览器打开android应用APP

2015-11-09 23:47 323 查看
浏览器打开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应用完毕,希望可以帮助大家。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: