接上節(jié)繼續(xù)。。版權所有:飛雪無情,轉載請注明出處:
http://flysnow.iteye.com/blog/978225
Android開發(fā)技術交流群86686524 (已滿)請加120059404
Android系列教程目錄:
-
Android教程之一:Window下搭建Android開發(fā)環(huán)境
-
Android教程之二:Linux下搭建Android開發(fā)環(huán)境
-
Android教程之三:第一個Android應用,HelloWorld
-
Android系列教程之四:Android項目的目錄結構
-
Android系列教程之五:Activity的生命周期
-
Android系列教程之六:TextView小組件的使用--附帶超鏈接和跑馬燈效果
-
Android系列教程之七:EditText使用詳解-包含很多教程上看不到的功能演示
-
Android系列教程之八:ListView組件的使用
-
Android系列教程之九:GridView組件的使用
-
Android系列教程之十:Intents and Intent Filters(一)
-
Android系列教程之十一:Intents and Intent Filters(二)
數(shù)據(jù)(data)檢測
data標記也是在intent-filter中定義的,大致格式如下:
<intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:mimeType="text/plain"></data> </intent-filter>
??? ?? 每個data定義一個URI和數(shù)據(jù)類型(MIME),URI由4個屬性來定義,分別是android:scheme,android:host,android:port,android:path..這個四個屬性構成如下格式的URI: scheme://host:port/path ?? 如:content://com.flysnow.intent:8080/show/view。其中content就是scheme,com.flysnow.intent就是host,8080就是port,show/view就是path...如果有經常使用ContentProvider的應該熟悉。。我們經常定義的authority不就是host+port嗎?還有這幾個元素都是可選的,但是不是隨便用就可以的,port要依賴于host,沒有host,port就會被忽略,不起作用,同樣,如果要使用host+port(authority)就必須指定scheme。而path則依賴于scheme和authority。。
?????? 還有一個很重要的類型就是mimeType,這個屬性用于指定內容的類型,也就是這個組件可以處理哪些類型的內容。。如text/plain表示無格式文本類型,mimeType也支持通配符,使用text/*則表示所有文本類型。通過使用它,你可以很方便的開發(fā)出關聯(lián)打開諸如txt文件,pdf文件的應用。后面的兩個自理將會演示txt文件查看器,圖片查看器的例子。。MIME可以參考http://www.w3school.com.cn/media/media_mimeref.asp。這里有所有的內容類型的定義。。
開發(fā)實例-撥打電話,text閱讀器和圖片查看器
下面通過一個例子來演示data的檢測,項目名為Intents,應用名為Intents and Filters,運行在Android2.2版本上.主啟動Activity為IntentsTestList。例子包括以下演示:
- 通過發(fā)送intent的方式“打開撥號界面并輸入電話123456”。
- 創(chuàng)建一個Text文件閱讀器
- 創(chuàng)建一個圖片查看器
首先我們實現(xiàn)第一項,修改IntentsTestList類如下:
/** * Intents測試列表類 * @author 飛雪無情 * @since 2011-3-14 */ public class IntentsTestList extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //定義ListAdapter setListAdapter(new SimpleAdapter(this, getData(), android.R.layout.simple_list_item_1, new String[] { "title" },new int[] {android.R.id.text1})); getListView().setTextFilterEnabled(true); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { Intent intent=(Intent)getData().get(position).get("intent"); Boolean isActivity=(Boolean)getData().get(position).get("isActivity"); if(isActivity){ startActivity(intent); }else{ sendBroadcast(intent); } } /** * 返回ListView需要的數(shù)據(jù) * @return ListView需要的數(shù)據(jù) */ private List<Map<String,Object>> getData() { List<Map<String,Object>> data=new ArrayList<Map<String,Object>>(); addItem(data, "打開撥號界面并輸入電話123456", new Intent(Intent.ACTION_DIAL, Uri.parse("tel://123456")), true); return data; } /** * 給ListView添加數(shù)據(jù) * @param data 存儲數(shù)據(jù)的List * @param name 要顯示的Title * @param intent 單擊某一項時要啟動的Activity * @param isActivity 啟動的是否是Activity,true是,false為廣播 */ private void addItem(List<Map<String,Object>> data, String name, Intent intent,boolean isActivity) { Map<String, Object> temp = new HashMap<String, Object>(); temp.put("title", name); temp.put("intent", intent); temp.put("isActivity", isActivity); data.add(temp); } }
?這時我們運行程序,單擊“打開撥號界面并輸入電話123456”就會打開系統(tǒng)的自帶的撥號界面,并且默認已經錄入了要撥打的號碼“123456”。效果圖如下:
?
? 然后我們實現(xiàn)第二功能-txt文件閱讀器
新建TextWatcherActivity代碼如下:
/** * 顯示文本的Activity * @author 飛雪無情 * @since 2011-3-24 */ public class TextWatcherActivity extends Activity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTextView=new TextView(this); setContentView(mTextView); } @Override protected void onStart() { super.onStart(); Intent txtIntent=getIntent(); Uri data=txtIntent.getData(); String txt; try { txt = readTxt(data); } catch (IOException e) { txt="打開txt文件異常"; } mTextView.setText(txt); } /** * 讀取txt文本 * @param txtUri * @return * @throws IOException */ private String readTxt(Uri txtUri) throws IOException{ BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(txtUri),Charset.forName("GBK"))); StringBuilder txt=new StringBuilder(); String buf=""; while((buf=bufferedReader.readLine())!=null){ txt.append(buf).append("\n"); } return txt.toString(); } }
?然后在AndroidManifest.xml中加入如下定義:
<activity android:name=".TextWatcherActivity" android:label="查看TXT文件"> <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:mimeType="text/plain"></data> </intent-filter> </activity>
?
這樣在單擊txt文件的時候就可以選擇我們的這個Activity對txt文件處理,顯示其內容.我們新建一個1.txt文件,寫上一些內容,放在我們的sd卡中,使用文件管理工具查看這個txt文件,會彈出如下圖的提示,看到我們剛剛做的《TXT閱讀器》了吧。
最后實現(xiàn)第三個功能--圖片查看器
新建ImageWatcherActivity,代碼如下:
/** * 顯示文本的Activity * @author 飛雪無情 * @since 2011-3-24 */ public class ImageWatcherActivity extends Activity { private final String IMAGE_URI_KEY="imageUriKey"; private Uri image; private ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mImageView=new ImageView(this); setContentView(mImageView); } @Override protected void onStart() { super.onStart(); Intent txtIntent=getIntent(); image=txtIntent.getData(); //對于大圖片未做優(yōu)化處理 mImageView.setImageURI(image); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); image=savedInstanceState.getParcelable(IMAGE_URI_KEY); mImageView.setImageURI(image); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable(IMAGE_URI_KEY,image); } }
? 然后在AndroidManifest.xml中加入如下定義:
<activity android:name=".ImageWatcherActivity" android:label="查看圖片文件"> <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:mimeType="image/*"></data> </intent-filter> </activity>
?
這樣在單擊圖片文件的時候就可以選擇我們的這個Activity對txt文件處理并且顯示。
?
數(shù)據(jù)(data)檢測小結
對于data的匹配,如果說怎么怎么匹配,在什么情況下通過可能會比較難以理解,這里以一種簡單的方式來解說。
假定我們定義的Intent Filter 的data標簽為集合A,傳遞的Intent中包含的data為集合B,當B是A的子集時就通過了(Action和Category也得檢測通過)。如果B為空(不配置data),那么A也得為空(不配置data)才能通過 。更詳細(繁瑣)的介紹請參考doc
?
Intents and Intent Filters總結
Android提供了以Intent的方式調用Android設備的內置Google應用,比如打電話,調用Google瀏覽器打開網頁,搜索等。關于這方便的介紹可以參考Android開發(fā)文檔《Intents List: Invoking Google Applications on Android Devices》這一節(jié)的介紹,很詳細。docs/guide/appendix/g-app-intents.html。
Intent是一個很好的設計,它提供了一種在各個組建之間通信的方式,也為我們使用其他的應用的功能提供了可能,這樣如果我們想在自己的應用打開一個網頁,我們就不用特意遷入一個webview,我們直接調用Android內的瀏覽器打開即可。。
最后值得一提的是PackageManager這個類中為我們提供了一系列的query...()方法,可以讓我們根據(jù)我們定義的Intent查詢特定的匹配Intent Filter標記的所有組件。。有興趣的可以研究一下。。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
