Android中Intent的使用
Intent可以看成是一個“中介”,它不僅可以實現不同元件之間的互動和通訊,還可以實現應用程式之間的互動!
Intent根據是否明確接收的物件,而分為兩種情況,第一種顯式的Intent,就是在構造的時候指定了接收者的,第二種隱式的Intent,就是沒有在構造的時候指定接收者, 對於隱式的Intent來說,就需要進行解析,Intent解析機制是通過查詢註冊在AndroidManifest.xml檔案中的所有IntentFilter,以及IntentFilter所定義的Intent,找到最匹配的Intent。
在解析過程中,Android通過判斷Intent的Action、Type、Category這三個屬性,從而找出最匹配的Intent,具體的判斷方法如下:
1,如果Intent指明瞭Action,則目標元件IntentFilter的Action列表中就必須包含有這個Action,否則不能匹配;
2,如果Intent沒有提供Type,系統將從Data中得到資料型別。目標元件的資料型別列表中必須包含Intent的資料型別,否則不能匹配。
3,如果Intent中的資料不是content: URI,而且Intent也沒有明確指定它的Type,將根據Intent中資料的scheme (比如 http: 或者mailto:) 進行匹配。同理,Intent 的scheme必須出現在目標元件的scheme列表中,否則不能匹配。
4,如果Intent指定了一個或多個Category,這些類別必須全部出現在目標元件的類別列表中,否則不能匹配。
Intent的屬性
1.Action(執行的動作)
2.Data(執行動作所需的資料)
3.Category(執行動作的附加資訊)
4.Type(顯式的指定Intent的資料型別)
5.Compent(指定Intent目標元件的類名稱)
6.Extra(其它所有附加資訊的集合)
7.其中最常用的還是Action和Data。
一,Action(執行的動作)
在SDK中的動作如下:
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. (時區的設定已更改。) |
二,Data(執行動作所需的資料)
Data使用指向資料的URI來表示。比如,在聯絡人應用中,指向聯絡人列表的URI是content://contacts/people/。
三, Type(顯式的指定Intent的資料型別)
對於不同的動作,其URI資料的型別不同。Intent的資料型別能夠根據其資料本身進行判定,但是通過設定這個屬性,可以強制採用顯式指定的型別。
四, Category(執行動作的附加資訊)
Category表示執行動作的附加資訊。比如,當我們想要讓所執行的動作被接收後,作為頂級應用而位於其他所有應用的最上層,並可以使用附加資訊LAUNCHER_CATEGORY來實現。
五, Component(指定Intent目標元件的類名稱)
Component用於指定Intent目標元件的類名稱。通常,Android會根據Intent 中所包含的其它屬性資訊(比如Action、Data/Type、Category)進行查詢,並找到一個與之匹配的目標元件。但是,如果我們設定了Component屬性,明確的指定了Intent目標元件的類名稱,那麼上述查詢過程將不需要執行。
六,Extras(其它所有附加資訊的集合)
使用extras可以為元件提供擴充套件資訊。
Intent的使用
一,啟動應用程式
1,撥打電話
需要在AndroidManifest.xml裡面新增撥打電話的許可權
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
//撥打電話的方法
public void call(){
Intent intent = new Intent();//隱式
intent.setAction(Intent.ACTION_CALL);//撥打電話的請求
intent.setData(Uri.parse("tel:" + "10086"));//撥打的號碼這裡的格式必須是"tel:+“電話號碼”"
startActivity(intent);//啟動
}
2,啟動微信
try{
Intent intent=getPackageManager().getLaunchIntentForPackage("com.tencent.mm");//獲取intent。"com.tencent.mm"是微信的包名
startActivity(intent);//啟動
}catch (NullPointerException n){//出現異常則沒有安裝微信
Toast.makeText(this, "你沒有安裝微信", Toast.LENGTH_SHORT).show();
}
二,啟動Activity()
startActivity();
1,跳轉到下一個Activity
Intent intent=new Intent(this,NewActivity.class);//顯式,指定了接收者
startActivity(intent);
2,跳轉到下一個Activity(帶值);
public void tz(){//跳轉方法
Intent intent=new Intent(this,NewActivity.class);
Bundle bundle=new Bundle();//使用Bundle
bundle.putString("name","常常大傢伙");//使用鍵值對來傳遞
bundle.putString("pwd","1200");
intent.putExtras(bundle);
startActivity(intent);
}
NewActivity程式碼如下:
public class NewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
Intent intent=getIntent();//獲取Intent
Bundle bundle=intent.getExtras();//獲取Bundle
String name=bundle.getString("name");//通過鍵值對獲取值
String pwd=bundle.getString("pwd");
}
}
3,當Activity銷燬時,返回給上一個Activity值
public class IntentActivity extends AppCompatActivity {
private int BZ=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
ButterKnife.bind(this);//繫結黃油刀
}
@OnClick(R.id.tz)//跳轉,這是使用的黃油刀,和監聽是一樣的效果,
public void tz(){
Intent intent=new Intent(this,NewActivity.class);
Bundle bundle=new Bundle();
bundle.putString("name","常常大傢伙");
bundle.putString("pwd","1200");
intent.putExtras(bundle);
startActivityForResult(intent,BZ);//需要返回值的啟動方式,這裡的1000是返回回撥的requestCode引數
}
@Override
//獲取返回值的回撥方法,requestCode是跳轉到NewActivity時的int的值用於判斷,
// resultCode則是NewActivity的返回時的int值
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==BZ&&resultCode==2000){
String a=data.getStringExtra("finish");//獲取data返回的值
Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
}
}
}
NewActivity程式碼如下:
public class NewActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String name=bundle.getString("name");
String pwd=bundle.getString("pwd");
button= (Button) findViewById(R.id.buttont);
button.setOnClickListener(this);//開啟監聽
}
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra("finish","得到了返回值");
setResult(2000,intent);//返回intent,和int值(2000)用於判斷也就是返回回撥的resultCode引數
finish();
}
}
三,啟動Service
startService();
bindService();
啟動Service(第一種)
// 建立Intent
Intent intent = new Intent(MainActivity.this, BindService.class);
// 啟動該Service
startService(intent);//stopService(intent);為停止Service
啟動Service(第二種)
// 建立Intent
Intent intent = new Intent(MainActivity.this, BindService.class);
// 繫結Service
bindService(conn); //unbindService(conn);解除繫結
/----------------------------------------------------------------------------------/
// 連線物件
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("Service", "連線成功!");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("Service", "斷開連線!");
}
};
具體程式碼請查閱Service的使用
四,啟動Broadcast
Intent intent = new Intent();//隱式
intent.setAction("staticreceiver");//設定Action,這裡的staticreceiver要和清單檔案的name一樣
intent.putExtra("msg", "靜態廣播接收成功");//新增附加資訊
sendBroadcast(intent);//普通廣播的傳送廣播方法
//sendOrderedBroadcast(intent,null);有序廣播的傳送方法
具體程式碼請查閱Broadcast廣播的使用