Intent:提供了一種通用的消息系統,它容許在你的應用程序與其餘的應用程序間傳遞Intent來執行動做和產生事件.至關於一個請求.
Intent能夠激活Android應用的三個核心組件:活動(Activity)、服務(Service)、廣播接收器(BroadcastReceiver).
Intent中有兩種意圖:分別是顯式意圖和隱式意圖
一、顯示意圖:明確指定了組件名的Intent.
三種方式:
1)
Intent intent=
new Intent();
intent.setComponent(
new Component(MainActivity.this,OtherActivity.class));
2)
intent.setClass(MainActivity.
this,OtherActivity.
class);
3)
Intent intent=
new Intent(MainActivity.
this,OtherActivity.
class);
二、隱式意圖:沒有明確指定組件名的Intent.
Android系統會根據隱式意圖中設置的動做(action)、類別(category)、數據(data)找到最合適的組件來處理這個意圖.
例如:撥打電話的意圖:
Uri uri=Uri.parse(
"tel:"+phoneCode);
Intent intent=
new Intent(Intent.ACTION_CALL,uri);
下面就兩種意圖分別舉例:
首先,新建一個Android項目--->取名IntentDemo
MainActivity.java的代碼以下:
public
class MainActivity
extends Activity {
private EditText name =
null;
private Button submit =
null;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText)findViewById(R.id.etname);
submit = (Button)findViewById(R.id.submit);
submit.setOnClickListener(
new View.OnClickListener() {
@Override
public
void onClick(View v) {
String content = name.getText().toString();
Intent intent =
new Intent();
intent.putExtra(
"name", content);
intent.setClass(MainActivity.
this, OtherActivity.
class);
MainActivity.
this.startActivity(intent);
}
});
}
}
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/name"
android:id="@+id/name"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etname"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:id="@+id/submit"
/>
</LinearLayout>
strings.xml
<?xml version=
"1.0" encoding=
"utf-8"?>
<resources>
<string name=
"hello">Hello World, MainActivity!</string>
<string name=
"app_name">Intent應用</string>
<string name=
"name">你最親愛的人 :</string>
<string name=
"submit">提交無返</string>
<string name=
"resubmit">提交有返</string>
<string name=
"resend">返回</string>
</resources>
OtherActivity.java的代碼以下:
public
class OtherActivity
extends Activity {
private TextView result =
null;
@Override
protected
void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.other);
result = (TextView)findViewById(R.id.result);
Intent intent =
this.getIntent();
String content = intent.getExtras().getString(
"name");
result.setText(content);
}
}
other.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:id="@+id/result"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version=
"1.0" encoding=
"utf-8"?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package="com.gem.teset"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OtherActivity"
android:label="@string/app_name"
/>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
效果圖以下:
首先,新建一個Android項目--->取名IntentPhoneUrl
MainActivity.java的代碼以下:
public
class MainActivity
extends Activity {
private EditText num =
null;
private Button call =
null;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
num = (EditText)findViewById(R.id.num);
call =(Button)findViewById(R.id.call);
call.setOnClickListener(
new View.OnClickListener() {
@Override
public
void onClick(View v) {
String phoneNum = num.getText().toString();
Uri uri = Uri.parse(
"tel:" + phoneNum);
Intent intent =
new Intent(Intent.ACTION_CALL,uri);
startActivity(intent);
}
});
}
}
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"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/num"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/call"
android:text="撥號"
/>
</LinearLayout>
strings.xml
<?xml version=
"1.0" encoding=
"utf-8"?>
<resources>
<string name=
"hello">電話號碼</string>
<string name=
"app_name">IntentPhoneUrl</string>
</resources>
AndroidManifest.xml
<?xml version=
"1.0" encoding=
"utf-8"?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package="com.gem.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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 android:name="android.permission.SEND_SMS"/>
</manifest>
效果圖以下: