Android基础 ---- 意图与意图过滤器

一、意图

  • 意图是什么?

    • 意图就是你想做的事情
  • 意图的类型有哪几种?

    • 显示意图
    • 隐式意图
  • 隐式意图

    • 什么叫隐式意图?
      隐式意图指的是通过一组动作或者数据去完成想完成的事情
    • 隐式意图怎么用?
      现在有两个页面,从A页面跳转到B页面
      • (1) 在AndroidManifest.xml文件中进行设置

        • 其中action、category都可以自己定义。data中的scheme和mimeType也都可以自己定义
        <activity android:name=".TestActivity">
                    <intent-filter>
                        <!-- action 表示动作-->
                        <action android:name="com.example.chen.B"/>
                        <!-- category 表示类别-->
                        <category android:name="android.intent.category.DEFAULT"/>
                        <!--data 表示数据-->
                        <data android:scheme="chen"
                            android:mimeType="aa/bb"/>
                    </intent-filter>
        </activity>
        
      • (2) 在Activity类中设置意图

        public void click(View view) {
                Intent intent = new Intent();
                intent.setAction("com.example.chen.B");
                intent.addCategory("android.intent.category.DEFAULT");
                /*intent.setData(Uri.parse("chen:"));
                intent.setType("aa/bb");*/
                intent.setDataAndType(Uri.parse("chen:"),"aa/bb");
                startActivity(intent);
        }
        
  • 使用隐式意图注意事项:

    • 在Activity中设置的动作、类别、数据必须与intent-filter中配置的动作、类别、数据相同
    • 当data中既配置了scheme,又配置了mimeType时,在Activity中设置数据时只能使用setDataAndType()方法进行设置
    • 一个activity中可以有多个intent-filter。
      • 当activity中有多个intent-filter,Activity中的intent是根据匹配的action、category以及data来确定是与哪个页面对应的
  • 显示意图

    • 什么叫显示意图?
      通过指定具体的包名和类名去完成想完成的事情
    • 显示意图怎么用?
      • (1) 在AndroidManifest.xml文件中进行设置
        <activity android:name=".TestActivity"/>
        
      • (2) 在Activity中设置意图
        public void click(View view) {
        		/*
        		 *      参数一:上下文对象
        		 *      参数二:目的跳转界面的class文件
        		 * */
                Intent intent = new Intent(this,TestActivity.class);
                startActivity(intent);
        }
        
  • 什么时候用显示意图,什么时候用隐式意图?

    • 开启自己应用的界面时,用显示意图
    • 开启其他应用的界面时( 系统应用 ),用隐式意图
  • 显示意图与隐式意图哪个更安全?
    显示意图比隐式有更加安全,因为显示意图无法根据设置动作、数据、类别等方法进行直接访问


二、意图过滤器

  • 意图过滤器怎么表示?
    <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>
    
    其中<intent-filter>就是意图过滤器
  • 意图过滤器有什么作用?
    • 意图过滤器中可以设置action、category以及data属性
    • 意图过滤器常与隐式意图配合使用,实现页面间的逻辑



三、使用显示意图传输数据

  • 需求1:
    在这里插入图片描述
    • A页面界面编写
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
          <EditText
              android:id="@+id/et_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textSize="18sp"
              android:textColor="#000"
              android:hint="@string/name"/>
          <EditText
              android:id="@+id/et_age"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textSize="18sp"
              android:textColor="#000"
              android:hint="@string/age"
              android:layout_marginTop="5dp"/>
          <Button
              android:id="@+id/bt_next"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/next"
              android:textSize="18sp"
              android:textColor="#000"
              android:onClick="click"
              android:layout_marginTop="5dp"/>
      </LinearLayout>
      
    • A页面逻辑代码编写
      package com.example.chenzhaoyu.intentandactivitydemo;
      
      import android.content.Intent;
      import android.net.Uri;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.text.TextUtils;
      import android.view.View;
      import android.widget.EditText;
      import android.widget.RadioGroup;
      import android.widget.Toast;
      
      public class MainActivity extends AppCompatActivity {
      
          private EditText et_name;
          private EditText et_age;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              et_name = findViewById(R.id.et_name);
              et_age = findViewById(R.id.et_age);
          }
      
          public void click(View view) {
              String name = et_name.getText().toString().trim();
              String age = et_age.getText().toString().trim();
              Intent intent = new Intent(this,TestActivity.class);
              intent.putExtra("name",name);
              intent.putExtra("age",age);
              startActivity(intent);
          }
      }
      
      
    • B页面界面编写
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
          <TextView
              android:id="@+id/tv_name"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/test_txt"
              android:textSize="18sp"
              android:textColor="#000"/>
          <TextView
              android:id="@+id/tv_age"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/test_txt"
              android:textSize="18sp"
              android:textColor="#000"/>
      </LinearLayout>
      
    • B页面逻辑代码编写
      package com.example.chenzhaoyu.intentandactivitydemo;
      
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.widget.TextView;
      
      public class TestActivity extends Activity {
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_test);
              TextView tv_name = findViewById(R.id.tv_name);
              TextView tv_age = findViewById(R.id.tv_age);
              Intent intent = getIntent();
              String name = intent.getStringExtra("name");
              String age = intent.getStringExtra("age");
              tv_name.setText(name);
              tv_age.setText(age);
          }
      }
      
  • 总结数据传输步骤:
    • 在A页面中
      • 创建一个Intent意图对象
      • 通过putExtra()方法将数据绑定到intent中
        • 其中putExtra()底层是通过HashMap的方式进行存储的
    • 在B页面中
      • 首先通过getIntent()方法,获取到从A页面传过来的Intent对象
      • 通过getXxxExtra()方法获取对应的值即可
  • 需求2:
    在这里插入图片描述
    • A页面界面编写
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
      
              <EditText
                  android:id="@+id/et_phone"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="3"
                  android:textColor="#000"
                  android:textSize="18sp" />
      
              <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginStart="3dp"
                  android:layout_weight="1"
                  android:onClick="click"
                  android:text="@string/add"
                  android:textColor="#000"
                  android:textSize="26sp" />
          </LinearLayout>
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop="5dp"
              android:text="@string/content"
              android:textColor="#000"
              android:textSize="18sp" />
      </LinearLayout>
      
    • A页面逻辑代码编写
      package com.example.chenzhaoyu.intentandactivitydemo;
      
      import android.content.Intent;
      import android.support.annotation.Nullable;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.EditText;
      
      public class MainActivity extends AppCompatActivity {
      
          private EditText et_phone;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              et_phone = findViewById(R.id.et_phone);
          }
      
          public void click(View view) {
              Intent intent = new Intent(getApplicationContext(), TestActivity.class);
              startActivityForResult(intent, 1);
          }
      
          @Override
          protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
              if (resultCode == 2 && data != null) {
                  String phone = data.getStringExtra("phone");
                  et_phone.setText(phone);
              }
          }
      }
      
    • B页面界面编写
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
          <ListView
              android:id="@+id/lv_content"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          </ListView>
      </LinearLayout>
      
    • B页面逻辑代码编写
      package com.example.chenzhaoyu.intentandactivitydemo;
      
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.AdapterView;
      import android.widget.BaseAdapter;
      import android.widget.ListView;
      import android.widget.TextView;
      
      import java.util.ArrayList;
      import java.util.List;
      
      public class TestActivity extends Activity {
      
          private List<Person> personList;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_test);
              // [1] 找到我们需要的控件
              ListView lv_content = findViewById(R.id.lv_content);
              // [2.1] 首先我们需要得到数据
              personList = new ArrayList<>();
              for (int i = 0; i < 10; i++) {
                  Person person = new Person();
                  person.setName("李四" + i);
                  person.setPhone("111" + i);
                  personList.add(person);
              }
              // [2.2] 设置适配器
              lv_content.setAdapter(new MyAdapter());
              // [2.3] 将ListView中某个点击的子项目数据返回到A页面中
              lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                      Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                      intent.putExtra("phone",personList.get(position).getPhone());
                      setResult(2,intent);
                      finish();
                  }
              });
          }
      
          class MyAdapter extends BaseAdapter {
      
              @Override
              public int getCount() {
                  return personList.size();
              }
      
              @Override
              public Object getItem(int position) {
                  return null;
              }
      
              @Override
              public long getItemId(int position) {
                  return 0;
              }
      
              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                  View view;
                  if (convertView == null) {
                      view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test_item, null);
                  } else {
                      view = convertView;
                  }
                  TextView tv_name = view.findViewById(R.id.tv_name);
                  TextView tv_phone = view.findViewById(R.id.tv_phone);
                  tv_name.setText(personList.get(position).getName());
                  tv_phone.setText(personList.get(position).getPhone());
                  return view;
              }
          }
      }
      
    • ListView控件布局界面编写
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
          <TextView
              android:id="@+id/tv_name"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="wrap_content"
              android:textSize="18sp"
              android:textColor="#000"
              android:gravity="center"/>
          <TextView
              android:id="@+id/tv_phone"
              android:layout_width="0dp"
              android:layout_weight="1"
              android:layout_height="wrap_content"
              android:textSize="18sp"
              android:textColor="#000"
              android:gravity="start" />
      </LinearLayout>
      
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值