ContentProvider 中getType()方法的认识
来源:互联网 发布:armageddon软件 编辑:程序博客网 时间:2024/05/18 00:59
我们都知道ContentProvider类是将数据库暴露出来,方便不同进程间数据库的访问。
但是对于getType方法,我们却很少知道它的作用,其实该方法在数据库的操作中起到的作用并不是多大,只有在activity中的隐式跳转中倒是起到了作用。
什么叫activity的隐式跳转呢?
打个比方,当我们在一群人中找出一个人来(这群人的名字都是唯一的),我们找他的方式有两种。
1:直接通过名字寻找。(类似activity中的显式跳转)
2,:通过这个人的外貌特征进行寻找。(类似activi中的隐式跳转)
知道了什么叫隐式跳转后,那我们就来讲讲activity的特征属性
1,action
2,data
3,category
前面的铺垫已经讲完,现在我们就通过实战进行讲解:
需要的文件有Test1.java,Test2.java,TestProvider.java ,AndroidManifest.xml中的配置,以及连个布局文件
Test1.java
package testprovidertype;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.example.testandroidui.R;public class Test1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_one); Button btn1 = (Button) findViewById(R.id.testBtn); btn1.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (v.getId() == R.id.testBtn) { Intent intent = new Intent(); intent.setAction("test"); intent.setData(Uri.parse("content://" + "testprovdertype.test" + "/test")); startActivity(intent); } } }); }}
test_one.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/testBtn" android:layout_width="match_parent" android:layout_height="160dp" android:text="test" /></LinearLayout>
第一个页面已经出来了
Test2.java
package testprovidertype;import android.app.Activity;import android.os.Bundle;import android.util.Log;import com.example.testandroidui.R;public class Test2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("test2", "test2"); setContentView(R.layout.test_two); }}
test_two.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/testTv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="我是test2" /></LinearLayout>
第二页面也出来。
下面开始编写TestProvider
因为我们主要讲getType方法所以该类中的其他方法并不去实现。
TestProvider.java
package testprovidertype;import android.content.ContentProvider;import android.content.ContentValues;import android.content.UriMatcher;import android.database.Cursor;import android.net.Uri;import android.util.Log;public class TestProvider extends ContentProvider { private static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { mUriMatcher.addURI("testprovdertype.test", "test", 1); mUriMatcher.addURI("testprovdertype.test", "test/#", 2); } @Override public boolean onCreate() { return true; } public String getType(Uri url) { int match = mUriMatcher.match(url); String result = ""; switch (match) { case 1: result = "vnd.android.cursor.dir/" + "test"; Log.v("1", "集合" + result); break; case 2: result = "vnd.android.cursor.item/" + "test"; break; default: break; } return result; } public Uri insert(Uri url, ContentValues initialValues) { return null; } public int delete(Uri url, String where, String[] whereArgs) { return 0; } public int update(Uri url, ContentValues values, String where, String[] whereArgs) { return 0; } public Cursor query(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sortOrder) { return null; }}
所有的类和布局文件都已经写好,那么我们就开始写配置文件AndroidManifest.xml
在这里我主要粘贴了主要代码
<activity android:name="testprovidertype.Test1" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="testprovidertype.Test2" android:label="@string/app_name" > <intent-filter> <action android:name="test"/> <data android:mimeType="vnd.android.cursor.dir/test" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <provider android:name="testprovidertype.TestProvider" android:authorities="testprovdertype.test" > </provider>
看完代码:我们就来整理一下逻辑。
主要讲data的隐式匹配。
Test1,中点击按钮,意图发送,将意图中Uri 的一部分与配置文件中provider的authorities进行匹配。
匹配成功后,进入该provider中执行getType,返回Minetype,再在配置文件中匹配activity 中的data属性.匹配成功,则启动该activity
- ContentProvider 中getType()方法的认识
- ContentProvider中getType方法的作用
- Android中关于ContentProvider类的getType()方法的理解
- 对ContentProvider中getType方法的一点理解
- 是否需要覆盖ContentProvider的getType方法?
- 是否需要覆盖ContentProvider的getType方法?
- 是否需要覆盖ContentProvider的getType方法?
- ContentProvider的getType
- android ContentProvider中getType(Uri uri)的理解
- android ContentProvider中getType(Uri uri)的理解
- ContentProvider中gettype() 和MIME类型的理解
- android ContentProvider中getType(Uri uri)的理解
- ContentProvider中gettype() 和MIME类型的理解
- ContentProvider中gettype() 和MIME类型的理解
- ContentProvider中gettype() 和MIME类型的理解
- ContentProvider 的 getType(Uri uri)
- 对ContentProvider中getType(Uri uri)和android.intent.category.DEFAULT的理解
- 对ContentProvider中getType(Uri uri)和android.intent.category.DEFAULT的理解
- Myeclipse中如何移出Capabilities??
- 英文站外链查询工具
- dumpsys cpuinfo
- Populating Next Right Pointers in Each Node
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- ContentProvider 中getType()方法的认识
- Jquery Uploadify之Java获取动态传参参数
- 实战Nginx与PHP(FastCGI)的安装、配置与优化
- Android Dialog无法填满屏幕宽度问题解决
- Oracle无法插入含有日期的数据
- Vagrant 快速入门
- 应用统计学-简单概念2
- VC工程的.gitignore模板
- C语言基础:C语言结构体(4) - 结构体数组