`

Android下intent的setdata、settype和setdataandtype函数

 
阅读更多

Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。利用Intent所实现的软件复用的粒度是Activity/Service,比函数复用更高一些,另外耦合也更为松散。

 

1 settype

使用该函数表示要查找文件的mime类型(如*/*),这个和组件在manifest里定义的相对应,但在源代码里:

  1. public  Intent setData(Uri data) {  
  2.         mData = data;  
  3.         mType = null ;  
  4.         return   this ;  
  5.     }  
  1. public  Intent setData(Uri data) {  
  2.         mData = data;  
  3.         mType = null ;  
  4.         return   this ;  
  5.     }  
 

会将type设为null。

2 setdata

该函数的参数是uri,所以要将数据通过该函数传递时,记得要把数据转化为uri,如Uri.fromFile(new File("/mnt/sdcard/"))。

该函数源代码

  1. public  Intent setType(String type) {  
  2.         mData = null ;  
  3.         mType = type;  
  4.         return   this ;  
  5.     }  
  1. public  Intent setType(String type) {  
  2.         mData = null ;  
  3.         mType = type;  
  4.         return   this ;  
  5.     }  
 

3 所以要同时设置data和type的话只能用函数setdataandtype了

  1. public  Intent setDataAndType(Uri data, String type) {  
  2.         mData = data;  
  3.         mType = type;  
  4.         return   this ;  
  5.     }  
  1. public  Intent setDataAndType(Uri data, String type) {  
  2.         mData = data;  
  3.         mType = type;  
  4.         return   this ;  
  5.     } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics