android之Intent的七大属性

简介:

    Intent用于封装程序的“调用意图”。两个Activity之间,可以把需要交换的数据,封装成Bundle对象,然后使用Intent对象,携带数据到另一个Activity中。实现两个Activity的数据交换。

    Intent还是各种应用程序组件之间通信的重要媒介。启动ActivityServiceBroadcastReceiver,都需要使用Intent

    Intent还有一个好处,如果应用程序只是想启动具有某种特征的组件,并不想和某个具体的组件耦合,则可以通过在int ent - filt er 中配置相应的属性进行处理,与s t uc t s 2 中的MVC框架思路类似

    Intent对象大致包括7大属性:1Component Name2Action3Catory4data5Type6Extra7Flag

    ·Action作为标识符,代表一个Intent ,当一个Activity 需要外部协助处理时,就会发出一个Intent,如果一个程序能完成相应功能,只要在int ent - filt er 加上这个这个intent 就可以了。

    ·Data保存需要传递的数据格式,比如:tel://

    ·Extras保存需要传递的额外数据。

    ·Category 表示Intent的种类,从android上启动Activity 有多种方式,比如 程序列表、桌面图标、点击Home激活的桌面等等,Category则用来标识这些Activity的图标会出现在哪些启动的上下文环境里。

(一)  ComponentName:明确指定Intent将要启动哪个组件,因此这种Intent被称为显示Intent,没有指定ComponentName属性的Intent被称为隐式Intent。隐式Intent没有明确要启动哪个组件,应用汇根据Intent指定的规则去启动符合条件的组件。ComponentName不仅可以启动本程序中的activity,还可以启动其它程序的activity

启动方式:

Intent intent =new Intent();

ComponentNamecomponent= new ComponentName(this, TwoActivity.class);

intent.setComponent(component);

等价于:

Intent intent =new Intent(this, TwoActivity.class);

startActivity(intent);

等价于:

Intent intent =new Intent();

intent.setClass(this,TwoActivity.class);

startActivity(intent);

等价于:

Intent intent =new Intent();

intent.setClassName(this,"com.zzh.day06_intent.TwoActivity");

startActivity(intent);

本程序中启动其它程序:

 

(二) ActionCategory 属性与intent-filter配置:

通常,Action, Category属性结合使用。定义这两个属性都是在主配置文件的<intent-filter>节点中。Intent通过定义Action属性(其实就是定义一段自定义的字符串),这样就可以把Intent与具体的某个Activity分离,实现了解耦。否则,每次跳转都有写成,

(三) data

1、 用于添加数据。通常是启动某个系统程序或其他程序,带给此程序的信息。Data属性通常用于向Action属性提供操作的数据。Data属性的值是个Uri对象。

Uri的格式如下scheme://host:port /path

2、 系统内置的属性常量

tel:是固定写法,是系统内置的属性常量。

系统内置的几个Dat a属性常量:

·tel: //:号码数据格式,后跟电话号码。

·mailto: //:邮件数据格式,后跟邮件收件人地址。

·smsto: //:短息数据格式,后跟短信接收号码。

·content : //:内容数据格式,后跟需要读取的内容。

·file://:文件数据格式,后跟文件路径。

·market://search?q=pname:pkgname:市场数据格式,在Google Market里搜索包名为pkgname的应用。

·geo: //latitude, longitude:经纬数据格式,在地图上显示经纬度所指定的位置。

四、Intent利用Action属性和Dat a属性启动Android系统内置组件的代码

(一)、拨打电话:

Intent intent=new Intent();

intent.setAction(Intent.ACTION_CALL);

//intent.setAction("android.intent.action.CALL");  //以下各项皆如此,都有两种写法。

intent.setData(Uri.parse("tel:1320010001"));

startActivity(intent);

//调用拨号面板:

Intent intent=new Intent();

intent.setAction(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:1320010001"));

startActivity(intent);

//调用拨号面板:

Intent intent=new Intent();

intent.setAction(I ntent.ACTION_VIEW);

intent.setData(Uri.parse("tel:1320010001"));

startActivity(intent);

(二)、利用Uri打开浏览器、打开地图等:

Uri uri = Uri.parse("https://www.baidu.com");//浏览器

Uriuri=Uri.parse("geo:39.899533,116.036476"); //打开地图定位

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setData(uri);

startActivity(intent);

()Type属性

1Type属性用于指定Data所指定的Uri对应的MIME类型。MIME只要符合“abc /xyz”这样的字符串格式即可。

2Intent利用ActionDataType属性启动Android系统内置组件的代码:

播放视频:

Intent intent = new Intent();

Uri uri =Uri.parse("file:///sdcard/media.mp4");

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(uri,"video/*");

startActivity(intent);

()Extra属性

1、通过intent.putExtra()的形式在多个Activity之间进行数据交换。

2、系统内置的几个Extra常量

EXTRA_BCC:存放邮件密送人地址的字符串数组。

EXTRA_CC:存放邮件抄送人地址的字符串数组。

EXTRA_EMAIL :存放邮件地址的字符串数组。

EXTRA_SUBJECT:存放邮件主题字符串。

EXTRA_TEXT:存放邮件内容。

EXTRA_KEY_EVENT:以KeyEvent对象方式存放触发Intent 的按键。

EXTRA_PHONE_ NUMBER :存放调用ACTION_CALL 时的电话号码。

Intent 利用ActionDataTypeExtra属性启动Android系统内置组件的代码:

调用发送短信的程序

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setType("vnd.android-dir/mms-sms");

intent.putEx tra("sms_body","信息内容...");

startActivity(intent);

//发送短信息

Uri uri =Uri.parse("smsto:13200100001");

Intent intent = new Intent();

intent.setAction(Intent.ACTION_SENDTO);

intent.setData(uri);

intent.putEx tra("sms_body","信息内容...");

startActivity( intent );

//发送彩信,设备会提示选择合适的程序发送

Uri uri = Uri.parse("content://media/external/images/media/23"); //设备中的资源(图像或其他资源)

Intent intent = new Intent();

intent.setAction(Intent. ACTION_SEND );

intent.setType("image/png");

intent.putExtra("sms_body","内容");

intent.putExtra(Intent.EX TRA_STREAM,uri);

startActivity(it);

发送Email

Intent intent=new Intent();

intent.setAction(I ntent.ACTION_SEND);

String[]tos={"android1@163.com"}

String[]ccs={"you@yahoo.com"};

intent.putExtra(Intent.EXTRA_EMAIL, tos);

intent.putExtra(Intent.EXTRA_CC, ccs);

intent.putExtra(Intent.EXTRA_TEX T,"The email body text");

intent.putExtra(Intent.EXTRA_SU BJ ECT,"The email subject text");

intent.setType("message/rfc822");

startActivity(Intent.createChooser(intent,"Choose Email Client"));

Intent intent = newIntent(Intent.ACTION_SEND);

String[] tos = {"mobileservice@ablesky.com"};

intent.putExtra(I ntent.EXTRA_EMAIL, tos);

intent.putExtra(I ntent.EXTRA_TEXT,getPhoneParameter());

intent.putExtra(Intent.EXTRA_SUBJECT,"Android日志");

intent.putEx tra(Intent.EXTRA_STREAM, Uri.fromFile(cacheDir));

intent.setType("message/rfc882");

intent.setType("plain/text");

Intent.createChooser(intent, "请选择邮件发送软件");

startActivity(intent);

intent.setAction(android.provider.Settings.ACTION_SETTINGS)


Intent利用Action属性中的ACTION_GET_CONTENT获取返回值

//选择图片requestCode返回的标识

Intent intent = new I ntent();

intent.setAction(I ntent.ACTION_GET_CONTENT );

intent.setType( "image/* " );

Intent wrapperIntent = Intent.createChooser(intent, null);

startActivityForResult(wrapperIntent,requestCode);

//添加音频

Intent intent = new Intent();

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setType( "video/* " );

Intent wrapperIntent = Intent.createChooser(intent, null);

startActivityForResult(wrapperIntent,requestCode);

//视频

Intent intent = new Intent();

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setType( "video/* ");

Intent wrapperIntent = Intent.createChooser(intent, null);

startActivityForResult(wrapperI ntent,requestCode);

//录音

Intent intent = new Intent();

intent.setAction(Intent.ACTION_GET_CONTENT );

intent.setType( "audio/amr" );

intent.setClassName("com.android.soundrecorder","com.android.soundrecorder.SoundRecorder");

startActivityForResult(intent, requestCode);

(七)、Flags 属性:Intent 可调用addFlags()方法来为Intent 添加控制标记。【重要】

FLAG_ ACTIVITY_CLEAR_TOP:(效果同Activity LaunchModesingleTask

如果在栈中已经有该Activity的实例,就重用该实例。重用时,会让该实例回到栈顶,因此在它上面的实例将会被移除栈。如果栈中不存在该实例,将会创建新的实例放入栈中。

FLAG_ACTIVITY_SINGLE_TOP:(效果同Activity L aunchModesingleTop

如果在任务的栈顶正好存在该Activity的实例, 就重用该实例,而不会创建新的Activity 对象。

FLAG_ ACTIVITY_NEW_TASK:

【备注:】以下几个为了解。

FLAG_ACTIVITY_MULTIPLE_TASK:

FLAG_ACTIVITY_BROUGHT_TO_FRONT:

FLAG_ACTIVITY_RESET_TASK_IF_NEEDED:

示例代码:

Intent intent = new Intent(this,MainActivity.class);

//Activity栈中处于MainActivity主页面之上的Activity都弹出。

intent.setFlags(Intent.FLAG_ACTI VI TY_CLEAR_TOP);

startActivity(intent);

例如:

如果依次启动了四个Activity ABD

D Activity 里,跳到B Activity,同时希望Dfinish掉,可以在startActivity (intent )里的intent 里添加flags标记,如下所示:

Intent intent = new Intent(this , B.class);   

intent .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP); 

s t ar t Activity(intent );

这样启动B  Activity的同时,就会把Dfinis hed掉。

如果B  ActivitylaunchMode是默认的“st andar d”,则B  Activity会首先finis hed掉旧的B页面,再启动一个新的Activity   B如果不想重新再创建一个新的B  Activity,而是重用之前的B  Activity,可以将B  ActivitylaunchMode设置为“singleTask”。【特别需要注意的是:在部分手机中,如三星手机。即便是singleTask也会产生新的页面,而不是重用之前的页面。】


四、利用Intent 属性调用系统app的示例代码:

1、布局核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
< ScrollView
       android:id = "@+id/ScrollView1"
       android:layout_width = "match_parent"
       android:layout_height = "wrap_content"  >
       < LinearLayout
           android:layout_width = "match_parent"
           android:layout_height = "match_parent"
           android:orientation = "vertical"  >
 
           < Button
               android:id = "@+id/Button_main_call "
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 直接拨号"  />
 
           < Button
               android:id = "@+id/Button_main_dial"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 启动拨号面板"  />
 
           < Button
               android:id = "@+id/Button_main_dialer"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = "显示拨号面板"  />
 
           < Button
               android:id = "@+id/Button_main_sms"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton "
               android:text = "发送短信"  />
 
           < Button
               android:id = "@+id/Button_main_setting"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton "
               android:text = "系统设置"  />
 
           < Button
               android:id = "@+id/Button_main_datesetting"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 日期设置"  />
 
           < Button
               android:id = "@+id/Button_main_soundsetting"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 声音设置"  />
 
           < Button
               android:id = "@+id/Button_main_wifisetting"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " W I F I 设置"  />
 
           < Button
               android:id = "@+id/Button_main_web"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 浏览网页"  />
 
           < Button
               android:id = "@+id/Button_main_contacts"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 查看联系人"  />
 
           < Button
               android:id = "@+id/Button_main_showimage"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 查看图片"  />
 
           < Button
               android:id = "@+id/Button_main_showtext"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 查看文本"  />
 
           < Button
               android:id = "@+id/Button_main_playvideo"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 播放视频"  />
 
           < Button
               android:id = "@+id/Button_main_playaudio"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " 播放音频"  />
 
           < Button
               android:id = "@+id/Button_main_home"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:onClick = "clickButton"
               android:text = " H O M E "  />
       </ LinearLayout >
   </ ScrollView >

java核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public  class  MainActivity  extends  Activity
{
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.a);
     }
 
     public  void  clickButton(View view)
     {
         Intent intent =  new  Intent();
         // android.content.intent.ACTION_VIEW
         intent.setAction(android.content.Intent.ACTION_VIEW);
         switch  (view.getId())
         {
         case  R.id.Button_main_call:
             intent.setAction(android.content.Intent.ACTION_CALL);
             intent.setData(Uri.parse( "tel:10086" ));
             break ;
         case  R.id.Button_main_dial:
             intent.setAction(android.content.Intent.ACTION_DIAL);
             intent.setData(Uri.parse( "tel:10086" ));
             break ;
         case  R.id.Button_main_dialer:
             intent.setAction( "com.android.phone.action.TOUCH_DIALER" );
             break ;
         case  R.id.Button_main_sms:
             intent.setAction(android.content.Intent.ACTION_SENDTO);
             intent.setData(Uri.parse( "smsto:10086" ));
             intent.putExtra( "sms_body" "该吃饭了,下课吧!" );
             break ;
         case  R.id.Button_main_setting:
             intent.setAction( "android.settings.settings" );
             break ;
         case  R.id.Button_main_datesetting:
             intent.setAction( "android.settings.DATE_settingS" );
             break ;
         case  R.id.Button_main_soundsetting:
             intent.setAction( "android.settings.SOUND_settingS" );
             break ;
         case  R.id.Button_main_wifisetting:
             intent.setAction( "android.settings.WIFI_settings" );
             break ;
         case  R.id.Button_main_contacts:
             intent.setAction( "com.android.contacts.action.LIST_contacts" );
             break ;
         case  R.id.Button_main_web:
             intent.setAction(android.content.Intent.ACTION_VIEW);
             intent.setData(Uri
                     .parse( "http://www.baidu.com" ));
             break ;
         case  R.id.Button_main_showimage:
             intent.setAction(android.content.Intent.ACTION_VIEW);
             intent.setDataAndType(
                     Uri.fromFile( new  File(
                             "mnt/sdcard/Download/landscape.jpg" )),
                     "image/*" );
             break ;
         case  R.id.Button_main_showtext:
             intent.setAction(android.content.Intent.ACTION_VIEW);
             intent.setDataAndType(
                     Uri.fromFile( new  File(
                             "mnt/sdcard/Download/info.txt" )),
                     "text/*" );
             break ;
         case  R.id.Button_main_playaudio:
             intent.setAction(android.content.Intent.ACTION_VIEW);
             intent.setDataAndType(
                     Uri.fromFile( new  File(
                             "mnt/sdcard/Download/heavencity.mp3" )),
                     "audio/*" );
             break ;
         case  R.id.Button_main_playvideo:
             intent.setAction(android.content.Intent.ACTION_VIEW);
             intent.setDataAndType(
                     Uri.fromFile( new  File(
                             "mnt/sdcard/Download/girl.3gp" )),
                     "video/*" );
             break ;
         case  R.id.Button_main_home:
             intent.setAction( "android.intent.action.main" );
             intent.addCategory( "android.intent.category.HOME" );
             break ;
         default :
             break ;
         }
         startActivity(intent);
     }
}



本文转自 墨宇hz 51CTO博客,原文链接:http://blog.51cto.com/zzhhz/1627818

相关文章
|
10月前
|
Shell API Android开发
android queries属性
android queries属性
642 2
|
10月前
|
Android开发 开发者
Android基础知识:什么是Intent?有哪些类型的Intent?
Android基础知识:什么是Intent?有哪些类型的Intent?
612 0
|
10月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
174 1
|
存储 SQL 人工智能
Android Activity启动流程一:从Intent到Activity创建
Android Activity启动流程一:从Intent到Activity创建
|
5月前
|
存储 大数据 数据库
Android经典面试题之Intent传递数据大小为什么限制是1M?
在 Android 中,使用 Intent 传递数据时存在约 1MB 的大小限制,这是由于 Binder 机制的事务缓冲区限制、Intent 的设计初衷以及内存消耗和性能问题所致。推荐使用文件存储、SharedPreferences、数据库存储或 ContentProvider 等方式传递大数据。
211 0
|
7月前
|
Android开发
AutoX——当Android中clickable属性显示为false,实际可点击的布局如何处理
AutoX——当Android中clickable属性显示为false,实际可点击的布局如何处理
98 0
|
7月前
|
Java Android开发 UED
安卓scheme_url调端:如果手机上多个app都注册了 http或者https 的 intent。 调端的时候,调起哪个app呢?
当多个Android应用注册了相同的URL Scheme(如http或https)时,系统会在尝试打开这类链接时展示一个选择对话框,让用户挑选偏好应用。若用户选择“始终”使用某个应用,则后续相同链接将直接由该应用处理,无需再次选择。本文以App A与App B为例,展示了如何在`AndroidManifest.xml`中配置对http与https的支持,并提供了从其他应用发起调用的示例代码。此外,还讨论了如何在系统设置中管理这些默认应用选择,以及建议开发者为避免冲突应注册更独特的Scheme。
|
10月前
|
Java Android开发 C++
Android源代码定制:MK文件执行顺序|属性覆盖
Android源代码定制:MK文件执行顺序|属性覆盖
632 2
Android源代码定制:MK文件执行顺序|属性覆盖
|
10月前
|
缓存 安全 Java
Android中的persistent属性
Android中的persistent属性
457 2
|
10月前
|
定位技术 Android开发
Intent在Android中的几种用法
Intent在Android中的几种用法
144 1

热门文章

最新文章