赞
踩
Activity之间的跳转分为隐式意图和显示意图。
1.显示意图
显示意图通过构造Intent方法来实现,需要传递两个参数,一个是当前的Activity对象,另一个是要启动的Activity对象。
代码如下:
- Intent intent = new Intent(Activity.this,Activity2.class);//创建Intent对象
- startActivity(intent);//开启Activity2
2.隐式意图
隐式意图有setAction()、setData()、setCategory()方法,这些方法在使用时都需要在清单文件AndroidManifest中,为Activity添加<intent-filter>标签.
setAction()设置Activity开启动作
java文件中
- Intent intent = new Intent();
- intent.setAction("cn.itcast.START_ACTIVITY");
- startActivity(intent);
AndroidManifest文件中
- <intent-filter>
- <action android:name="cn.itcast.START_ACTIVITY"/>
- <category android:name="android.intent.category.DEFAULT"/>
- </intent-filter>
setData()
data属性用来指定数据的URI或者数据MIME类型 。隐式Intent携带的data数据只要与IntentFilter中的任意一个data声明相同,data属性就匹配成功。
- <intent-filter>
- <data android:mimeType="video/mpeg" android:scheme="http......" />
- <data android:mimeType="audio/mpeg" android:scheme="http......" />
- ......
- </intent-filter>
-
setCatagory()
category属性用于为action添加额外信息,一个IntentFilter可以不声明category属性,也可以声明多个category属性。
- <intent-filter>
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.BROWSABLE" />
- ......
- </intent-filter>
-
简单实例代码——在ADV中打开百度界面
activity_main.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/img"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btn_baidu"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="15dp"
- android:layout_centerInParent="true"
- android:text="百度"
- android:textSize="20sp"/>
-
- </RelativeLayout>

MainActivity.java文件
- package com.example.openbrowser;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button button1 = (Button) findViewById(R.id.btn_baidu);
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent();
- intent.setAction("android.intent.action.VIEW");
- intent.setData(Uri.parse("http://baidu.com"));
- startActivity(intent);
- }
- });
- }
- }

AndroidManifest.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.openbrowser">
-
- <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/Theme.OpenBrowser">
- <activity
- android:name=".MainActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <action android:name="android.intent.action.VIEW" />
- <data android:mimeType="video/mpeg" android:scheme="http://baidu.com" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest>

赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。