二.Inten启动组件的方法
Intent可以启动一个Activity,也可以启动一个Service,还可以发起一个广播Broadcasts。具体方法如下:
组件名称 | 方法名称 |
Activity | startActvity( ) startActivity( ) |
Service | startService( ) bindService( ) |
Broadcasts | sendBroadcasts( ) sendOrderedBroadcasts( ) sendStickyBroadcasts( ) |
三.Intent的属性
1.Intent的Action属性
Action是指Intent要完成的动作,是一个字符串常量。SDK中定义了一些标准的Action常量如下表所示。
Constant | Target component | Action |
ACTION_CALL | activity | Initiate a phone call. |
ACTION_EDIT | activity | Display data for the user to edit. |
ACTION_MAIN | activity | Start up as the initial activity of a task, with no data input and no returned output. |
ACTION_SYNC | activity | Synchronize data on a server with data on the mobile device. |
ACTION_BATTERY_LOW | broadcast receiver | A warning that the battery is low. |
ACTION_HEADSET_PLUG | broadcast receiver | A headset has been plugged into the device, or unplugged from it. |
ACTION_SCREEN_ON | broadcast receiver | The screen has been turned on. |
ACTION_TIMEZONE_CHANGED | broadcast receiver | The setting for the time zone has changed. |
下面是一个测试Action常量的例子:
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"
/>
<Button
android:text="测试Action属性"
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">测试Action属性</string>
<string name="app_name">IntentActionDemo</string>
</resources>
</pre><pre name="code" class="java">MainActivity.java
package com.android.action.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button getBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getBtn=(Button)findViewById(R.id.getBtn);
getBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);// 设置Intent Action属性
intent.setType("vnd.android.cursor.item/phone");// 设置Intent Type 属性
//主要是获取通讯录的内容
startActivity(intent); // 启动Activity
}
});
}
}
3.Intent的Category属性
Intent中的Category属性是一个执行动作Action的附加信息。比如:CATEGORY_HOME则表示放回到Home界面,ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个。下表是SDK文档中关于Category的信息。
Constant | Meaning |
CATEGORY_BROWSABLE | The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message. |
CATEGORY_GADGET | The activity can be embedded inside of another activity that hosts gadgets. |
CATEGORY_HOME | The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed. |
CATEGORY_LAUNCHER | The activity can be the initial activity of a task and is listed in the top-level application launcher. |
CATEGORY_PREFERENCE | The target activity is a preference panel. |
下面是一个回到Home界面的例子:
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="测试Intent Category"
/>
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转到Home界面"
/>
</LinearLayout>
MainActivity.java
package com.android.category.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.Button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);// 添加Action属性
intent.addCategory(Intent.CATEGORY_HOME);// 添加Category属性
startActivity(intent);// 启动Activity
}
});
}
}
存数据-类型1:
- Intent intent = new Intent();
- //设置Intent的class属性,跳转到SecondActivity
- intent.setClass(FirstActivity.this, SecondActivity.class);
- //为intent添加额外的信息
- intent.putExtra("useName", etx.getText().toString());
- //启动Activity
- startActivity(intent);
取数据-类型1:
- //获得Intent
- Intent intent = this.getIntent();
- tv = (TextView)findViewById(R.id.TextView1);
- //从Intent获得额外信息,设置为TextView的文本
- tv.setText(intent.getStringExtra("useName"));
Intent intent = new Intent(baiduActivity.this, BNDemoGuideActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable(ROUTE_PLAN_NODE, (BNRoutePlanNode) mBNRoutePlanNode); intent.putExtras(bundle); startActivity(intent);取数据-类型2:
Intent intent = getIntent(); if (intent != null) { Bundle bundle = intent.getExtras(); if (bundle != null) { mBNRoutePlanNode = (BNRoutePlanNode) bundle.getSerializable(baiduActivity.ROUTE_PLAN_NODE); } }存数据-类型3
Intent intent=new Intent(context,endZhiboActivity.class); Bundle mBundle = new Bundle(); mBundle.putSerializable("closeRoom", closeRoom); intent.putExtras(mBundle); context.startActivity(intent)取数据-类型3:
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
closeRoomInfo= (CloseRoom) bundle.getSerializable("closeRoom");Intent的其它一些用法:
显示网页:显示地图:
- Uri uri = Uri.parse("http://www.google.com");
- Intent it = new Intent(Intent.ACTION_VIEW,uri);
- startActivity(it);
路径规划:
- Uri uri = Uri.parse("geo:38.899533,-77.036476");
- Intent it = new Intent(Intent.Action_VIEW,uri);
- startActivity(it);
拨打电话:
- Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
- Intent it = new Intent(Intent.ACTION_VIEW,URI);
- startActivity(it);
调用拨号程序
- Uri uri = Uri.parse("tel:xxxxxx");
- Intent it = new Intent(Intent.ACTION_DIAL, uri);
- startActivity(it);
发送SMS/MMS
- Uri uri = Uri.parse("tel.xxxxxx");
- Intent it =new Intent(Intent.ACTION_CALL,uri);
- 要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
调用发送短信的程序发送短信
- Intent it = new Intent(Intent.ACTION_VIEW);
- it.putExtra("sms_body", "The SMS text");
- it.setType("vnd.android-dir/mms-sms");
- startActivity(it);
发送彩信
- Uri uri = Uri.parse("smsto:0800000123");
- Intent it = new Intent(Intent.ACTION_SENDTO, uri);
- it.putExtra("sms_body", "The SMS text");
- startActivity(it);
发送Email
- Uri uri = Uri.parse("content://media/external/images/media/23");
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra("sms_body", "some text");
- it.putExtra(Intent.EXTRA_STREAM, uri);
- it.setType("image/png");
- startActivity(it);
- Uri uri = Uri.parse("mailto:xxx@abc.com");
- Intent it = new Intent(Intent.ACTION_SENDTO, uri);
- startActivity(it);
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
- it.putExtra(Intent.EXTRA_TEXT, "The email body text");
- it.setType("text/plain");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
添加附件
- Intent it=new Intent(Intent.ACTION_SEND);
- String[] tos={"me@abc.com"};
- String[] ccs={"you@abc.com"};
- it.putExtra(Intent.EXTRA_EMAIL, tos);
- it.putExtra(Intent.EXTRA_CC, ccs);
- it.putExtra(Intent.EXTRA_TEXT, "The email body text");
- it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- it.setType("message/rfc822");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
播放多媒体
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
- sendIntent.setType("audio/mp3");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
- Intent it = new Intent(Intent.ACTION_VIEW);
- Uri uri = Uri.parse("file:///sdcard/song.mp3");
- it.setDataAndType(uri, "audio/mp3");
- startActivity(it);
Uninstall 程序
- Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
- Uri uri = Uri.fromParts("package", strPackageName, null);
- Intent it = new Intent(Intent.ACTION_DELETE, uri);
- startActivity(it);