我們都知道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