Android实战:使用隐式Intent打开系统浏览器

阅读: 评论:0

Intent简介

使用Intent可以在Android的各个组件之间交互,如启动Activity, 启动Service,发送广播等。Intent不仅可以指定当前组件想要执行的动作,还可以在不同组件之间传递数据。
显式Intent:直接指明跳转到哪个Activity。Intent的构造函数接收两个参数,分别是Context和Activicy::java.class。
隐式Intent:指定Intent的Action和和Category。Intent的构造函数接收一个参数,就是Action。AndroidManiFest中通过 < intent-filter > 标签指定自己能够响应的intent。一个intent只能有一个Action,但是可以有多个Category。

启动系统浏览器

使用Intent启动系统浏览器属于隐式的一种用法。直接上代码。
布局文件:仅仅放了一个Button,用于启动浏览器。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_open_browser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开百度"/>

</LinearLayout>

代码文件
使用Intent打开网址。

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn_open_browser.setOnClickListener {
            //构造函数中指定ACTION是Intent.ACTION_VIEW,即用于打开浏览器
            val intent = Intent(Intent.ACTION_VIEW)
            //指定要打开的网址
            intent.data = Uri.parse("www.baidu")
            startActivity(intent)
        }
    }
}

事实上,我们也可以自己定义一个可以响应打开网页的Activity。
新建一个Activity,在AndroidManifest中修改其注册信息。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="schemas.android/apk/res/android"
    xmlns:tools="schemas.android/tools"
    package="">
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity2">
            <!--忽略警告-->
            <intent-filter tools:ignore = "APPLinkUrlError">
                <!--指定ACTION-->
                <action android:name="android.intent.action.VIEW"/>
                <!--指定Category-->
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="https"/>
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

实际开发中应该尽量避免响应自己实际无法实现的功能,毕竟用户体验太差了。

本文发布于:2024-09-21 01:40:00,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1726854009434855.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:实战   浏览器   隐式   系统   Android
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23