Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android 常用的intent Action整理

Android基本的设计理念是鼓励减少组件间的耦合,因此Android提供了Intent (意图) ,Intent提供了一种通用的消息系统,它允许在你的应用程序与其它的应用程序间传递Intent来执行动作和产生事件。Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递。通过其自带的属性,其实可以方便的完成很多较为复杂的操作。例如直接调用拨号功能、处理接收短信,诸如此类,都可以通过设置Intent属性来完成。Intent主要有以下四个重要属性,它们分别为:Action:Action属性的值为一个字符串,它代表了系统中已经定义了一系列常用的动作。通过setAction()方法或在清单文件AndroidManifest.xml中设置。标识Activity为一个程序开始的示例代码(AndroidManifest.xml进行配置)如下:
  1. <span style="font-size:16px;"><intent-filter>  
  2.                 <action android:name="android.intent.action.MAIN" />  
  3.                 <category android:name="android.intent.category.LAUNCHER" />  
  4. </intent-filter>  
  5. </span>  
Data:Data通常是URI格式定义的操作数据。例如:tel:// 。通过setData()方法设置。Category:Category属性用于指定当前动作(Action)被执行的环境。通过addCategory()方法或在清单文件AndroidManifest.xml中设置。默认为:CATEGORY_DEFAULT。Extras:Extras属性主要用于传递目标组件所需要的额外的数据。通过putExtras()方法设置。在本文中,主要介绍常见action的使用,Action描述Intent所触发动作名字的字符串,对于BroadcastIntent来说,Action指被广播出去的动作。理论上Action可 以为任何字符串,而与Android系统应用有关的Action字符串以静态字符串常量的形式定义在了Intent类中。Action中包含很多种,例如呼入,呼出电话,老师上课讲的接受短信等等,下面谨对常见的与系统有关的action进行整理:1. Intent.ACTION_MAINString: android.intent.action.MAIN标识Activity为一个程序的开始。2. Intent.Action_CALLStirng: android.intent.action.CALL呼叫指定的电话号码。Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:10086");
startActivity(intent);3. Intent.ACTION_POWER_CONNECTED;插上外部电源时发出的广播4 Intent.ACTION_POWER_DISCONNECTED;已断开外部电源连接时发出的广播5.Intent.Action.DIALString: action.intent.action.DIAL调用拨号面板Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086");
startActivity(intent);6.Intent.Action.ALL_APPSString: andriod.intent.action.ALL_APPS列出所有的应用。7.Intent.ACTION_ANSWERStirng:android.intent.action.ANSWER处理呼入的电话。8 .Intent.ACTION_BUG_REPORTString: android.intent.action.BUG_REPORT显示Dug报告。9. Intent.Action_CALL_BUTTONString: android.action.intent.CALL_BUTTON.相当于按“拨号”键。Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);10. Telephony.SMS_RECEIVEDString: android.provider.Telephony.SMS_RECEIVED接收短信的action <intent-filter>                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>                 <data android:host="localhost"/></intent-filter>11. Intent.ACTION_GET_CONTENTString: android.intent.action.GET_CONTENT允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)12. Intent.ACTION_BATTERY_LOW;String: android.intent.action.BATTERY_LOW表示电池电量低13. Intent.ACTION_SENDString: android.intent.action.Send发送邮件的action14. Intent.ACTION_CALL_PRIVILEGEDString:android.intent.action.CALL_PRIVILEGED调用skype的action         Intent intent = newIntent("android.intent.action.CALL_PRIVILEGED"); 
       intent.setClassName("com.skype.raider",
        "com.skype.raider.Main");
       intent.setData(Uri.parse("tel:" + phone)); 
        startActivity(intent); 15. Intent.ACTION_CLOSE_SYSTEM_DIALOGS当屏幕超时进行锁屏时,当用户按下电源按钮,长按或短按(不管有没跳出话框),进行锁屏时,android系统都会广播此Action消息以上是对常见的action进行总结,action其实有很多,如果要使用上文没有列举到的,google即可。更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11