您的位置:首页 > 移动开发 > Android开发

Android四大组件之ContentProvider实现多应用之间传递数据实例

2016-11-10 09:26 836 查看
两个项目:



内容提供者 :



内容接收者:



一.对于ContentProvider的简介我之前写好了博客,这里不再重复,刚入门的朋友还是建议先去看看简介这样才能实现这篇文章的一目十行,简介地址:http://blog.csdn.net/qq_33750826/article/details/52602402

二.过程:

1.因为ContentProvider主要用来多应用之间传递数据,所以这里我们首先创建两个Android Application Project,

一个用于提供数据的,我将Project取名为内容提供者

一个用于接收数据的,我将Project取名为内容接收者

2.内容提供者界面是看不到任何效果的,因为它只用于提供数据,只有在内容接收者界面才会看到效果。

3.无论在什么时候,必须先有内容提供者存在,不然内容接收者就是错误的,比如我们这里如果我们写好代码后,先运行的是内容接收者,这里内容接收者是会直接崩掉的,因为内容接收者接受的内容根本不存在,也就是我们这里的内容提供者还没运行,数据库根本就未创建,何来的接收呢,所以我们这里先运行内容提供者项目,接着运行内容接收者。

三:代码

根据上面我们先将内容提供者写好:

因为内容提供这其实就是简介数据库,关于数据库还不明白的小伙伴,可以参考这篇博客:http://blog.csdn.net/qq_33750826/article/details/53036695

所以首先写一个类继承SQLOpenHelper

1.MySqliteOpenHelper:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqliteOpenHelper extends SQLiteOpenHelper {

public MySqliteOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table tb(id Integer primary key autoincrement, name varchar(10))");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}


2.MyContentProvider:

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 MyContentProvider extends ContentProvider {

private MySqliteOpenHelper helper;
private SQLiteDatabase db;

private String path = "com.teacher.contentprovider";
// content://com.teacher.contentprovider/tb
private Uri uri = Uri.parse("content://" + path + "/tb/#");

private Uri uri2 = Uri.parse("content://" + path + "/tbs");

// 表示当contentrsolver传递的uri没有匹配时,返回值为-1;
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

@Override
public boolean onCreate() {
helper = new MySqliteOpenHelper(getContext(), "tb.db", null, 1);
db = helper.getReadableDatabase();

matcher.addURI(path, "/tb/#", 1);
matcher.addURI(path, "/tbs", 2);
return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// 状态码
int i = matcher.match(uri);
switch (i) {
case 1:

break;
case 2:

break;
default:
break;
}
Cursor cursor = db.query("tb", projection, selection, selectionArgs,
null, null, sortOrder);
return cursor;
}

@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
long id = db.insert("tb", null, values);
Uri uri2 = ContentUris.withAppendedId(uri, id);
return uri2;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
int index = db.delete("tb", selection, selectionArgs);
return index;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int index = db.update("tb", values, selection, selectionArgs);
return index;
}

}


3.activity_mian:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>


4.MainActivity:(这里的MainActivity其实是无关紧要的)

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}


不要着急运行,先在manifest中声明ConetentProvider:

<!-- android:authorities添加指定的内容提供者 -->
<!-- android:exported:用于设置是否允许外部应用程序访问contentProvider -->
<provider
android:name="com.example.cp.MyContentProvider"
android:authorities="com.teacher.contentprovider"
android:exported="true" >
</provider>


运行出现以上图二效果

接着写内容接受者:

acitivity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="插入"
android:onClick="btn_insert" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新"
android:onClick="btn_update" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="btn_delete" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"
android:onClick="btn_select" />

</LinearLayout>


MainActivity:

import android.net.Uri;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

private String path = "com.teacher.contentprovider";
// ccontent://com.teacher.contentprovider/tb
private Uri uri = Uri.parse("content://" + path + "/tb");
private ContentResolver resolver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resolver = getContentResolver();

}

public void btn_insert(View v) {
ContentValues values = new ContentValues();
values.put("name", "强仔");
Uri uri2 = resolver.insert(uri, values);

long id = ContentUris.parseId(uri2);
if (id > 0) {
Toast.makeText(this, "添加成功", 1000).show();
} else {
Toast.makeText(this, "添加失败", 1000).show();
}
}

public void btn_update(View v) {
ContentValues values = new ContentValues();
values.put("name", "强孙孙");
// update tb set name="强孙孙" where id=1
int index = resolver.update(uri, values, "id=?", new String[] { "1" });
if (index > 0) {
Toast.makeText(this, "修改成功", 1000).show();
} else {
Toast.makeText(this, "修改失败", 1000).show();
}
}

public void btn_select(View v) {
// content:// contentprovider的域名/表名
// select name from tb where id=1
Cursor cursor=resolver.query(uri, new String[] { "name" }, null,
null, null);
while (cursor.moveToNext()) {
String name=cursor.getString(cursor.getColumnIndex("name"));
Toast.makeText(this, name, 1000).show();
}
}

public void btn_delete(View v) {
// delete from tb where id=2
int index = resolver.delete(uri, "id=?", new String[] { "2" });
if (index > 0) {
Toast.makeText(this, "删除成功", 1000).show();
} else {
Toast.makeText(this, "删除失败", 1000).show();
}
}
}


运行出现以上图3效果

以上讲解的就是ContentProvider的实例,不会的小伙伴赶快敲起来吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: