调用系统自带浏览器的方法

来源:互联网 发布:sql语句between包含 编辑:程序博客网 时间:2024/04/29 21:32

调用系统自带的浏览器打开网页有两种方法。

第一种方法:

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://www.baidu.com"));startActivity(intent);

第二种方法:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));startActivity(intent);

一个完整的例子如下:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.webviewdemo1.MainActivity" >    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Open Website" /></RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button)findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//Intent intent = new Intent(Intent.ACTION_VIEW);//intent.setData(Uri.parse("http://www.baidu.com"));//startActivity(intent);Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));startActivity(intent);}});}}


0 0
原创粉丝点击