intent使用总结

 1. 指定act ion 和type
// SIM imp ort
        Intent imp ortIntent = new Intent(Intent.ACT ION_VIEW);
        imp ortIntent.setType("vnd.android.cursor.item/sim-contact");
        imp ortIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
        menu.add(0, 0, 0, R.string.imp ortFromSim)
                .setIcon(R.drawable.ic_menu_imp ort_contact)
                .setIntent(imp ortIntent);
                
2. 指定act ion, da ta和type
(1)隐式查找type
示例代码:
uri: content://simcontacts/simPeople/(id)
intent = new Intent("android.intent.act ion.SIMEDIT",uri);
            startActivity(intent);

程序会很据da ta中的uri去查找匹配的type(必须的)            
provider中的getType()            
case SIM_PEOPLE_ID:
            return "vnd.android.cursor.item/sim-contact";  

配置文件中的filter设定           
AndroidManifest.xml
    <intent-filter>
                <act ion android:name="android.intent.act ion.SIMEDIT" />
                <category android:name="android.intent.category.DEFAULT" />          
                <da ta android:mimeType="vnd.android.cursor.item/sim-contact" />
      </intent-filter>

也可以自己设定type,但只能使用 setDataAndType()     
      
3. 其他设定intent的属性方式
   Intent setComponent(ComponentName component)
   Intent setClassName(Context packageContext, String className)
   Intent setClassName(String packageName, String className)
   Intent setClass(Context packageContext, Class<?> cls)
   
   
注:
1. intent的实现代码
   public Intent setData(Uri da ta) {
        mData = da ta;
        mType = null;
        return this;
    }
    
    public Intent setType(String type) {
        mData = null;
        mType = type;
        return this;
    }
    
    public Intent setDataAndType(Uri da ta, String type) {
        mData = da ta;
        mType = type;
        return this;
    }
    
2. 如果指定da ta,而type为null,则会提示找不到activity
这时需要在putExtra()中指定uri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值