Intent七大属性
生活随笔
收集整理的這篇文章主要介紹了
Intent七大属性
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、Intent的作用是什么?
???1、Intent?用于封裝程序的”調(diào)用意圖“。兩個(gè)Activity之間,可以把需要交換的數(shù)據(jù)封裝成Bundle對象,然后使用Intent攜帶Bundle對象,實(shí)現(xiàn)兩個(gè)Activity之間的數(shù)據(jù)交換; ? ?2、Intent還是各種應(yīng)用程序組件之間通信的重要媒介。不管想啟動(dòng)一個(gè)Service、Acitivity還是BroadcastReceiver,Android均使用統(tǒng)一的Intent對象來封裝這種”啟動(dòng)意圖“。很明顯使用Intent提供了一致的編程模型; ? ?3、Intent還有一個(gè)好處,如果應(yīng)用程序只是想啟動(dòng)具有某種特征的組件,并不想和某個(gè)具體的組件耦合,則可以通過在intent-filter中配置相應(yīng)的屬性進(jìn)行處理,與stucts2中的MVC框架思路類似。 ? ??? ?//①.第一種方式:開啟另一個(gè)Activity?//Intent?intent=new?Intent(this,SecondActivity.class);Intent?intent=new?Intent();
ComponentName?component=new?ComponentName(this,?SecondActivity.class);
ComponentName?component=new?ComponentName(this,?"com.qianfeng.day06_intentattribute01.SecondActivity");
ComponentName?component=new?ComponentName("com.qianfeng.day06_intentattribute01",?"com.qianfeng.day06_intentattribute01.SecondActivity");intent.setComponent(component);
intent.setClass(this,?SecondActivity.class);
intent.setClassName(this,?"com.qianfeng.day06_intentattribute01.SecondActivity");?
intent.setAction("ergouzisimida");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
? ??? ??1、指定了ComponentName屬性的Intent已經(jīng)明確了它將要啟動(dòng)哪個(gè)組件,因此這種Intent被稱為顯式Intent,沒有指定ComponentName屬性的Intent被稱為隱式Intent。隱式Intent沒有明確要啟動(dòng)哪個(gè)組件,應(yīng)用會(huì)根據(jù)Intent指定的規(guī)則去啟動(dòng)符合條件的組件。 ? ??? ??2、示例代碼:
Intent?intent?=?new?Intent();ComponentName?cName?=?new?ComponentName(MainActivity.this,NextActivity.class);intent.setComponent(cName);startActivity(intent);//實(shí)際上,以上的寫法都被簡化為以下寫法:Intent?intent?=?new?Intent(MainActivity.this,NextActivity.class);????????startActivity(intent);//也就是說,平時(shí)我們最常用的Intent頁面跳轉(zhuǎn)的寫法就調(diào)用的是顯式Intent。
Intent?intent?=?new?Intent();?intent.setAction("com.train.task01.editactivity");?startActivity(intent);
<activity?android:name="com.train.taskstack01.EditActivity">??<intent-filter>??????<action?android:name="com.train.task01.editactivity"?/>??????<category?android:name="android.intent.category.DEFAULT"?/>?????</intent-filter></activity>
????????????<intent-filter>????????????????<action?android:name="android.intent.action.MAIN"?/>????????????????<category?android:name="android.intent.category.LAUNCHER"?/>????????????</intent-filter>
ACTION_EDIT:?(android.intent.action.EDIT)?編輯指定數(shù)據(jù)。? ACTION_DIAL:?(android.intent.action.DIAL)?顯示撥號(hào)面板。
ACTION_CALL:?(android.intent.action.CALL)?直接呼叫Data中所帶的號(hào)碼。??
ACTION_ANSWER:?(android.intent.action.ANSWER)?接聽來電。??
ACTION_SEND:?(android.intent.action.SEND)?向其他人發(fā)送數(shù)據(jù)(例如:彩信/email)。??
ACTION_SENDTO:??(android.intent.action.SENDTO)?向其他人發(fā)送短信。 ACTION_SEARCH:?(android.intent.action.SEARCH)?執(zhí)行搜索。??
ACTION_GET_CONTENT:?(android.intent.action.GET_CONTENT)?讓用戶選擇數(shù)據(jù),并返回所選數(shù)據(jù)。 <3>? Category屬性 Category屬性為Action增加額外的附加類別信息。CATEGORY_LAUNCHER意味著在加載程序的時(shí)候Acticity出現(xiàn)在最上面,而CATEGORY_HOME表示頁面跳轉(zhuǎn)到HOME界面。 實(shí)現(xiàn)頁面跳轉(zhuǎn)到HOME界面的代碼:【記憶】
Intent?intent?=?new?Intent();?intent.setAction(Intent.ACTION_MAIN);?intent.addCategory(Intent.CATEGORY_HOME);?startActivity(intent);?
CATEGORY_DEFAULT:?(android.intent.category.DEFAULT)?Android系統(tǒng)中默認(rèn)的執(zhí)行方式,按照普通Activity的執(zhí)行方式執(zhí)行。? CATEGORY_HOME:?(android.intent.category.HOME)?設(shè)置該組件為Home?Activity。
CATEGORY_PREFERENCE:?(android.intent.category.PREFERENCE)?設(shè)置該組件為Preference。? CATEGORY_LAUNCHER:?(android.intent.category.LAUNCHER)?設(shè)置該組件為在當(dāng)前應(yīng)用程序啟動(dòng)器中優(yōu)先級最高的Activity,通常與入口ACTION_MAIN配合使用。CATEGORY_BROWSABLE:?(android.intent.category.BROWSABLE)?設(shè)置該組件可以使用瀏覽器啟動(dòng)。 <4>Data屬性
Data屬性通常用于向Action屬性提供操作的數(shù)據(jù)。Data屬性的值是個(gè)Uri對象。 ? ? ?Uri的格式如下:scheme://host:port/path Intent利用Action屬性和Data屬性啟動(dòng)Android系統(tǒng)內(nèi)置組件代碼: //發(fā)送短信
public?void?sendSMS(View?view){???Intent?intent=new?Intent(Intent.ACTION_SENDTO);???intent.setData(Uri.parse("smsto:13366201398"));???intent.putExtra("sms_body",?"發(fā)送短信的內(nèi)容");???startActivity(intent);}
<uses-permission?android:name="android.permission.CALL_PHONE"?/>public?void??callPhone(View?view){???Intent?intent=new?Intent(Intent.ACTION_CALL,Uri.parse("tel:13366201398"));???startActivity(intent);}
<uses-permission?android:name="android.permission.INTERNET"?/>
public?void?openHtml(View?view){???Intent?intent=new?Intent(Intent.ACTION_VIEW,Uri.parse("https://www.baidu.com/"));???startActivity(intent);}
public?void?openImage(View?view){???Intent?intent=new?Intent(Intent.ACTION_VIEW,Uri.parse("http://192.168.129.94:8080/images/s1.jpg"));???startActivity(intent);}
public?void?openAlbum(View?view){???Intent?intent?=?new?Intent();????intent.setAction(Intent.?ACTION_GET_CONTENT?);???intent.setType(?"image/*"?);????startActivityForResult(wrapperIntent,?100);??}
Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE);?startActivityForResult(intent,?100);??
smsto://:短息數(shù)據(jù)格式,后跟短信接收號(hào)碼。??
content://:內(nèi)容數(shù)據(jù)格式,后跟需要讀取的內(nèi)容。??
file://:文件數(shù)據(jù)格式,后跟文件路徑。??
market://search?q=pname:pkgname:市場數(shù)據(jù)格式,在Google?Market里搜索包名為pkgname的應(yīng)用。??
geo://latitude,?longitude:經(jīng)緯數(shù)據(jù)格式,在地圖上顯示經(jīng)緯度所指定的位置。 <5>Type屬性 1、Type屬性用于指定Data所指定的Uri對應(yīng)的MIME類型。MIME只要符合“abc/xyz”這樣的字符串格式即可。 2、?Intent利用Action、Data和Type屬性啟動(dòng)Android系統(tǒng)內(nèi)置組件的代碼: 播放視頻:
Intent?intent?=?new?Intent();?Uri?uri?=?Uri.parse("file:///sdcard/media.mp4");?intent.setAction(Intent.ACTION_VIEW);intent.setDataAndType(uri,?"video/*");?startActivity(intent);
Intent可調(diào)用addFlags()方法來為Intent添加控制標(biāo)記 實(shí)例:
Intent?intent?=?new?Intent(this,?MainActivity.class);?//將Activity棧中處于MainActivity主頁面之上的Activity都彈出。?intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);?startActivity(intent);
Intent??intent??=?new?Intent();intent.setAction(Intent.ACTION_VIEW);intent.setType("vnd.android-dir/mms-sms");?intent.putExtra("sms_body",?"信息內(nèi)容...");?startActivity(intent);
Uri?uri?=?Uri.parse("smsto:13200100001");?Intent??intent??=?new?Intent();?intent.setAction(Intent.??ACTION_SENDTO?);intent.setData(uri);intent.putExtra("sms_body",?"信息內(nèi)容...");?startActivity(?intent?);
Uri?uri?=?Uri.parse("content://media/external/images/media/23");?//設(shè)備中的資源(圖像或其他資源)?Intent?intent?=?new?Intent();?intent.setAction(Intent.??ACTION_SEND?);intent.setType("image/png");?intent.putExtra("sms_body",?"內(nèi)容");?intent.putExtra(Intent.EXTRA_STREAM,?uri);?startActivity(it);
Intent?intent=new?Intent();?intent.setAction(Intent.??ACTION_SEND?);String[]?tos={"android1@163.com"};?String[]?ccs={"you@yahoo.com"};?intent.putExtra(Intent.EXTRA_EMAIL,?tos);?intent.putExtra(Intent.EXTRA_CC,?ccs);intent.putExtra(Intent.EXTRA_TEXT,?"The?email?body?text");?intent.putExtra(Intent.EXTRA_SUBJECT,?"The?email?subject?text");?intent.setType("message/rfc822");?startActivity(Intent.createChooser(intent,?"Choose?Email?Client"));
Intent?intent?=?new?Intent();?intent.setAction(Intent.?ACTION_GET_CONTENT?);intent.setType(?"image/*"?);?Intent?wrapperIntent?=?Intent.createChooser(intent,?null);startActivityForResult(wrapperIntent,?requestCode); ?
轉(zhuǎn)載于:https://www.cnblogs.com/android-blogs/p/5690844.html
總結(jié)
以上是生活随笔為你收集整理的Intent七大属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: codeforces水题100道 第二十
- 下一篇: 斗破苍穹第二季什么时候上映