zoukankan      html  css  js  c++  java
  • Intent组件

    Android页面之间的跳转可以分为几类,比如有A和B两个页面,从A页面直接跳转到B页面,这样的跳转比较简单,或者从A页面跳转到B页面但是需要传送数据。在Android中,页面的跳转离不开Intent。Intent被称为Android页面之间联系的,他能在程序运行过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来完成请求。Intent主要包含的内容如下:

    1.Action

    用来指明要实施的动作是什么,比如说ACTION_VIEW, ACTION_EDIT等。

    一些常用的Action:


    ACTION_CALL activity 启动一个电话.
    ACTION_EDIT activity 显示用户编辑的数据.
    ACTION_MAIN activity 作为Task中第一个Activity启动
    ACTION_SYNC activity 同步手机与数据服务器上的数据.
    ACTION_BATTERY_LOW broadcast receiver 电池电量过低警告.
    ACTION_HEADSET_PLUG broadcast receiver 插拔耳机警告
    ACTION_SCREEN_ON broadcast receiver 屏幕变亮警告.
    ACTION_TIMEZONE_CHANGED broadcast receiver 改变时区警告.

    2.Data

    Data也就是要执行操作的数据。Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content: URIs。

    3.Type

    type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

    4.Categpry

    一个字符串,包含了关于处理该intent的组件的种类的信息。一个intent对象可以有任意个category。intent类定义了许多category常数.

    addCategory()方法为一个intent对象增加一个category,
    removeCategory删除一个category,
    getCategories()获取intent所有的category.

    5.Component

    component(组件),指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

    6.Extras

    extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

    Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有IntentFilter及其中定义的Intent,最终找到匹配的Intent。在这个解析过程中,Android是通过Intent的action、type、category这三个属性来进行判断的,判断方法如下:

    • 如果Intent指明定了action,则目标组件的IntentFilter的action列表中就必须包含有这个action,否则不能匹配;
    • 如果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。
    • 如果Intent中的数据不是content: 类型的URI,而且Intent也没有明确指定它的type,将根据Intent中数据的scheme (比如 http: 或者mailto:) 进行匹配。同上,Intent 的scheme必须出现在目标组件的scheme列表中。
    • 如果Intent指定了一个或多个category,这些类别必须全部出现在组建的类别列表中。比如Intent中包含了两个类别:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目标组件必须至少包含这两个类别。
     1 <activity android:name="NotesList" android:label="@string/title_notes_list">
     2             <intent-filter>
     3                 <action android:name="android.intent.action.MAIN" />
     4                 <category android:name="android.intent.category.LAUNCHER" />
     5             </intent-filter>
     6             <intent-filter>
     7                 <action android:name="android.intent.action.VIEW" />
     8                 <action android:name="android.intent.action.EDIT" />
     9                 <action android:name="android.intent.action.PICK" />
    10                 <category android:name="android.intent.category.DEFAULT" />
    11                 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
    12             </intent-filter>
    13             <intent-filter>
    14                 <action android:name="android.intent.action.GET_CONTENT" />
    15                 <category android:name="android.intent.category.DEFAULT" />
    16                 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
    17             </intent-filter>
    18         </activity>
    Intent-filter

    Intent使用的实例:

    Intent向activity传送数据,实现了一个页面向另外一个页面传送数据。

    1                  Intent intent=new Intent();
    2             intent.setClass(MainActivity.this,NextActivity.class);
    3             intent.putExtra("MyTest", myEdit.getText().toString());
    4             startActivity(intent);
    Intent传送数据

    另外一个页面接收数据:

    1 textView=(TextView)findViewById(R.id.textview2);
    2         Bundle bundel=this.getIntent().getExtras();
    3         textView.setText(bundel.getString("MyTest"));
    接收数据

    这个接收数据也是同Bundle类来实现,和键值对有点相似。

    接收下一个页面返回来的数据

    1 Intent intent=new Intent();
    2             intent.setClass(MainActivity.this,NextActivity.class);
    3             intent.putExtra("MyTest", myEdit.getText().toString());
    4              startActivityForResult(intent, REQUEST_CODE);
    跳转到下一页
     1  protected void onActivityResult(int requestCode, int resultCode, Intent data){
     2         super.onActivityResult(requestCode, resultCode, data);
     3          if (requestCode == REQUEST_CODE) {
     4               if (resultCode == NextActivity.RUSULT_CODE_1)
     5               {
     6             String temp=null;
     7             Bundle bundle=data.getExtras();
     8             if(bundle!=null)   temp=bundle.getString("name");
     9             setTitle(temp);
    10               }
    11        }
    12     }
    前一页执行的代码

    返回来的数据:

    1 Intent intent=getIntent();
    2                 Bundle bundle2=new Bundle();
    3                 bundle2.putString("name", "123456");
    4                 intent.putExtras(bundle2);
    5                  setResult(RUSULT_CODE_1,intent);
    向上一页返回数据

    下面是转载来的其他的一些Intent用法实例(转自javaeye)

    显示网页
       1. Uri uri = Uri.parse("http://google.com");  
       2. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
       3. startActivity(it);

    显示地图
       1. Uri uri = Uri.parse("geo:38.899533,-77.036476");  
       2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
       3. startActivity(it);   
       4. //其他 geo URI 範例  
       5. //geo:latitude,longitude  
       6. //geo:latitude,longitude?z=zoom  
       7. //geo:0,0?q=my+street+address  
       8. //geo:0,0?q=business+near+city  
       9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom

    路径规划
       1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");  
       2. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
       3. startActivity(it);  
       4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456 

    打电话
       1. //叫出拨号程序 
       2. Uri uri = Uri.parse("tel:0800000123");  
       3. Intent it = new Intent(Intent.ACTION_DIAL, uri);  
       4. startActivity(it);  
       1. //直接打电话出去  
       2. Uri uri = Uri.parse("tel:0800000123");  
       3. Intent it = new Intent(Intent.ACTION_CALL, uri);  
       4. startActivity(it);  
       5. //用這個,要在 AndroidManifest.xml 中,加上  
       6. //<uses-permission id="android.permission.CALL_PHONE" /> 

    传送SMS/MMS
       1. //调用短信程序 
       2. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
       3. it.putExtra("sms_body", "The SMS text");   
       4. it.setType("vnd.android-dir/mms-sms");  
       5. startActivity(it); 
       1. //传送消息 
       2. Uri uri = Uri.parse("smsto://0800000123");  
       3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
       4. it.putExtra("sms_body", "The SMS text");  
       5. startActivity(it); 
       1. //传送 MMS  
       2. Uri uri = Uri.parse("content://media/external/images/media/23");  
       3. Intent it = new Intent(Intent.ACTION_SEND);   
       4. it.putExtra("sms_body", "some text");   
       5. it.putExtra(Intent.EXTRA_STREAM, uri);  
       6. it.setType("image/png");   
       7. startActivity(it); 

    传送 Email
       1. Uri uri = Uri.parse("mailto:xxx@abc.com");  
       2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
       3. startActivity(it); 


       1. Intent it = new Intent(Intent.ACTION_SEND);  
       2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");  
       3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");  
       4. it.setType("text/plain");  
       5. startActivity(Intent.createChooser(it, "Choose Email Client")); 


       1. Intent it=new Intent(Intent.ACTION_SEND);    
       2. String[] tos={"me@abc.com"};    
       3. String[] ccs={"you@abc.com"};    
       4. it.putExtra(Intent.EXTRA_EMAIL, tos);    
       5. it.putExtra(Intent.EXTRA_CC, ccs);    
       6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");    
       7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");    
       8. it.setType("message/rfc822");    
       9. startActivity(Intent.createChooser(it, "Choose Email Client"));


       1. //传送附件
       2. Intent it = new Intent(Intent.ACTION_SEND);  
       3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
       4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");  
       5. sendIntent.setType("audio/mp3");  
       6. startActivity(Intent.createChooser(it, "Choose Email Client"));

    播放多媒体
           Uri uri = Uri.parse("file:///sdcard/song.mp3");  
           Intent it = new Intent(Intent.ACTION_VIEW, uri);  
           it.setType("audio/mp3");  
           startActivity(it); 
           Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");  
           Intent it = new Intent(Intent.ACTION_VIEW, uri);  
           startActivity(it);

    Market 相关
    1.        //寻找某个应用 
    2.        Uri uri = Uri.parse("market://search?q=pname:pkg_name"); 
    3.        Intent it = new Intent(Intent.ACTION_VIEW, uri);  
    4.        startActivity(it);  
    5.        //where pkg_name is the full package path for an application 
    1.        //显示某个应用的相关信息 
    2.        Uri uri = Uri.parse("market://details?id=app_id");  
    3.        Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    4.        startActivity(it);  
    5.        //where app_id is the application ID, find the ID   
    6.        //by clicking on your application on Market home   
    7.        //page, and notice the ID from the address bar

    Uninstall 应用程序
    1.        Uri uri = Uri.fromParts("package", strPackageName, null); 
    2.        Intent it = new Intent(Intent.ACTION_DELETE, uri);   
    3.        startActivity(it); 

  • 相关阅读:
    《人月神话》读书笔记二
    正则表达式进阶篇
    Zend Framework学习之自定义认证适配器
    Zend Framework学习之常用校验器
    Zend Framework学习之Zend_Mail
    《人月神话》读书笔记一
    Zend Framework学习之校验器Zend_Validate
    我的第一个C++程序
    Zend Framework学习之Zend_Filter_StripTags
    《黑客与画家》读书笔记三(完结)
  • 原文地址:https://www.cnblogs.com/ggz19/p/3823423.html
Copyright © 2011-2022 走看看