Android3 Intent和IntentFilter

本文详细介绍了Android中Intent的使用方法,包括ComponentName、Action、Data、Category及Extra等关键概念,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Intent意思为意图,就是下一步要做的事情。

包括

1

ComponentName:对象用于表示唯一的长须应用组件,指明了期望的intent组件,即下一步要执行的组件。是activity转换的一种方法。

2

Action:实际上是intent所触发动作名称的字符串。

3

Data:主要是对intent消息中的数据惊醒封装,病描述intent的Action所操作数据的url(资源定位符)以及类型(根据格式区分类型),这样就能通过相对应的程序打开这个data

4

Category:对目标组件类信息的描述(比如对activity属性的描述)

5

Extra:中封装了一些附加的i型你先,这些信息已键值对的形式存在,intent可以通过putExtras()和Gerextras()来储存和获取信息。

一下为相应用法。

 

package com.example.inte;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View V){

Intent intent=new Intent();
ComponentName cName=new ComponentName("com.example.inte",
"com.example.inte.OneActivity"
);
intent.setComponent(cName);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener(){
public void onClick(View V){

Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri=Uri.parse("http://www.baidu.com/");
intent.setData(uri);
startActivity(intent);
}
});
button3.setOnClickListener(new OnClickListener(){
public void onClick(View V){

Intent intent=new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
});
button4.setOnClickListener(new OnClickListener(){
public void onClick(View V){

String recevier = "1065194059@qq.com";
String ccStrings = "113654201@163.com";
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_EMAIL, recevier);
intent.putExtra(Intent.EXTRA_CC, ccStrings);
intent.putExtra(Intent.EXTRA_SUBJECT, "Theme");
intent.putExtra(Intent.EXTRA_TEXT, "This is a test email");
intent.setType("text/plain");
startActivity(intent);
}
});
}
}

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="79dp"
android:layout_toRightOf="@+id/textView1"
android:text="Button" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="32dp"
android:text="Button2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="Button3" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button3"
android:layout_below="@+id/button3"
android:text="Button4" />

</RelativeLayout>

转载于:https://www.cnblogs.com/hitxx/p/4501730.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值