## 意圖(Intent)
>[info] 簡述:
> Android中提供了Intent機制來協助應用間的交互與通訊,Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將Intent傳遞給調用的組件,并完成組件的調用。Intent不僅可用于應用程序之間,也可用于應用程序內部的Activity/Service之間的交互。因此,Intent在這里起著一個媒體中介的作用,專門提供組件互相調用的相關信息,實現調用者與被調用者之間的解耦。在SDK中給出了 Intent作用的表現形式為:
* 通過**Context.startActivity()** or**Activity.startActivityForResult(**) 啟動一個Activity;
* 通過**Context.startService()** 啟動一個服務,或者通過**Context.bindService(**) 和后臺服務交互;
* 通過廣播方法(比如 **Context.sendBroadcast()**,**Context.sendOrderedBroadcast()**, **Context.sendStickyBroadcast**()) 發給broadcast receivers
### 1,Intent的屬性
**(1)Action**,也就是**要執行的動作**
**(2)Data**,也就是執行動作要操作的數據
Android 中采用指向數據的一個URI來表示,如在聯系人應用中,一個指向某聯系人的URI可能為:`content://contacts/1 `。 對于不同的動作,其 URI數據的類型是不同的(可以設置type屬性指定特定類型數據),如ACTION_EDIT指定Data為文件URI,打電話為tel:URI , 訪問 網絡為http:URI ,而由content provider提供的數據則為content: URIs。
**(3)type(數據類型)**,顯式指定Intent的數據類型(MIME)。一般Intent的數據類型能夠根據數據本身進行判定,但是通過設置這個屬性,可以強制采用顯式指定的類型而不再進行推導。
**(4)category(類 別)**,被執行動作的附加信息。例如 LAUNCHER_CATEGORY 表示Intent 的接受者應該在Launcher中作為頂級應用出現;而ALTERNATIVE_CATEGORY表示當前的Intent是一系列的可選動作中的一個,這 些動作可以在同一塊數據上執行。
**(5)component(組 件)**,指定Intent的的目標組件的類名稱。通常 Android會根據Intent 中包含的其它屬性的信息,比如action、data/type、category進行查找,最終找到一個與之匹配的目標組件。但是,**如果 component這個屬性有指定的話,將直接使用它指定的組件,而不再執行上述查找過程。指定了這個屬性以后,Intent的其它所有屬性都是可選的。**
**(6)extras(附加信息)**,是其它所有附加信息的集合。使用extras可以為組件提供擴展信息,比如,如果要執行“發送電子郵件”這個動作,可以將電子郵件的標題、正文等保存在extras里,傳給電子郵件發送組件。
### 2,Intent 的基本用法
- **顯式的Intent**
即在構造Intent對象時就指定接收者;
eg:
~~~
//顯式意圖 必須指定動作的執行者
Intent intent = new Intent();
//顯式的指定了要激活的組件的包名和類名
intent.setClassName( "com.android.calculator2", "com.android.calculator2.Calculator" );
startActivity(intent);
~~~
- **隱式的Intent**,
即Intent的發送者在構造Intent對象時,并不知道也不關心接收者是誰,有利于降低發送者和接收者之間的耦合。
eg:
~~~
//隱式意圖 只需要指定動作,解耦合
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com" ));
startActivity(intent);
~~~
### 3,使用自定義Intent(隱式意圖)
自定義隱式意圖
~~~
<activity
android:name="com.it360.mobilesafe.activity.LockOneKeyActivity"
android:label="@string/title_activity_lock_one_key" >
<intent-filter >
<action android:name="com.it360.mobilesafe.LOCKONEKEY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter >
</activity >
~~~
使用自定義意圖
~~~
Intent shortcutIntent = new Intent();
shortcutIntent.setAction("com.it360.mobilesafe.LOCKONEKEY");
shortcutIntent.addCategory(Intent.CATEGORY_DEFAULT);
~~~
### 4,Intent 的解析機制(隱式意圖)
對于顯式Intent,Android不需要去做解析,因為目標組件已經很明確,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,系統將從data中得到數據類型。和action一樣,目標組件的數據類型列表中必須包含Intent的數據類型,否則不能匹配。
* 如果Intent中的數據不是content: 類型的URI,而且Intent也沒有明確指定它的type,將根據Intent中數據的scheme (比如 http: 或者mailto:) 進行匹配。同上,Intent 的scheme必須出現在目標組件的scheme列表中。
* 如果Intent指定了一個或多個category,這些類別必須**全部**出現在組建的類別列表中。比如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>
~~~
### 5,Intent 用法實例
**①、無參數Activity跳轉**
~~~
Intent it = new Intent(Activity.Main.this, Activity2.class);
startActivity(it);
~~~
**②、向下一個Activity傳遞數據(使用Bundle和Intent.putExtras)**
>[info] 注意:Activity間的數據通信,對于數據量比較大的,避免使用 Intent+Parcelable的方式,可以考慮EventBus等替代方案,以免造成TransactionTooLargeException。
~~~
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");
~~~
**③、向上一個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);
~~~
**④、回調上一個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);
}
}
}
~~~
### 6,Intent的Component屬性(顯示意圖)
前面講到,component(組件),指定Intent的的目標組件的類名稱。**通常 Android會根據Intent 中包含的其它屬性的信息,比如action、data/type、category進行查找,最終找到一個與之匹配的目標組件。但是,如果 component這個屬性有指定的話,將直接使用它指定的組件,而不再執行其他查找過程。指定了這個屬性以后,Intent的其它所有屬性都是可選的。**
**Component 是用來查找組件的,我們可以根據setComponent()、setClass()、setClassName()來設置組件的名稱。通過getComponet(ComponetName componetName)來獲取組件。**
方法、一:
~~~
Intent intent = new Intent();
intent.setClassName(<package name>, <class name>); //簡寫
startActivity(intent);
~~~
方法、二:
~~~
Intent i=new Intent;
ComponentName com= new ComponentName(<Package Name> , <Calss Name>);
i.setComponent(com);
startActivity(i);
~~~
**CompentName對象**
這是一個可以注冊系統組件的對象,,可以注冊四大組件
eg:
**啟動htmlviewer,并打開指定的一個文件**
~~~
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.android.htmlviewer",
"com.android.htmlviewer.HTMLViewerActivity");
intent.setComponent(cn);
Uri uri = Uri.fromFile(new File("/sdcard/demo.txt"));
intent.setDataAndType(uri, "text/plain");
startActivity(intent);
~~~
### 7,[PendingIntent](https://developer.android.com/reference/android/app/PendingIntent.html) 延遲意象
**PendingIntent這個類用于處理即將發生的事情。比如在通知Notification中用于跳轉頁面,但不是馬上跳轉**。
**Intent 是及時啟動,intent 隨所在的activity 消失而消失。**
PendingIntent 可以看作是對intent的包裝,**通常通過getActivity,getBroadcast ,getService來得到pendingIntent的實例**,當前activity并不能馬上啟動它所包含的intent,而是在外部執行 pendingIntent時,調用intent的。正由于pendingIntent中 保存有當前App的Context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執行pendingIntent里的Intent, 就算在執行時當前App已經不存在了,也能通過存在pendingIntent里的Context照樣執行Intent。另外還可以處理intent執行后的操作。常和Alermanger 和NotificationManager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之間傳遞數據,而**Pendingintent,一般用在 Notification上,可以理解為延遲執行的intent,PendingIntent是對Intent一個包裝。**
1. GSM網絡中android發送短信示例
~~~
String msg ="你好,美女";
String number = "135****6784";
SmsManager sms = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
sms.sendTextMessage(number, null, msg, pi, null);
Toast.makeText(SmsActivity.this,"發送成功",Toast.LENGHT_LONG).show();
~~~
- 代碼解釋
- PendingIntent就是**一個Intent的描述**,我們可以把這個描述交給別的程序,別的程序根據這個描述在后面的別的時間做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相當于PendingIntent代表了Intent)。本例中別的程序就是發送短信的程序,短信發送成功后要把intent廣播出去 。
~~~
SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text,
PendingIntent sentIntent, PendingIntent deliveryIntent)
~~~
- 參數解釋:
- 1)PendingIntent **sentIntent**:當短信發出時,成功的話sendIntent會把其內部的描述的intent廣播出去,否則產生錯誤代碼并通過android.app.PendingIntent.OnFinished進行回調,這個參數最好不為空,否則會存在資源浪費的潛在問題;
- 2)PendingIntent **deliveryIntent**:是當消息已經傳遞給收信人后所進行的PendingIntent廣播。
查看PendingIntent 類可以看到許多的Send函數,就是PendingIntent在進行被賦予的相關的操作。
實例:
~~~
public class NotificationActivity extends Activity {
private static final int NOTIFICATION_ID = 1;
private NotificationManager myNotificationManager;
public void onCreate(Bundle saveIntenceState) {
……
myNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.smallIcon;
String tickerText = “Get New Message”;
String expandTitle = “NewMessage”;
String expandContent = “The notification content abstract when it expanded. ”;
Context context = getApplicationContext();
Notification notifiMessage =
new Notification(icon,tickerText,System.currentTimeMillis());
Intent requestIntent = new Intent(OrginalActivity.class, DestinationActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, requestIntent, 0);
notifiMessage.setLatestEventInfo(context, expandTitle, expandContent, pendIntent);
myNotificationManager.notify(NOTIFICATION_ID, notifiMessage);
}
}
~~~
這里實例化一個 Notification對象,之后通過NotificationManager將通知顯示出來。需要注意通知管理器需要將每一個管理的通知都設置一個唯一的ID,這個ID由開發者自行指定。對于顯示的通知調用setLatestEventInfo()方法設置通知展開后將要顯示的標題和內容,將顯式 Intent 傳入 getActivity()方法獲得 PendingIntent對象實例作為參數傳入設置單擊事件處理消息。當用戶點擊該通知時,系統會使用相應的 Intent 消息加載并啟動目標組件。
### 8,Intent 常見應用
**顯示網頁**
~~~
Uri uri = Uri.parse("http://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);
~~~
- 其他 geo URI 範例
1. //geo:latitude,longitude
2. //geo:latitude,longitude?z=zoom
3. //geo:0,0?q=my+street+address
4. //geo:0,0?q=business+near+city
5. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
**路徑規劃**
~~~
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);
~~~
//where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456
**打電話**
- **①、叫出撥號程序**
~~~
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
~~~
- **②、直接打電話出去**
~~~
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
~~~
> 用這個,要在 AndroidManifest.xml 中,加上 `<uses-permission id="android.permission.CALL_PHONE" />`
**傳送SMS/MMS**
- ①、調用短信程序
~~~
Intent it = new Intent(Intent.ACTION_VIEW, uri);
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);
~~~
- ③、傳送 MMS
~~~
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);
~~~
**傳送 Email**
①、
~~~
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"));
~~~
**播放多媒體**
~~~
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 相關**
1. 尋找某個應用
~~~
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
~~~
> where pkg_name is the full package path for an application
2. 顯示某個應用的相關信息
~~~
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
~~~
> where app_id is the application ID, find the ID by clicking on your application on Market home page, and notice the ID from the address bar
- Uninstall 應用程序
~~~
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
~~~
### 9,Android Intent Action 一覽表








- 前言
- Android系統的體系結構
- Dalvik VM 和 JVM 的比較
- Android 打包應用程序并安裝的過程
- Android ADB工具
- Android應用開發
- Android UI相關知識總結
- Android 中window 、view、 Activity的關系
- Android應用界面
- Android中的drawable和bitmap
- AndroidUI組件adapterView及其子類和Adapter的關系
- Android四大組件
- Android 數據存儲
- SharedPreference
- Android應用的資源
- 數組資源
- 使用Drawable資源
- Material Design
- Android 進程和線程
- 進程
- 線程
- Android Application類的介紹
- 意圖(Intent)
- Intent 和 Intent 過濾器(Google官網介紹)
- Android中關于任務棧的總結
- 任務和返回棧(官網譯文)
- 總結
- Android應用安全現狀與解決方案
- Android 安全開發
- HTTPS
- 安卓 代碼混淆與打包
- 動態注入技術(hook技術)
- 一、什么是hook技術
- 二、常用的Hook 工具
- Xposed源碼剖析——概述
- Xposed源碼剖析——app_process作用詳解
- Xposed源碼剖析——Xposed初始化
- Xposed源碼剖析——hook具體實現
- 無需Root也能Hook?——Depoxsed框架演示
- 三、HookAndroid應用
- 四、Hook原生應用程序
- 五、Hook 檢測/修復
- Android 應用的逆向與加固保護技術
- OpenCV在Android中的開發
- Android高級開發進階
- 高級UI
- UI繪制流程及原理
- Android新布局ConstraintLayout約束布局
- 關鍵幀動畫
- 幀動畫共享元素變換
- Android異步消息處理機制完全解析,帶你從源碼的角度徹底理解
- Android中為什么主線程不會因為Looper.loop()里的死循環卡死?
- 為什么 Android 要采用 Binder 作為 IPC 機制?
- JVM 中一個線程的 Java 棧和寄存器中分別放的是什么?
- Android源碼的Binder權限是如何控制?
- 如何詳解 Activity 的生命周期?
- 為什么Android的Handler采用管道而不使用Binder?
- ThreadLocal,你真的懂了嗎?
- Android屏幕刷新機制