扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
郑州app开发android内容提供者访问其他应用的数据库。整个思路是这样的,一共需要建立两个项目,第一个是内容提供者自己的数据,另外一个项目是访问刚才内容提供者的数据。因为两个项目中,布局都是button按钮,所以在这里就不展示了。
为尖扎等地区用户提供了全套网页设计制作服务,及尖扎网站建设行业解决方案。主营业务为成都网站设计、做网站、尖扎网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
第一个内容提供者
MainActivity.java
package cn.xhhkj.cyd;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.bundle;
import android.os.Ibinder;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase database;
private static final String TAG = "结果是";
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyOpenHelper helper = new MyOpenHelper(this);
database = helper.getReadableDatabase();
}
public void insert(View view) {
database.execSQL("insert into info(name,phone) values('王小明','13777777')");
database.execSQL("insert into info(name,phone) values('赵小明','13888888')");
database.execSQL("insert into info(name,phone) values('李大明','13666666')");
}
public void query(View view) {
Cursor cursor = database.rawQuery("select * from info", null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
Log.d(TAG,"name="+name+"phone="+phone);
}
cursor.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
database.close();
}
}
MyOpenHelper.java 这个类是提供数据的类。
package cn.xhhkj.cyd;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, "xhhkj.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
db.execSQL("alter table info add age integer");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Myprovider.java
package cn.xhhkj.cyd;
import android.content.Contentprovider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class Myprovider extends Contentprovider {
private MyOpenHelper openHelper;
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int QUERY_SUCESS = 0;
private static final int INSERT_MATCHED = 1;
private static final int UpDATE_MATCHED = 2;
private static final int DELETE_MATCHED = 3;
static
{
sURIMatcher.addURI("cn.xhhkj.provider", "query", QUERY_SUCESS);
sURIMatcher.addURI("cn.xhhkj.provider", "insert", INSERT_MATCHED);
sURIMatcher.addURI("cn.xhhkj.provider", "update", UpDATE_MATCHED);
sURIMatcher.addURI("cn.xhhkj.provider", "delete", DELETE_MATCHED);
//sURIMatcher.addURI("com.itheima.provider", "student", 5);
}
@Override
public boolean onCreate() {
openHelper = new MyOpenHelper(getContext());
return false;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
int result = sURIMatcher.match(uri);
if(result == QUERY_SUCESS){
SQLiteDatabase db = openHelper.getReadableDatabase();
Cursor cursor = db.query("info", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
}else{
throw new IllegalStateException("异常");
}
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
int result = sURIMatcher.match(uri);
if(result==INSERT_MATCHED){
SQLiteDatabase db = openHelper.getReadableDatabase();
long insert = db.insert("info", null, values);
getContext().getContentResolver().notifyChange(uri, null);
return Uri.parse(String.valueOf(insert));
}else{
return null;
}
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
int result = sURIMatcher.match(uri);
if(result==DELETE_MATCHED){
SQLiteDatabase db = openHelper.getReadableDatabase();
int delete = db.delete("info", selection, selectionArgs);
return delete;
}else{
return -1;
}
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
int result = sURIMatcher.match(uri);
if (result == UpDATE_MATCHED) {
SQLiteDatabase db = openHelper.getReadableDatabase();
int update = db.update("info", values, selection, selectionArgs);
db.close();
return update;
} else {
return -1;
}
}
}
这个类是真正的内容提供者。
另外一个项目,只是通过getContentResolver来查询内容提供者。
MainActivity.java
package cn.xhhkj.xhhkjtest;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private static final String TAG = "结果是";
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void query(View view) {
ContentResolver contentResolver = getContentResolver();
Uri uri =Uri.parse("content://cn.xhhkj.provider/query");
Cursor cursor = contentResolver.query(uri, null, null, null, null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
Log.d(TAG,"name="+name+"phone="+phone);
}
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流