此文不足之處請發(fā)郵件提醒我,Email:i#openwudi.com(#改成@)
Activity
Activity是所有程序的根本,所有程序流程都運行在Activity之中,所以Activity類是為你創(chuàng)建一個窗口的,它可以對你的用戶見面進行處理。Activity在系統(tǒng)中被Activity堆所管理,當一個新的Activity被運行的時候,它被放置在堆的頂端,并且成為了一個活動的Activity。之前運行的Activity則在堆中被放在它的下面,將不能在新的Activity前面,直到那個新的Activity退出。(一直想把Activity翻譯成中文,發(fā)現(xiàn)翻譯成中文很別扭)
一個Activity有四個狀態(tài)(按重要性依次排序):
- 如果一個Activity顯示在屏幕上(也可以說是在堆的頂端),則稱為Activity是活動的。
- 如果一個Activity沒有激活,但是仍然可見,則這個Activity處于暫停狀態(tài)。這個狀態(tài)是并沒有死亡,但是可以被系統(tǒng)殺死。
- 如果一個Activity完成,但是它仍然保持著它之前的狀態(tài)和內(nèi)存中的信息。
- 如果一個Activity被暫?;蛘咄瓿闪?,系統(tǒng)可以隨時從內(nèi)存中丟掉這個Activity。
對于Android的生命周期和對垃圾的處理,我覺得充分的顯示出使用Java作為應用程序API的優(yōu)勢,獨特的聲明管理機制對開發(fā)者和使用者提供了便利。以下是生命周期圖,重點是對于生命周期的把握,狀態(tài)的保持和恢復,還有一會將要介紹作為Activity中間傳輸?shù)腎ntent。
Activity中常用的函數(shù)有:
SetContentView() 增加另外一個內(nèi)容
findViewById() 根據(jù)在XML中所View控件的id屬性找到控件,次方法要加到onCreate中
finish() 在Activity運行完應該被關(guān)閉時使用
startActivity() 運行一個Activity
其生命周期涉及的函數(shù)有:
void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
注意的是,Activity的使用需要在Manifest文件中添加相應 的<Activity>,并設(shè)置其屬性和intent-filter。
Intentintent是提供Activity直接交互的抽象描述,可以被startActivty運行。intent提供了一個媒介,提供組件的相互調(diào)用,實現(xiàn)調(diào)用者和被調(diào)用者之間的解耦。
intent中屬性的設(shè)置
- action —— 通常要執(zhí)行的動作,比如ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, 等.
- data —— 執(zhí)行動作要操作的數(shù)據(jù),Android采用指向數(shù)據(jù)的一個URI來表示。對于不同的動作,其URI數(shù)據(jù)的類型是不同的(可以設(shè)置type屬性指 定特定類型數(shù)據(jù)),如ACTION_EDIT指定Data為文件URI,打電話為tel:URI,訪問網(wǎng)絡(luò)為http:URI,而由content provider提供的數(shù)據(jù)則為content: URIs。
- category —— 被執(zhí)行動作的附加信息。
- type —— 顯式指定Intent的數(shù)據(jù)類型 (MIME)。一般Intent的數(shù)據(jù)類型能夠根據(jù)數(shù)據(jù)本身進行判定,但是通過設(shè)置這個屬性,可以強制采用顯式指定的類型而不再進行推導。
- component —— 指定Intent的的目標組件的類名稱。通常 Android會根據(jù)Intent 中包含的其它屬性的信息,比如action、data/type、category進行查找,最終找到一個與之匹配的目標組件。但是,如果 component這個屬性有指定的話,將直接使用它指定的組件,而不再執(zhí)行上述查找過程。指定了這個屬性以后,Intent的其它所有屬性都是可選的。
- extras —— 是其它所有附加信息的集合。使用extras可以為組件提供擴展信息,比如,如果要執(zhí)行“發(fā)送電子郵件”這個動作,可以將電子郵件的標題、正文等保存在extras里,傳給電子 郵件發(fā)送組件。
理解Intent的關(guān)鍵之一是理解清楚Intent的兩種基本用法:一種是顯式的Intent,即在構(gòu)造Intent對象時就指定接收者;另一種是隱式的Intent,即Intent的發(fā)送者在構(gòu)造Intent對象時,并不知道也不關(guān)心接收者是誰,有利于降低發(fā)送者和接收者之間的耦合。
對于顯式Intent,Android不需要去做解析,因為目標組件已經(jīng)很明確,Android需要解析的是那些隱式Intent,通過解析,將Intent映射給可以處理此Intent的Activity、IntentReceiver或Service。
Intent解析機制主要是通過查找已注冊在AndroidManifest.xml中的所有IntentFilter及其中定義的Intent,最終找到匹配的Intent。在這個解析過程中,Android是通過Intent的action、type、category這三個屬性來進行判斷的,判斷方法如下:
• 如果 Intent指明定了action,則目標組件的IntentFilter的action列表中就必須包含有這個action,否則不能匹配;
• 如 果Intent沒有提供type,系統(tǒng)將從data中得到數(shù)據(jù)類型。和action一樣,目標組件的數(shù)據(jù)類型列表中必須包含Intent的數(shù)據(jù)類型,否則 不能匹配。
• 如果Intent中的數(shù)據(jù)不是content: 類型的URI,而且Intent也沒有明確指定它的type,將根據(jù)Intent中數(shù)據(jù)的scheme(比如 http: 或者mailto:) 進行匹配。同上,Intent 的scheme必須出現(xiàn)在目標組件的scheme列表中。
• 如果Intent指定了一個或多個 category,這些類別必須全部出現(xiàn)在組建的類別列表中。比如Intent中包含了兩個類別:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目標組件必須至少包含這兩個類別。
Intent-Filter的定義范例:
<activity android:name=
"NotesList" android:label=
"@string/title_notes_list"> <intent-filter> <action android:name=
"android.intent.action.MAIN" /> <category android:name=
"android.intent.category.LAUNCHER" /> </intent-filter>
<intent-filter> <action android:name=
"android.intent.action.VIEW" /> <action android:name=
"android.intent.action.EDIT" /> <action android:name=
"android.intent.action.PICK" /> <category android:name=
"android.intent.category.DEFAULT" /> <data android:mimeType=
"vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> <intent-filter> <action android:name=
"android.intent.action.GET_CONTENT" /> <category android:name=
"android.intent.category.DEFAULT" /> <data android:mimeType=
"vnd.android.cursor.item/vnd.google.note" /> </intent-filter></activity>我的intent簡單的實現(xiàn):Activity02.javapackage dan.activity;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity02 extends Activity {
Button myButton = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myButton);
myButton.setText("跳轉(zhuǎn)到另一個Activity");
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements android.view.View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//生成一個Intent對象
Intent intent = new Intent();
intent.setClass(Activity02.this,OtherActivity.class);
intent.putExtra("testExtra", "haha");
Activity02.this.startActivity(intent);
}
}
}
OtherActivity.javapackage dan.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
TextView myTextView = null;
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView = (TextView)findViewById(R.id.myTextView);
Intent intent = getIntent();
String mytext = intent.getStringExtra("testExtra");
myTextView.setText(mytext);
}
}
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dan.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity02"
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/other"></activity>
</application>
</manifest>
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"
>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
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:id = "@+id/myTextView"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
string.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Activity02!</string>
<string name="app_name">Activity02</string>
<string name = "other">otherActivity</string>
</resources>
運行效果:點擊按鈕之后:
下面是轉(zhuǎn)載來的其他的一些Intent用法 實例(轉(zhuǎn)自javaeye)
顯示網(wǎng)頁
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
路徑規(guī)劃
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. //調(diào)用短信程序
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相關(guān)
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. //顯示某個應用的相關(guān)信息
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);