Android 通过网页打开自己的APP(scheme)
通过用手机的浏览器(内置,第三方都可)访问一个网页,实现点击一个链接启动自己的应用,并传递数据。
首先在Mainifest文件里面对要启动的Activity添加一个过滤器。
1
|
< activity
|
2
|
android:name = "com.example.helloworld.MainActivity"
|
3
|
android:label = "@string/app_name" >
|
4
|
< intent-filter >
|
5
|
< action android:name = "android.intent.action.MAIN" />
|
6
|
7
|
< category android:name = "android.intent.category.LAUNCHER" />
|
8
|
</ intent-filter >
|
9
|
< intent-filter >
|
10
|
< action android:name = "android.intent.action.VIEW" />
|
11
|
< category android:name = "android.intent.category.DEFAULT" />
|
12
|
< category android:name = "android.intent.category.BROWSABLE" />
|
13
|
< data android:scheme = "znn" />
|
14
|
</ intent-filter >
|
15
|
</ activity >
|
在MainActivity接收数据:
1
|
public class MainActivity extends Activity implements View.OnClickListener{
|
2
|
3
|
@Override
|
4
|
protected void onCreate(Bundle
|
5
|
super .onCreate(savedInstanceState);
|
6
|
setContentView(R.layout.activity_main);
|
7
|
8
|
Intent
|
9
|
String
|
10
|
Uri
|
11
|
System.out.println( "scheme:" +scheme);
|
12
|
if (uri null )
|
13
|
String
|
14
|
String
|
15
|
String "id" );
|
16
|
String
|
17
|
String
|
18
|
String
|
19
|
System.out.println( "host:" +host);
|
20
|
System.out.println( "dataString:" +dataString);
|
21
|
System.out.println( "id:" +id);
|
22
|
System.out.println( "path:" +path);
|
23
|
System.out.println( "path1:" +path1);
|
24
|
System.out.println( "queryString:" +queryString);
|
25
|
}
|
26
|
|
27
|
}
|
28
|
}
|
写一个最简单的网页:
<a href=”znn://aa.bb:80/test?p=12&d=1″>test</a>
测试结果:
scheme:znn
host:aa.bb
dataString:znn://aa.bb:80/test?p=12&id=1
id:1
path:/test
path1:/test
queryString:p=12&d=1
时间: 2024-11-16 01:16:36