标签:android blog http java 使用 os
Intent的中文意思是“意图,目的”的意思,可以理解为不同组件之间通信的“媒介”或者“信使”。
目标组件一般要通过Intent来声明自己的条件,一般通过组件中的<intent-filter>元素来过滤。
Intent在由以下几个部分组成:动作(action),数据(data),分类(Category),类型(Type),组件(Component),和扩展信息(Extra)。
Intent在寻找目标组件的时候有两种方法:第一,通过组件名称直接指定;第二,通过Intent Filter过滤指定
Intent启动不同组件的方法
|
组件名称
|
方法名称
|
|
Activity
|
startActivity()
startActivityForResult()
|
|
Service
|
startService()
bindService()
|
|
Broadcasts
|
sendBroadcast()
sendOrderedBroadcast()
sendStickyBroadcast()
|
常见的Activity Action Intent常量
|
常量名称
|
常量值
|
意义
|
|
ACTION_MAIN
|
android.intent.action.MAIN
|
应用程序入口
|
|
ACTION_VIEW
|
android.intent.action.VIEW
|
显示数据给用户
|
|
ACTION_ATTACH_DATA
|
android.intent.action.ATTACH_DATA
|
指明附加信息给其他地方的一些数据
|
|
ACTION_EDIT
|
android.intent.action.EDIT
|
显示可编辑的数据
|
|
ACTION_PICK
|
android.intent.action.PICK
|
选择数据
|
|
ACTION_CHOOSER
|
android.intent.action.CHOOSER
|
显示一个Activity选择器
|
|
ACTION_GET_CONTENT
|
android.intent.action.GET_CONTENT
|
获得内容
|
|
ACTION_DIAL
|
android.intent.action.GET_CONTENT
|
显示打电话面板
|
|
ACITON_CALL
|
android.intent.action.DIAL
|
直接打电话
|
|
ACTION_SEND
|
android.intent.action.SEND
|
直接发短信
|
|
ACTION_SENDTO
|
android.intent.action.SENDTO
|
选择发短信
|
|
ACTION_ANSWER
|
android.intent.action.ANSWER
|
应答电话
|
|
ACTION_INSERT
|
android.intent.action.INSERT
|
插入数据
|
|
ACTION_DELETE
|
android.intent.action.DELETE
|
删除数据
|
|
ACTION_RUN
|
android.intent.action.RUN
|
运行数据
|
|
ACTION_SYNC
|
android.intent.action.SYNC
|
同步数据
|
|
ACTION_PICK_ACTIVITY
|
android.intent.action.PICK_ACTIVITY
|
选择Activity
|
|
ACTION_SEARCH
|
android.intent.action.SEARCH
|
搜索
|
|
ACTION_WEB_SEARCH
|
android.intent.action.WEB_SEARCH
|
Web搜索
|
|
ACTION_FACTORY_TEST
|
android.intent.action.FACTORY_TEST
|
工厂测试入口点
|
常见的BroadcastIntent Action常量
|
BroadcastIntent Action字符串常量
|
描述
|
|
ACTION_TIME_TICK
|
系统时间每过一分钟发出的广播
|
|
ACTION_TIME_CHANGED
|
系统时间通过设置发生了变化
|
|
ACTION_TIMEZONE_CHANGED
|
时区改变
|
|
ACTION_BOOT_COMPLETED
|
系统启动完毕
|
|
ACTION_PACKAGE_ADDED
|
新的应用程序apk包安装完毕
|
|
ACTION_PACKAGE_CHANGED
|
现有应用程序apk包改变
|
|
ACTION_PACKAGE_REMOVED
|
现有应用程序apk包被删除
|
|
ACTION_UID_REMOVED
|
用户id被删除
|
Intent的Action和Data属性匹配
|
Action属性
|
Data属性
|
说明
|
|
ACTION_VIEW
|
content://contacts/people/1
|
显示id为1的联系人信息
|
|
ACTION_DIAL
|
content://contacts/people/1
|
将id为1的联系人电话号码显示在拨号界面中
|
|
ACITON_VIEW
|
tel:123
|
显示电话为123的联系人信息
|
|
ACTION_VIEW
|
http://www.google.com
|
在浏览器中浏览该网站
|
|
ACTION_VIEW
|
file://sdcard/mymusic.mp3
|
播放MP3
|
|
ACTION_VIEW
|
geo:39.2456,116.3523
|
显示地图
|
常见的Category常量
|
Category字符串常量
|
描述
|
|
CATEGORY_BROWSABLE
|
目标Activity能通过在网页浏览器中点击链接而激活(比如,点击浏览器中的图片链接)
|
|
CATEGORY_GADGET
|
表示目标Activity可以被内嵌到其他Activity当中
|
|
CATEGORY_HOME
|
目标Activity是HOME Activity,即手机开机启动后显示的Activity,或按下HOME键后显示的Activity
|
|
CATEGORY_LAUNCHER
|
表示目标Activity是应用程序中最优先被执行的Activity
|
|
CATEGORY_PREFERENCE
|
表示目标Activity是一个偏爱设置的Activity
|
常见的Extra常量
|
Extra键值字符串常量
|
描述
|
|
EXTRA_BCC
|
装有邮件密送地址的字符串数组
|
|
EXTRA_CC
|
装有邮件抄送地址的字符串数组
|
|
EXTRA_EMAIL
|
装有邮件发送地址的字符串数组
|
|
EXTRA_INTENT
|
使用ACTION_PICK_ACTIVITY动作时装有Intent选项的键
|
|
EXTRA_KEY_EVENT
|
触发该Intent的案件的KeyEvent对象
|
|
EXTRA_PHONE_NUMBER
|
使用拨打电话相关的Action时,电话号码字符串的键,类型为String
|
|
EXTRA_SHORTCUT_ICON
|
使用ACTION_CREATE_SHORTCUT在HomeActivity创建快捷方式时,对快捷方式的描述信息。其中ICON和ICON_RESOURCE描述的是快捷方式的图标,类型分别为Bitmap和ShortcutIconResource。INTENT描述的是快捷方式相对应的Intent对象。NAME描述的是快捷方式的名字。
|
|
EXTRA_SHORTCUT_ICON_RESOURCE
|
|
EXTRA_SHORTCUT_INTENT
|
|
EXTRA_SHORTCUT_NAME
|
|
EXTRA_SUBJECT
|
描述信息主题的键
|
|
EXTRA_TEXT
|
使用ACTION_SEND动作时,用来描述要发送的文本信息,类型为CharSequence
|
|
EXTRA_TITLE
|
使用ACTION_CHOOSER动作时,描述对话框标题的键,类型为CharSequence
|
|
EXTRA_UID
|
使用ACTION_UID_REMOVED动作时,描述删除的用户id的键,类型为int
|
Android.telephony包中的类
|
类名
|
描述
|
|
CellLocation
|
表示设备位置的抽象类
|
|
PhoneNumberFormattingTextWather
|
监视一个TextView控件,如果有电话号码输入,则用formatNumber()方法处理电话号码
|
|
PhoneNumberUtils
|
包含各种处理电话号码字符串的使用工具
|
|
PhoneStateListener
|
监视手机中电话状态变化的监听类
|
|
ServiceState
|
包含电话状态和相关的服务信息
|
|
TelephonyManager
|
提供对手机中电话服务信息的访问
|
与短信服务相关的类主要在包android.telephony.gsm中
|
类名
|
描述
|
|
GsmCellLocation
|
表示GSM手机的基站位置
|
|
SmsManager
|
管理各种短信操作
|
|
SmsMessage
|
表示具体的短信
|
例子一:计算器
- package org.loulijun.calculate;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class Calculater extends Activity {
- /** Called when the activity is first created. */
- private EditText numberone;
- private EditText numbertwo;
- private Button mybutton;
- private TextView symbol;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- numberone=(EditText)findViewById(R.id.numberone);
- numbertwo=(EditText)findViewById(R.id.numbertwo);
- mybutton=(Button)findViewById(R.id.mybutton);
- symbol=(TextView)findViewById(R.id.symbol);
- mybutton.setOnClickListener(new Calculate());
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- menu.add(0, 1, 1, R.string.exit);
- menu.add(0,2,2,R.string.about);
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- if(item.getItemId()==1)
- {
- finish();
- }
- else if(item.getItemId()==2)
- {
- Toast.makeText(this, "亲爱的你可知,我有多么思念你!", Toast.LENGTH_SHORT).show();
- }
- return super.onOptionsItemSelected(item);
- }
-
- class Calculate implements OnClickListener
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String number1=numberone.getText().toString();
- String number2=numbertwo.getText().toString();
- Intent intent=new Intent();
- intent.putExtra("num1",number1);
- intent.putExtra("num2", number2);
- intent.setClass(Calculater.this,Result.class);
- Calculater.this.startActivity(intent);
- }
- }
- }
package org.loulijun.calculate;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
-
- public class Result extends Activity {
- private TextView textview;
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.result);
- textview=(TextView)findViewById(R.id.textview);
- Intent intent=getIntent();
- String numberone=intent.getStringExtra("num1");
- String numbertwo=intent.getStringExtra("num2");
- Double d1=Double.parseDouble(numberone);
- Double d2=Double.parseDouble(numbertwo);
- Double result=d1/d2;
-
- textview.setText(numberone+" / "+numbertwo+" = "+result);
- }
- }
main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/numberone"
- android:hint="请输入数字"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/symbol"
- android:text="@string/symbol"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/numbertwo"
- android:hint="请输入数字"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/mybutton"
- android:text="@string/calculate"
- />
- </LinearLayout>
result.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textview"
- />
- </LinearLayout>
注意:result.java需要注册到AndroidManifest.xml文件中
<activity android:name=".Result" android:label="@string/result">
</activity>
运行结果如下:


例子二:调用系统内置action
- package org.hualang.intent;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
-
- public class IntentTest extends ListActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 菜单项数组
- String[] menus = { "查看电话信息", "编辑电话信息", "显示拨打电话界面","直接打电话","访问浏览器","访问地图"};
- // 将菜单项数组设置为ListView的列表项展示
- setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, menus));
- getListView().setTextFilterEnabled(true);
- }
-
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- Intent intent = new Intent();
- Uri uri ;
- String data;
- switch (position) {
- // 查看_id 为1的用户电话信息
- case 0:
- data = "content://contacts/people/1";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 编辑_id 为1的用户电话信息
- case 1:
- data = "content://contacts/people/1";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_EDIT);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 显示拨打电话界面
- case 2:
- data = "tel:13800138000";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_DIAL);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 直接打电话
- case 3:
- data = "tel:13800138000";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_CALL);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 访问浏览器
- case 4:
- data = "http://www.google.com";
- uri = Uri.parse(data);
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(uri);
- startActivity(intent);
- break;
- // 访问地图
- case 5:
- data = "geo:39.92,116.46";
- uri = Uri.parse(data);
- intent = new Intent(Intent.ACTION_VIEW,uri);
- startActivity(intent);
- break;
- default:
- break;
- }
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.hualang.intent"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".IntentTest"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- </application>
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
- </manifest>
运行结果如下:

当点击直接打电话后


原文:http://hualang.iteye.com/blog/983471
玩转Android---组件篇---Intent(意图),布布扣,bubuko.com
玩转Android---组件篇---Intent(意图)
标签:android blog http java 使用 os
原文地址:http://www.cnblogs.com/kevincode/p/3853844.html