再识Intent-实现调用Android内置浏览器打开网页
2016-12-14 16:24
471 查看
本篇博客主要讲解如何使用Intent来实现调动Android内置浏览器打开网页
本篇博客实现的是一个简单的输入网址打开网页的Demo
[b]实现步骤:[/b]
实例化一个Intent对象
使用Uri.parse()方法解析网页地址
intent.setAction()方法 PS:action规定了intent要完成的动作,是一个字符串常量,可以使系统自定义的action,比如本博客使用的ACTION_VIEW。
intent.setData()方法,传入的是Uri,用于数据过滤,可以让系统自动自动去寻找匹配目标组件
本篇博客实现的是一个简单的输入网址打开网页的Demo
[b]实现步骤:[/b]
实例化一个Intent对象
使用Uri.parse()方法解析网页地址
intent.setAction()方法 PS:action规定了intent要完成的动作,是一个字符串常量,可以使系统自定义的action,比如本博客使用的ACTION_VIEW。
intent.setData()方法,传入的是Uri,用于数据过滤,可以让系统自动自动去寻找匹配目标组件
界面设计展示
MainActivity的布局文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.ggq.intent_openweb.MainActivity" android:weightSum="1"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="网址:" android:id="@+id/tv1" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/et_web" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:gravity="center" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开网页" android:id="@+id/btn_oepn" android:textSize="25dp" /> </LinearLayout> </LinearLayout>
Java代码实现
MainActivity代码:package com.ggq.intent_openweb; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText et_web=null; private Button btn_open=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_open = (Button) findViewById(R.id.btn_oepn); et_web = (EditText) findViewById(R.id.et_web); btn_open.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String address = et_web.getText().toString().trim(); Intent intent = new Intent(); Uri uri = Uri.parse(address); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); MainActivity.this.startActivity(intent); } }); } }
最终效果
相关文章推荐
- Android调用系统自带浏览器打开网页的实现方法
- Android调用手机浏览器打开某网页出现异常情况
- Android----Intent,运用由android系统帮助匹配实现打电话、发送短信、打开网页、播放音乐、打开视频、打开图片、安装APK、通知栏消息、拍照上传头像等功能
- 调用Android自带浏览器打开网页
- Android获取本机浏览器包名,并调用指定浏览器打开网页
- 在Android中调用浏览器打开网页方式
- 使用android内置Intent打开网页、拨号、打电话
- 20.调用系统浏览器打开网页-隐式Intent的用法
- 调用Android自带浏览器打开网页
- Android中通过intent打开浏览器到指定网页
- XE5 android怎么调用自带浏览器打开网页
- Android 调用浏览器,打开网页
- android学习笔记---53_采用网页设计软件界面,以及使用android系统内置的浏览器,利用js调用java方法
- cocos2d-x调用android内嵌浏览器打开网页
- 调用Android自带浏览器打开网页
- C#实现调用默认浏览器打开多个网页
- 在Android中调用浏览器打开网页方式
- Intent调用手机指定浏览器打开网页
- Android打开网页(android内置浏览器+webView)
- cocos2d-x调用android内嵌浏览器打开网页