多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > 【邊做項目邊學Android】知識點:Intent

【邊做項目邊學Android】知識點:Intent

來源:程序員人生   發布時間:2014-11-14 08:08:50 閱讀次數:3109次

1. Intent的介紹

  • Android中提供了Intent機制來協助利用間的交互與通訊,或采取更準確的說法是,Intent不但可用于利用程序之間,也可用于利用程序內部的activity, service和broadcast receiver之間的交互。
  • Intent是1種運行時綁定(runtime binding)機制,它能在程序運行的進程中連接兩個不同的組件。通過Intent,你的程序可以向Android表達某種要求或意愿,Android會根據意愿的內容選擇適當的組件來響應。

Intent的中文意思是“意圖,意向”,在Android中提供了Intent機制來協助利用間的交互與通訊,Intent負責對利用中1次操作的動作、動作觸及數據、附加數據進行描寫,Android則根據此Intent的描寫,負責找到對應的組件,將 Intent傳遞給調用的組件,并完成組件的調用。Intent不但可用于利用程序之間,也可用于利用程序內部的Activity/Service之間的交互。因此,可以將Intent理解為不同組件之間通訊的“媒介”專門提供組件相互調用的相干信息,起著1個媒體中介的作用,專門提供組件相互調用的相干信息,實現調用者與被調用者之間的解耦。

Intent 是1個將要履行的動作的抽象的描寫,1般來講是作為參數來使用,由Intent來協助完成android各個組件之間的通訊。比如說調用 startActivity()來啟動1個activity,或由broadcaseIntent()來傳遞給所有感興趣的 BroadcaseReceiver, 再或由startService()/bindservice()來啟動1個后臺的service。所以可以看出來,intent主要是用來啟動其他的 activity 或service,所以可以將intent理解成activity之間的粘合劑。

主要用處

Intent是1種消息傳遞機制,它可以在利用程序內使用,也能夠在利用程序間使用,主要用處分為:

1.使用類名顯示的啟動1個特定的Activity或Service

2.啟動Activity或Service來履行1個動作的Intent,通常需要使用特定的數據,或對特定的數據履行動作

3.廣播某個事件已產生

activity、service和broadcast receiver之間是通過Intent進行通訊的,而另外1個組件Content Provider本身就是1種通訊機制,不需要通過Intent。我們來看下面這個圖就知道了:

如果Activity1需要和Activity2進行聯系,2者不需要直接聯系,而是通過Intent作為橋梁。通俗來說,Intnet類似于中介、牙婆的角色。

Intent最多見的1個用法是顯示的(通過指定要裝載的類)或隱式的(通過要求對1條數據履行某個動作)啟動新的activity,在后1種情況下,動作不1定由調用利用程序中的Activty履行。

Intent也能夠在系統范圍內發送廣播消息。利用程序可以注冊1個Broadcast Receiver來監聽相應這些廣播的Intent。Android就是通過廣播Intent來公布系統事件,比如網絡連接狀態或電池電量的改變。


2. Inten啟動組件的方法

Intent可以啟動1個Activity,也能夠啟動1個Service,還可以發起1個廣播Broadcasts。具體方法以下:

對向這3種組件發送intent有不同的機制:

  • 使用Context.startActivity() 或 Activity.startActivityForResult(),傳入1個intent來啟動1個activity。使用 Activity.setResult(),傳入1個intent來從activity中返回結果。
  • 將intent對象傳給Context.startService()來啟動1個service或傳消息給1個運行的service。將intent對象傳給 Context.bindService()來綁定1個service。
  • 將intent對象傳給 Context.sendBroadcast(),Context.sendOrderedBroadcast(),或Context.sendStickyBroadcast()等廣播方法,則它們被傳給 broadcast receiver。

組件名稱

方法名稱

Activity

startActvity( )

startActivityForResult( )

Service

startService( )

bindService( )

Broadcasts

sendBroadcasts( )

sendOrderedBroadcasts( )

sendStickyBroadcasts( )


3. Intent的構成

Intent由以下各個組成部份:

  • component(組件):目的組件
  • action(動作):用來表現意圖的行動
  • category(種別):用來表現動作的種別
  • data(數據):表示與動作要操縱的數據
  • type(數據類型):對data范例的描述
  • extras(擴大信息):擴大信息
  • Flags(標志位):期望這個意圖的運行模式

1、component(組件):目的組件

指定Intent的的目標組件的類名稱。通常 Android會根據Intent 中包括的其它屬性的信息,比如action、data/type、category進行查找,終究找到1個與之匹配的目標組件。但是,如果 component這個屬性有指定的話,將直接使用它指定的組件,而不再履行上述查找進程。指定了這個屬性以后,Intent的其它所有屬性都是可選的。

例如,啟動第2個Activity時,我們可以這樣來寫:

button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //創建1個意圖對象 Intent intent = new Intent(); //創建組件,通過組件來響應 ComponentName component = new ComponentName(MainActivity.this, SecondActivity.class); intent.setComponent(component); startActivity(intent); } });

如果寫的簡單1點,監聽事件onClick()方法里可以這樣寫:

Intent intent = new Intent(); //setClass函數的第1個參數是1個Context對象 //Context是1個類,Activity是Context類的子類,也就是說,所有的Activity對象,都可以向上轉型為Context對象 //setClass函數的第2個參數是1個Class對象,在當前場景下,應當傳入需要被啟動的Activity類的class對象 intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent);

再簡單1點,可以這樣寫:(固然,也是最多見的寫法)

Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent);

2、Action(動作):用來表現意圖的行動

當平常生活中,描寫1個意愿或欲望的時候,總是有1個動詞在其中。比如:我想“做”3個俯臥撐;我要“寫”1封情書,等等。在Intent中,Action就是描寫做、寫等動作的,當你指明了1個Action,履行者就會依照這個動作的唆使,接受相干輸入,表現對應行動,產生符合的輸出。在Intent類中,定義了1批量的動作,比如ACTION_VIEW,ACTION_PICK等, 基本涵蓋了經常使用動作。加的動作越多,越精確。

標準的Activity Actions
ACTION_MAIN                             作為1個主要的進入口,而其實不期望去接受數據
ACTION_VIEW                             向用戶去顯示數據
ACTION_ATTACH_DATA               用于指定1些數據應當附屬于1些其他的地方,例如,圖片數據應當附屬于聯系人
ACTION_EDIT                              訪問已給的數據,提供明確的可編輯
ACTION_PICK                              從數據當選擇1個子項目,并返回你所選中的項目
ACTION_CHOOSER                      顯示1個activity選擇器,允許用戶在進程之前選擇他們想要的
ACTION_GET_CONTENT               允許用戶選擇特殊種類的數據,并返回(特殊種類的數據:照1張相片或錄1段音)
ACTION_DIAL                               撥打1個指定的號碼,顯示1個帶有號碼的用戶界面,允許用戶去啟動呼喚
ACTION_CALL                              根據指定的數據履行1次呼喚
(ACTION_CALL在利用中啟動1次呼喚有缺點,多數利用ACTION_DIAL,ACTION_CALL不能用在緊急呼喚上,緊急呼喚可以用ACTION_DIAL來實現)
ACTION_SEND                             傳遞數據,被傳送的數據沒有指定,接收的action要求用戶發數據
ACTION_SENDTO                         發送1個信息到指定的某人
ACTION_ANSWER                        處理1個打進電話呼喚
ACTION_INSERT                          插入1條空項目到已給的容器
ACTION_DELETE                          從容器中刪除已給的數據
ACTION_RUN                               運行數據,不管怎樣
ACTION_SYNC                             同步履行1個數據
ACTION_PICK_ACTIVITY              為已知的Intent選擇1個Activity,返回別選中的類
ACTION_SEARCH                         履行1次搜索
ACTION_WEB_SEARCH                履行1次web搜索
ACTION_FACTORY_TEST              工場測試的主要進入點,

標準的廣播Actions

ACTION_TIME_TICK                   當前時間改變,每分鐘都發送,不能通過組件聲明來接收,只有通過Context.registerReceiver()方法來注冊
ACTION_TIME_CHANGED            時間被設置
ACTION_TIMEZONE_CHANGED   時間區改變
ACTION_BOOT_COMPLETED       系統完成啟動后,1次廣播
ACTION_PACKAGE_ADDED         1個新利用包已安裝在裝備上,數據包括包名(最新安裝的包程序不能接收到這個廣播)
ACTION_PACKAGE_CHANGED    1個已存在的利用程序包已改變,包括包名
ACTION_PACKAGE_REMOVED   1個已存在的利用程序包已從裝備上移除,包括包名(正在被安裝的包程序不能接收到這個廣播)
ACTION_PACKAGE_RESTARTED 用戶重新開始1個包,包的所有進程將被殺死,所有與其聯系的運行時間狀態應當被移除,包括包名(重新開始包程序不能接收到這個廣播)
ACTION_PACKAGE_DATA_CLEARED 用戶已清楚1個包的數據,包括包名(清除包程序不能接收到這個廣播)
ACTION_BATTERY_CHANGED 電池的充電狀態、電荷級別改變,不能通過組建聲明接收這個廣播,只有通過Context.registerReceiver()注冊
ACTION_UID_REMOVED 1個用戶ID已從系統中移除

3、category(種別):用來表現動作的種別

Intent中的Category屬性是1個履行動作Action的附加信息。比如:CATEGORY_HOME則表示放回到Home界面,ALTERNATIVE_CATEGORY表示當前的Intent是1系列的可選動作中的1個。

指定Action范圍,這個選項指定了將要履行的這個action的其他1些額外的束縛。有時通過Action,配合Data 或Type,很多時候可以準確的表達出1個完全的意圖了,但也會需要加1些束縛在里面才能夠更精準。比如,如果你雖然很喜歡做俯臥撐,但1次做3個還只是在特殊的時候才會產生,那末你可能表達說:每次吃撐了的時候 ,我都想做3個俯臥撐。吃撐了,這就對應著Intent的Category的范疇,它給所產生的意圖附加1個束縛。在Android中,1個實例是:所有利用的主Activity(單獨啟動時候,第1個運行的那個Activity...),都需要1個Category為 CATEGORY_LAUNCHER,Action為ACTION_MAIN的Intent。

在自定義動作時,使用activity組件時,必須添加1個默許的種別

具體的實現為:

<intent-filter> <action android:name="com.example.action.MY_ACTION"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter>
如果有多個組件被匹配成功,就會以對話框列表的方式讓用戶進行選擇。

每一個Intent中只能指定1個action,但卻能指定多個category;種別越多,動作越具體,意圖越明確(類似于相親時,給對方提了很多要求)。

自定義種別: 在Intent添加種別可以添加多個種別,那就要求被匹配的組件必須同時滿足這多個種別,才能匹配成功。操作Activity的時候,如果沒有種別,須加上默許種別

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.

4、data(數據):表示與動作要操縱的數據

  • Data屬性是Android要訪問的數據,和action和Category聲明方式相同,也是在<intent-filter>中。
  • 多個組件匹配成功顯示優先級高的; 相同顯示列表。

Data是用1個uri對象來表示的,uri代表數據的地址,屬于1種標識符。通常情況下,我們使用action+data屬性的組合來描寫1個意圖:做甚么

使用隱式Intent,我們不但可以啟動自己程序內的活動,還可以啟動其他程序的活動,這使得Android多個利用程序之間的功能同享成了可能。比如利用程序中需要展現1個網頁,沒有必要自己去實現1個閱讀器(事實上也不太可能),而是只需要條用系統的閱讀器來打開這個網頁就好了。

  • 當Intent匹配成功的組件有多個時,顯示優先級高的組件,如果優先級相同,顯示列表讓用戶自己選擇
  • 優先級從⑴000至1000,并且其中1個必須為負的才有效

注:系統默許的閱讀器并沒有做出優先級聲明,其優先級默許為正數。

Data屬性的聲明中要指定訪問數據的Uri和MIME類型。可以在<data>元素中通過1些屬性來設置:

android:scheme、android:path、 android:port、android:mimeType、android:host等,通過這些屬性來對應1個典型的Uri格式 scheme://host:port/path。例如:http://www.google.com。

5、type(數據類型):對data范例的描述

如果Intent對象中既包括Uri又包括Type,那末,在<intent-filter>中也必須2者都包括才能通過測試。

Type屬性用于明確指定Data屬性的數據類型或MIME類型,但是通常來講,當Intent不指定Data屬性時,Type屬性才會起作用,否則Android系統將會根據Data屬性值來分析數據的類型,所以無需指定Type屬性。

data和type屬性1般只需要1個,通過setData方法會把type屬性設置為null,相反設置setType方法會把data設置為null,如果想要兩個屬性同時設置,要使用Intent.setDataAndType()方法。

6、extras(擴大信息):擴大信息

是其它所有附加信息的集合。使用extras可以為組件提供擴大信息,比如,如果要履行“發送電子郵件”這個動作,可以將電子郵件的標題、正文等保存在extras里,傳給電子郵件發送組件。

7、Flags(標志位):期望這個意圖的運行模式

1個程序啟動后系統會為這個程序分配1個task供其使用,另外同1個task里面可以具有不同利用程序的activity。那末,同1個程序能不能具有多個task?這就觸及到加載activity的啟動模式,這個需要單獨講1下。能辨認,有輸入,全部Intent基本就完全了,但還有1些附件的指令,需要放在Flags中帶過去。顧名思義,Flags是1個整形數,有1些列的標志 位構成,這些標志,是用來指明運行模式的。比如,你期望這個意圖的履行者,和你運行在兩個完全不同的任務中(或說進程也無妨吧...),就需要設置FLAG_ACTIVITY_NEW_TASK 的標志位。

注:android中1組邏輯上在1起的activity被叫做task,自己認為可以理解成1個activity堆棧。


Intent 的解析機制

理解Intent的關鍵之1是理解清楚Intent的兩種基本用法:1種是顯式的Intent,即在構造Intent對象時就指定接收者;另外一種是隱式的Intent,即Intent的發送者在構造Intent對象時,其實不知道也不關心接收者是誰,有益于下降發送者和接收者之間的耦合。官方建議使用隱式Intent。上述屬性中,component屬性為直接類型,其他均為間接類型。

對顯式Intent,Android不需要去做解析,由于目標組件已很明確,Android需要解析的是那些隱式Intent,通過解析,將 Intent映照給可以處理此Intent的Activity、IntentReceiver或Service。        

相比與顯式Intent,隱式Intnet則涵蓄了許多,它其實不明確指出我們想要啟動哪個活動,而是指定1系列更加抽象的action和category等信息,然后交由系統去分析這個Intent,并幫我們找出適合的活動去啟動。

Activity 中 Intent Filter 的匹配進程 :

Intent 解析機制主要是通過查找已注冊在AndroidManifest.xml中的所有IntentFilter及其中定義的Intent,終究找到匹配的 Intent。在這個解析進程中,Android是通過Intent的action、type、category這3個屬性來進行判斷的,判斷方法以下:

  • 如果Intent指明定了action,則目標組件的IntentFilter的action列表中就必須包括有這個action,否則不能匹配;
  • 如果Intent沒有提供type,系統將從data中得到數據類型。和action1樣,目標組件的數據類型列表中必須包括Intent的數據類型,否則不能匹配。
  • 如果Intent中的數據不是content: 類型的URI,而且Intent也沒有明確指定它的type,將根據Intent中數據的scheme (比如 http: 或mailto:) 進行匹配。同上,Intent 的scheme必須出現在目標組件的scheme列表中。
  • 如果Intent指定了1個或多個category,這些種別必須全部出現在組建的種別列表中。比如Intent中包括了兩個種別:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目標組件必須最少包括這兩個種別。

Intent-Filter的定義

1些屬性設置的例子:

<action android:name="com.example.project.SHOW_CURRENT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="video/mpeg" android:scheme="http" . . . /> <data android:mimeType="image/*" /> <data android:scheme="http" android:type="video/*" />

完全的實例

<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的構造函數

公共構造函數:

1、Intent() 空構造函數

2、Intent(Intent o) 拷貝構造函數

3、Intent(String action) 指定action類型的構造函數

4、Intent(String action, Uri uri) 指定Action類型和Uri的構造函數,URI主要是結合程序之間的數據同享ContentProvider

5、Intent(Context packageContext, Class<?> cls) 傳入組件的構造函數,也就是上文提到的

6、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前兩種結合體

Intent有6種構造函數,3、4、5是最經常使用的,其實不是其他沒用!

Intent(String action, Uri uri) 的action就是對應在AndroidMainfest.xml中的action節點的name屬性值。在Intent類中定義了很多的Action和Category常量。

示例代碼2:

Intent intent = new Intent(Intent.ACTION_EDIT, null); startActivity(intent);

示例代碼2是用了第4種構造函數,只是uri參數為null。履行此代碼的時候,系統就會在程序主配置文件AndroidMainfest.xml中尋覓

<action android:name="android.intent.action.EDIT" />對應的Activity,如果對應為多個activity具有<action android:name="android.intent.action.EDIT" />此時就會彈出1個dailog選擇Activity。

如果是用示例代碼1那種方式進行發送則不會有這類情況。

利用Intent在Activity之間傳遞數據

     在Main中履行以下代碼:

Bundle bundle = new Bundle(); bundle.putStringArray("NAMEARR", nameArr); Intent intent = new Intent(Main.this, CountList.class); intent.putExtras(bundle); startActivity(intent);
在CountList中,代碼以下:
Bundle bundle = this.getIntent().getExtras(); String[] arrName = bundle.getStringArray("NAMEARR");

以上代碼就實現了Activity之間的數據傳遞!

總結說明

這篇文章是我剛開始學習Android時看到的,當時理解的不是很深入,現在再回頭看這篇文章總結的很詳細,在這里與大家分享。

1,調用web閱讀器

<pre name="code" class="java">Uri myBlogUri = Uri.parse("http://kuikui.javaeye.com"); returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);

2,地圖
Uri mapUri = Uri.parse("geo:38.899533,⑺7.036476"); returnIt = new Intent(Intent.ACTION_VIEW, mapUri);
3,挑唆打電話界面

Uri telUri = Uri.parse("tel:100861"); returnIt = new Intent(Intent.ACTION_DIAL, telUri);
4,直接撥打電話
Uri callUri = Uri.parse("tel:100861"); returnIt = new Intent(Intent.ACTION_CALL, callUri);
5,卸載
Uri uninstallUri = Uri.fromParts("package", "xxx", null); returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
6,安裝
Uri installUri = Uri.fromParts("package", "xxx", null); returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

7,播放

Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3"); returnIt = new Intent(Intent.ACTION_VIEW, playUri);

8,調用發郵件

Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com"); returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);
9,發郵件

returnIt = new Intent(Intent.ACTION_SEND); String[] tos = { "shenrenkui@gmail.com" }; String[] ccs = { "shenrenkui@gmail.com" }; returnIt.putExtra(Intent.EXTRA_EMAIL, tos); returnIt.putExtra(Intent.EXTRA_CC, ccs); returnIt.putExtra(Intent.EXTRA_TEXT, "body"); returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject"); returnIt.setType("message/rfc882"); Intent.createChooser(returnIt, "Choose Email Client");
10,發短信

Uri smsUri = Uri.parse("tel:100861"); returnIt = new Intent(Intent.ACTION_VIEW, smsUri); returnIt.putExtra("sms_body", "shenrenkui"); returnIt.setType("vnd.android-dir/mms-sms");

11,直接發郵件

Uri smsToUri = Uri.parse("smsto://100861"); returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri); returnIt.putExtra("sms_body", "shenrenkui");

12,發彩信

Uri mmsUri = Uri.parse("content://media/external/images/media/23"); returnIt = new Intent(Intent.ACTION_SEND); returnIt.putExtra("sms_body", "shenrenkui"); returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri); returnIt.setType("image/png");

用獲得到的Intent直接調用startActivity(returnIt)就ok了。

Intent用法實例

1、打開指定網頁:(直接復制的上面的代碼)

MainActivity.java中,監聽器部份的核心代碼以下:

button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); } });
第4行代碼:指定了Intent的action是 Intent.ACTION_VIEW,表示查看的意思,這是1個Android系統內置的動作;

第5行代碼:通過Uri.parse()方法,將1個網址字符串解析成1個Uri對象,再調用intent的setData()方法將這個Uri對象傳遞進去。

2、打電話:

【方式1】打開撥打電話的界面:

Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086")); startActivity(intent);

運行程序后,點擊按鈕,顯示以下界面:

【方式2】直接撥打電話:

Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:10086")); startActivity(intent);

要使用這個功能必須在配置文件中加入權限:(加1行代碼)

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.CALL_PHONE"/>

3、發送短信:

【方式1】打開發送短信的界面:action+type

Intent intent = new Intent(Intent.ACTION_VIEW); intent.setType("vnd.android-dir/mms-sms"); intent.putExtra("sms_body", "具體短信內容"); //"sms_body"為固定內容 startActivity(intent);

【方式2】打開發短信的界面(同時指定電話號碼):action+data

Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("smsto:18780260012")); intent.putExtra("sms_body", "具體短信內容"); //"sms_body"為固定內容 startActivity(intent);

4、播放指定路徑音樂:action+data+type

Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///storage/sdcard0/平凡之路.mp3"); ////路徑也能夠寫成:"/storage/sdcard0/平凡之路.mp3" intent.setDataAndType(uri, "audio/mp3"); //方法:Intent android.content.Intent.setDataAndType(Uri data, String type) startActivity(intent);

5、卸載程序:action+data(例如點擊按鈕,卸載某個利用程序,根據包名來辨認)

注:不管是安裝還是卸載,利用程序是根據包名package來辨認的。

Intent intent = new Intent(Intent.ACTION_DELETE); Uri data = Uri.parse("package:com.example.smyh006intent01"); intent.setData(data); startActivity(intent);

6、安裝程序:action+data+type

Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.fromFile(new File("/storage/sdcard0/AndroidTest/smyh006_Intent01.apk")); //路徑不能寫成:"file:///storage/sdcard0/···" intent.setDataAndType(data, "application/vnd.android.package-archive"); //Type的字符串為固定內容 startActivity(intent);

注:第2行的路徑不能寫成:"file:///storage/sdcard0/???",不然報錯以下:

綜上所述,完全版代碼以下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/button1_browsePage" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="browsePageClick" android:text="打開指定網頁"/> <Button android:id="@+id/button2_openDialPage" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="openDialPageClick" android:text="打開撥號面板"/> <Button android:id="@+id/button3_dialPhone" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="dialPhoneClick" android:text="直接撥打指定號碼"/> <Button android:id="@+id/button4_openMsgPage" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="openMsgPageClick" android:text="打開發短信的界面"/> <Button android:id="@+id/button5_sendMsg" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="sendMsgClick" android:text="給指定的人發短信"/> <Button android:id="@+id/button6_playMusic" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="playMusicClick" android:text="播放指定路徑音樂"/> <Button android:id="@+id/button7_uninstall" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="uninstallClick" android:text="卸載程序"/> <Button android:id="@+id/button8_install" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="installClick" android:text="安裝程序"/> </LinearLayout>

MainActivity.java代碼以下:

package com.example.m06intent01; import java.io.File; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //打開指定網頁 public void browsePageClick(View view){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.baidu.com/")); startActivity(intent); } //打開撥號面板 public void openDialPageClick(View view){ Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086")); startActivity(intent); } //直接撥打指定號碼 public void dialPhoneClick(View view){ Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:10086")); startActivity(intent); } //打開發短信的界面:action+type public void openMsgPageClick(View view){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setType("vnd.android-dir/mms-sms"); intent.putExtra("sms_body", "具體短信內容"); //"sms_body"為固定內容 startActivity(intent); } //打開發短信的界面(指定電話號碼):action+data public void sendMsgClick(View view){ Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("smsto:18780260012")); intent.putExtra("sms_body", "具體短信內容"); //"sms_body"為固定內容 startActivity(intent); } //播放指定路徑音樂 public void playMusicClick(View view){ Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///storage/sdcard0/平凡之路.mp3"); //路徑也能夠寫成:"/storage/sdcard0/平凡之路.mp3" intent.setDataAndType(uri, "audio/mp3"); //方法:Intent android.content.Intent.setDataAndType(Uri data, String type) startActivity(intent); } //卸載某個利用程序,根據包名來辨認 public void uninstallClick(View view){ Intent intent = new Intent(Intent.ACTION_DELETE); Uri data = Uri.parse("package:com.example.smyh006intent01"); intent.setData(data); startActivity(intent); } //安裝某個利用程序,根據apk的文件名來辨認 public void installClick(View view){ Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.fromFile(new File("/storage/sdcard0/AndroidTest/smyh006_Intent01.apk")); //路徑不能寫成:"file:///storage/sdcard0/···" intent.setDataAndType(data, "application/vnd.android.package-archive"); //Type的字符串為固定內容 startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }


1.無參數Activity跳轉

Intent it = new Intent(Activity.Main.this, Activity2.class);
startActivity(it);   

2.向下1個Activity傳遞數據(使用Bundle和Intent.putExtras)

Intent it = new Intent(Activity.Main.this, Activity2.class); Bundle bundle=new Bundle(); bundle.putString("name", "This is from MainActivity!"); it.putExtras(bundle); // it.putExtra(“test”, "shuju”); startActivity(it); // startActivityForResult(it,REQUEST_CODE);

對數據的獲得可以采取:

Bundle bundle=getIntent().getExtras(); String name=bundle.getString("name");

3.向上1個Activity返回結果(使用setResult,針對startActivityForResult(it,REQUEST_CODE)啟動的Activity)

Intent intent=getIntent(); Bundle bundle2=new Bundle(); bundle2.putString("name", "This is from ShowMsg!"); intent.putExtras(bundle2); setResult(RESULT_OK, intent);

4.回調上1個Activity的結果處理函數(onActivityResult)

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode==REQUEST_CODE){ if(resultCode==RESULT_CANCELED) setTitle("cancle"); else if (resultCode==RESULT_OK) { String temp=null; Bundle bundle=data.getExtras(); if(bundle!=null) temp=bundle.getString("name"); setTitle(temp); } } }

運行后,主界面以下:

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生

------分隔線----------------------------
分享到:
------分隔線----------------------------
為碼而活
積分:4237
15粉絲
7關注
欄目熱點
關閉
程序員人生
主站蜘蛛池模板: 国产成人免费视频 | 日本乱人伦片中文三区 | 国产九色在线 | 欧美一区二区三区精品影视 | 羞羞网站入口 | 1000部禁止18观看视频在线看 | 国产欧美精品三区 | 日韩欧美综合在线二区三区 | 欧美精品亚洲精品日韩1818 | 亚洲国产成人久久三区 | 精品国产福利在线观看网址2022 | 黄站在线观看 | 在线观看视频一区二区三区 | 日韩欧美亚洲综合一区二区 | 国产午夜人做人免费视频中文 | 国产一区二区三区免费在线视频 | 国产国语一级毛片在线放 | 欧美久久综合性欧美 | 欧美黄色毛片 | 伊人久久成人成综合网222 | 亚洲欧美一级夜夜爽w | 国内一区二区三区精品视频 | 久久看视频 | 亚洲视频在线观看不卡 | 国产成人精品亚洲一区 | 国产99精品一区二区三区免费 | 国产午夜精品免费一二区 | 欧美色精品 | 九九精品免视频国产成人 | 素人259luxu在线观看暴露 | 成人中文字幕在线高清 | 国产亚洲人成网站在线观看不卡 | 成人永久福利免费观看 | 久久机热这里只有精品 | poronovideos巴西极品 | 91在线精品亚洲一区二区 | 五月天婷婷一区二区三区久久 | 日本一区二区三区四区在线观看 | 成人夜夜嗨 | 免费在线观看黄色的网站 | 噜噜噜噜私人影院 |