ContentProvider 中getType()方法的认识
2015-01-09 15:01
127 查看
我们都知道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
test_one.xml
第一个页面已经出来了
Test2.java
test_two.xml
第二页面也出来。
下面开始编写TestProvider
因为我们主要讲getType方法所以该类中的其他方法并不去实现。
TestProvider.java
所有的类和布局文件都已经写好,那么我们就开始写配置文件AndroidManifest.xml
在这里我主要粘贴了主要代码
看完代码:我们就来整理一下逻辑。
主要讲data的隐式匹配。
Test1,中点击按钮,意图发送,将意图中Uri 的一部分与配置文件中provider的authorities进行匹配。
匹配成功后,进入该provider中执行getType,返回Minetype,再在配置文件中匹配activity 中的data属性.匹配成功,则启动该activity
但是对于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方法的一点理解
- 是否需要覆盖ContentProvider的getType方法?
- 对ContentProvider中getType方法的一点理解
- ContentProvider中getType方法的作用
- 对ContentProvider中getType方法的一点理解
- 是否需要覆盖ContentProvider的getType方法?
- 对ContentProvider中getType方法的一点理解
- java方法中传值的认识
- Java入门--认识理解Java中native方法
- Java入门--认识理解Java中native方法
- GetTypeHashCode() 没有找到适合的方法来重写
- 认识基于中间件的软件开发方法
- 变革与突破—如何认识基于构件的软件开发方法
- 今天对于自定义事件与方法的区别有了一点感性的认识
- Java入门--认识理解Java中native方法
- 备忘:GetTypeHashCode() 没有找到适合的方法来重写
- GetTypeHashCode() 没有找到适合的方法来重写
- 用比较的方法认识PHP的面向对象
- 关于大家对U盘病毒认识误区的纠正及轻松对付U盘病毒的方法