当前位置:操作系统 > 安卓/Android >>

Android 使用ContentProvider 实现多个activity灵活显示,实现Intent隐式传值.

1. 在Manifest.xml添加 Provider关联
 
<application ...>
 <provider android:name="TestProvider" android:authorities="com.example.testandroid"/>
 <activityandroid:name="com.example.testandriod.ThirdActivity">
            <intent-filter>
                <actionandroid:name="android.intent.action.VIEW"/>
 
                <dataandroid:mimeType="com.cn.***/test"/>
 
                <categoryandroid:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
<activity>
...
</activity>
</application>
 
其中的<data>标签是标识你的activity,必须采用A/B的形式。
 
 
2.建立对应的Provider类
 
public classTestProvider extendsContentProvider {
 
@Override
public int delete(Uri arg0,String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
 
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
Log.d("debug", arg0+"");
return"com.cn.***/test";//找xml文件中对应的activity.
}
 
@Override
public Uri insert(Uri arg0,ContentValues arg1) {
// TODO Auto-generated method stub
returnnull;
}
 
@Override
publicboolean onCreate() {
// TODO Auto-generated method stub
returnfalse;
}
 
@Override
public Cursorquery(Uri arg0,String[] arg1, String arg2,String[] arg3,
String arg4) {
// TODO Auto-generated method stub
returnnull;
}
 
@Override
public int update(Uri arg0,ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
 
}
其中getType的返回值必须是和你的Manifest.xml文件中配置的<data mimeType="xxxx">xxx要完全一致。
3.在activity中实现如下代码:
 
Intent intent =new Intent();
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("content://com.example.testandroid/one")); // 这段常量会传递给上面的Provider的getType函数;
intent.setAction("android.intent.action.VIEW");
startActivity(intent);
即可启动任意activity,并传值.
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,