44、网页启动Activity,网页传值Activity
在assets新建一个demo.html 文件。
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>面试帮Demo</title>
5 </head>
6 <body style="font-size: 20px;">
7 <ul>
8 <li><a href="testapp://test?id=100101&title=HelloAndroid">Start
9 TestActivity</a></li>
10 </ul>
11 </body>
12 </html>
在XML布局中创建一个WebView控件。
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent" >
5 <WebView
6 android:id="@+id/web"
7 android:layout_width="match_parent"
8 android:layout_height="match_parent" />
9 </RelativeLayout>
在MainActivity代码中调用。
1 public class MainActivity extends Activity {
2
3 private WebView mWebView;
4
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8
9 setContentView(R.layout.activity_main);
10
11 initWebView();
12
13 mWebView.loadUrl("file:///android_asset/demo.html");
14 }
15
16 private void initWebView() {
17 mWebView = (WebView) findViewById(R.id.web);
18 mWebView.setWebChromeClient(new WebChromeClient() {
19 @Override
20 public void onReceivedTitle(WebView view, String title) {
21 setTitle(title);
22 }
23 });
24 }
25
26 }
传值到另外一个Activity接收。
1 @Override
2 protected void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4
5 TextView tv = new TextView(this);
6 setContentView(tv);
7
8 Uri data = getIntent().getData();
9 if (data != null) {
10 String id = data.getQueryParameter("id");
11 String title = data.getQueryParameter("title");
12 String text = "id=" + id + ", title=" + title;
13 tv.setText(text);
14 }
15 }