前面介绍了如何利用Android系统提供的WebView控件进行网页访问,这里再介绍另外一种通过调用系统浏览器打开网页。
新建工程BrowserIntent, 需要编写的两个文件贴出如下:
1)、Activity文件BrowserIntentActivity.java:
package com.android.browserintent;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnKeyListener;
public class BrowserIntentActivity extends Activity {
private EditText mUrlText;
private Button mGoButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mUrlText = (EditText)findViewById(R.id.url_field);
mGoButton = (Button)findViewById(R.id.go_button);
mGoButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openNetPage();
}
});
mUrlText.setOnKeyListener(new OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_ENTER){
openNetPage();
return true;
}
return false;
}
});
}
private void openNetPage(){
Uri uri = Uri.parse(mUrlText.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
BrowserIntentActivity.this.startActivity(intent);
}
}
2)、布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/url_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:inputType="textUri"
android:imeOptions="actionGo"
android:layout_weight="9"
/>
<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Go"
/>
</LinearLayout>
这里明显不同的是,已不需要在AndroidManifest.xml文件中指定网络访问权限了。因为,毕竟访问网络已经不是本程序的职责范畴啦,
![偷笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/titter.gif)