Android基础之Intent

一、显式Intent

FirstActivity.java(SecondActivity.java与此几乎完全相同,只是变量名称不一样)

package com.example.intenttest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends AppCompatActivity {
    private Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        btn1=(Button)findViewById(R.id.button_1);
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //第一个参数Context要求提供启动活动的上下文,第二个参数Class指定想要启动的目标活动
                Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
                //专门用来启动意图的方法
                startActivity(intent);
            }
        });
    }
}

first_layout.xml(second_layout.xml也一样)

<?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"
>
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1"/>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intenttest">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SecondActivity">
        </activity>
        <activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

效果图:

在这里插入图片描述

二、隐式Intent

  1. 定义

    不明确的指出我们想要启动哪一个活动,而是指定一系列更为抽象的action和category等信息,然后交由系统去分析这个Intent,并帮我们找出合适的活动去启动

  2. 实例(改写上面的功能)

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.intenttest">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".SecondActivity">
                <intent-filter>
                    <!--指明SecondActivity界面可以接受com.example.activitytest.ACTION_START的Action-->
                    <action android:name="com.example.activitytest.ACTION_START"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                    <category android:name="CategoryTest"/>
                </intent-filter>
            </activity>
            <activity android:name=".FirstActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    MainActivity.java

    package com.example.intenttest;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    
    public class FirstActivity extends AppCompatActivity {
        private Button btn1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first_layout);
            btn1=(Button)findViewById(R.id.button_1);
            btn1.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    //隐式Intent
                    Intent intent=new Intent("com.example.activitytest.ACTION_START");
                    intent.addCategory("CategoryTest");
                    startActivity(intent);
                }
            });
        }
    }
    
    

    结果:(效果与上面完全相同)
    在这里插入图片描述

  • 注意:

    在MainActivity.java中添加了category后

    intent.addCategory("CategoryTest");
    

    要在AndroidManifest.xml进行注册,否则会导致程序崩溃

    <category android:name="CategoryTest"/>
    

    结果:
    在这里插入图片描述

三、更多隐式Intent的用法

1. 打开网页

只需要将主程序中的代码修改成:

     btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent=new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });

效果:调用系统浏览器打开百度

2. 调出拨号界面

   btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent=new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            }
        });

效果:
在这里插入图片描述

3. 设置活动可以响应的数据类型

在标签中配置一个标签

将该活动设置为响应所有http协议的intent

   <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
       <!--响应所有http协议的intent-->
                <data android:scheme="http"/>
   </intent-filter>

效果:
在这里插入图片描述

四、利用Intent传递数据

1. 向下一个活动传递数据

FirstActivity.java

     btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String data="Hello SecondActivity";//传递的数据
                Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("extra_data",data);//通过键值对的形式传递
                startActivity(intent);
            }
        });

SecondActivity.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Intent intent=getIntent();
        String data=intent.getStringExtra("extra_data");
        Log.d("SecondActivity",data);//通过键找到值然后打印
    }

结果:在SecondActivity的logcat下面发现已经接收到传来的信息
在这里插入图片描述

  1. 返回数据给上一个活动(待补充…)

    《第一行代码》P51

    看了一下,比向下传递复杂的多,而且感觉没有什么用,所以暂时放在这里,待补充…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值