Android studio--Intent 介绍

一:Intent 介绍
Intent中文翻译为 “意图”,它是Android程序中各个组件进行交互的一种重要方式,它不仅可以指定当前组件要执行的 动作,还可以在不同组件之间进行 数据传递。
– Intent一般用于启动Activity、启动服务、发送广播等,承担了Android应用程序三大核心组件相互间的通信功能。
Intent启动Activity的方法有:
• startActivity (Intent intent )
• startActivityForResult (Intent intent,int code )
二:显式意图和隐式意图
–  显式意图
• 即在通过Intent启动Activity时,需明确指定激活组件的名称。如果需要在本应用中启动他 其他的Activity时,可以使用显式意图来启动Activity。
Intent intent = new Intent(this, Activity02.class);
startActivity(intent);
• 显式意图还可以根据目标组件的包名、全路径名来指定开启组
intent.setClassName("cn.itcast.xxx","cn.itcast.xxx.xxxx");
startActivity(intent);
- 隐图 式意图
• 没有明确指定组件名的Intent称为隐式意图。系统会根据隐式意图中设置的动作(action)、类别(category)、数据(Uri和数据类型)找到最合适的组件。
<activity android:name="com.itcast.intent.Activity02">
    <intent-filter>
        <action android:name="cn.itscast.xxx"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>
使用隐式意图开启Activity的示例代码如下所示:
Intent intent = new Intent();
// 设置动作和清单文件一样
intent.setAction("cn.itscast.xxx");
startActivity(intent);

在实际开发中,避免不了要调用其他应用程序的组件。例如,在开发新浪微博时,需要启动系统的相机功能。– 接下来通过打开系统照相机的案例向大家演示如何使用隐式意图。首先,需要创建一个button,在activity_main.xml中添加居中按钮后,在MainActivity.java中链接其ID,并且点击时做出反应,然后再创建一个模拟的摄像头页面(新建activity01.xml),创建activity01.Java,最后需要在Manifest.xml中注册activity01这个activity.

activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <Button
        android:id="@+id/openCamera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="打开照相机"
        android:textSize="30sp" />
</RelativeLayout>

MainActivity.java代码

package com.example.yuekang.demo03_;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
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 openCamera=(Button) findViewById(R.id.openCamera);//获取button ,findViewByID 得到的是一个View类型,因此要强制转化
        //添加事件监听,点击button的时候要出现一个效果
        openCamera.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                Intent intent=new Intent();
                intent.setAction("android.media.action.IMAGE_CAPTURE");//隐式意图,Action的名字是固定好的
                intent.addCategory("android.intent.category.DEFAULT");//设置一下类别
                //打开页面
                startActivity(intent);
            }


        });

    }
}

activity01.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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="你可以把我当做摄像头"
        android:textSize="30sp" />
</RelativeLayout>

activity01.Java代码

package com.example.yuekang.demo03_;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;

public class Activity01 extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {//需要把页面打开
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity01);//下一步需要在Manifest.xml中注册这一个Activity01
    }
}

Manifest.xml代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yuekang.demo03_">

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Activity01">
            <intent-filter>
                <action android:name="android.media.action.IMAGE_CAPTURE" />
                <!--将MainActivity.xml中的action name和category name 复制粘贴在这里必须与MainActivity.xml,这是因为启动时顺利-->
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

运行程序如下:


猜你喜欢

转载自blog.csdn.net/huanhuan59/article/details/80094642