Android四大元件之 內容提供者

2021-07-01 18:13:22 字數 3142 閱讀 6746

內容提供者說簡單了就是像外提供乙個uri,然後別人去匹配這個uri就可以使用你定義好的一些方法

下面寫個例項來介紹一下contentprodvider

寫乙個內容提供者的類

personcontentprovider 類

package com.example.sqlitedemo.providers;

import com.example.sqlitedemo.db.personsqliteopenhelper;

import android.content.contentprovider;

import android.content.contenturis;

import android.content.contentvalues;

import android.content.urimatcher;

import android.database.cursor;

import android.database.sqlite.sqlitedatabase;

import android.net.uri;

public class personcontentprovider extends contentprovider

@override

public boolean oncreate()

@override

public int delete(uri uri, string selection, string selectionargs)

break;

default:

throw new illegalargumentexception("uri不匹配: " + uri);

}return 0;

}@override

public string gettype(uri uri)

return null;

}@override

public uri insert(uri uri, contentvalues values)

break;

default:

throw new illegalargumentexception("uri不匹配: " + uri);

}return null;

}@override

public cursor query(uri uri, string projection, string selection,

string selectionargs, string sortorder)

break;

case person_query_item_code:

if(db.isopen()), null, null, sortorder);

return cursor;

}break;

default:

throw new illegalargumentexception("uri不匹配: " + uri);

}return null;

}@override

public int update(uri uri, contentvalues values, string selection,

string selectionargs)

break;

default:

throw new illegalargumentexception("uri不匹配: " + uri);

}return 0;

}}

需要注意一下provider是有許可權的

寫完這個之後呢我們將這個應用部署到手機上然後在寫乙個應用來測試我們寫的內容提供者

在新建的應用中寫乙個測試類test

package com.example.othercontentprodvider;

import android.content.contentresolver;

import android.content.contenturis;

import android.content.contentvalues;

import android.database.cursor;

import android.net.uri;

import android.test.androidtestcase;

import android.util.log;

public class mytest extends androidtestcase

public void testdetete();

int count = contentresolver.delete(uri, where, selectionargs);

log.i(tag, "刪除行:"+count);

}public void testupdate();

contentresolver.update(uri, values, where, selectionargs);

}public void testqueryall();

cursor cursor=contentresolver.query(uri, projection, null, null, "cr_id desc");

string name;

int id;

int age;

if(cursor!=null &&cursor.getcount()>0)}}

public void testqueryitem(), null, null, "cr_id desc");

if(cursor != null && cursor.movetofirst()) }}

在這個應用的androidmanifest.xml中也必須加入我們之前定義好的乙個許可權

測試junit  test必須要在androidmanifest中新增配置檔案

在manifest節點下新增

接著我們就可以執行test中的方法了

android 四大元件

1.activity 2.service 3.contentprovider 應用中的資料,對外進行共享,其它應用可以通過內容提供者,可以訪問到你應用中的資料,對資料進行增刪改查 1 對不同的資料格式,統一了檔案格式和資料訪問api 2 內容提供者要繼承contentprovider類 3 在清單檔...

Android 四大元件

activity intent receiver service content provider 並不是每乙個android應用程式都需要這四種構造塊,這不是必須的。當我們明確了我們的應用需要哪些構造塊後,我們就需要在androidmanifest.xml中登記這些構造塊的清單。這個配置檔案用於定...

android四大元件

android 四大元件 1.contentprovider contentprovider是什麼 android中的乙個應用元件 作為乙個引用元件的表現是生命週期方法 android中內容提供者 一般是內部儲存中的資料 contentprovider 物件的應用場合 例如 3.contentpri...