当前位置:   article > 正文

Android开发之隐示意图跳转_android 隐式意图 action打开activity

android 隐式意图 action打开activity

Activity之间的跳转分为隐式意图和显示意图。

1.显示意图

显示意图通过构造Intent方法来实现,需要传递两个参数,一个是当前的Activity对象,另一个是要启动的Activity对象。

代码如下:

  1. Intent intent = new Intent(Activity.this,Activity2.class);//创建Intent对象
  2. startActivity(intent);//开启Activity2

2.隐式意图

隐式意图有setAction()、setData()、setCategory()方法,这些方法在使用时都需要在清单文件AndroidManifest中,为Activity添加<intent-filter>标签.

setAction()设置Activity开启动作

java文件中

  1. Intent intent = new Intent();
  2. intent.setAction("cn.itcast.START_ACTIVITY");
  3. startActivity(intent);

AndroidManifest文件中

  1. <intent-filter>
  2. <action android:name="cn.itcast.START_ACTIVITY"/>
  3. <category android:name="android.intent.category.DEFAULT"/>
  4. </intent-filter>

setData()

data属性用来指定数据的URI或者数据MIME类型 。隐式Intent携带的data数据只要与IntentFilter中的任意一个data声明相同,data属性就匹配成功。

  1. <intent-filter>
  2. <data android:mimeType="video/mpeg" android:scheme="http......" />
  3. <data android:mimeType="audio/mpeg" android:scheme="http......" />
  4. ......
  5. </intent-filter>

setCatagory()

category属性用于为action添加额外信息,一个IntentFilter可以不声明category属性,也可以声明多个category属性。

  1. <intent-filter>
  2. <category android:name="android.intent.category.DEFAULT" />
  3. <category android:name="android.intent.category.BROWSABLE" />
  4. ......
  5. </intent-filter>

 简单实例代码——在ADV中打开百度界面

activity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/img"
  6. android:orientation="horizontal">
  7. <Button
  8. android:id="@+id/btn_baidu"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="15dp"
  12. android:layout_centerInParent="true"
  13. android:text="百度"
  14. android:textSize="20sp"/>
  15. </RelativeLayout>

MainActivity.java文件

  1. package com.example.openbrowser;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. public class MainActivity extends AppCompatActivity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. Button button1 = (Button) findViewById(R.id.btn_baidu);
  14. button1.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View view) {
  17. Intent intent = new Intent();
  18. intent.setAction("android.intent.action.VIEW");
  19. intent.setData(Uri.parse("http://baidu.com"));
  20. startActivity(intent);
  21. }
  22. });
  23. }
  24. }

 AndroidManifest.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.openbrowser">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/Theme.OpenBrowser">
  11. <activity
  12. android:name=".MainActivity"
  13. android:exported="true">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <action android:name="android.intent.action.VIEW" />
  17. <data android:mimeType="video/mpeg" android:scheme="http://baidu.com" />
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/236143?site
推荐阅读
相关标签
  

闽ICP备14008679号