zoukankan      html  css  js  c++  java
  • Android基础——隐式intent和intent过滤器

    当不直接在intent中指定组件名时,就是隐式使用intent,此时要配合过滤器来使用,找到目标的组件

    注册文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myintentiii">
    
        <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=".DetailActivity">
                <intent-filter>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </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>

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示图片" />
    
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".DetailActivity">
    
        <ImageView
            android:id="@+id/image"
            android:src="@drawable/b"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
    </LinearLayout>

    java代码

    package com.example.myintentiii;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    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 button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    // 隐式创建Intent,由于不知道调用的是谁,
                    // 所以对应的要给Detail增加intent过滤器
                    Intent intent = new Intent();
                    intent.setAction(intent.ACTION_VIEW);
                    startActivity(intent);
                }
            });
        }
    }
  • 相关阅读:
    【学相伴】Nginx最新教程通俗易懂-狂神说
    Linux基础知识总结(命令行)
    CentOS7 运维
    Linux 的基础知识回顾(安装vmware) ---- No.1 后面都以Centos8 为例
    Linux sudo权限提升漏洞(CVE-2021-3156)
    Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    矩阵的范数
    函数导出在kvm_intel.ko,kvm.ko不共享
    python 调用内部类的两种方法
    python3 字符串方法
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12237769.html
Copyright © 2011-2022 走看看