【ContentWriter】

package com.jikexueyuan.contentwriter;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri; public class Myprovider extends ContentProvider {
public static final Uri URI = Uri.parse("content://com.jikexueyuan.cp");
SQLiteDatabase database; @Override
public int delete(Uri arg0, String arg1, String[] arg2) {
return 0;
} @Override
public String getType(Uri arg0) {
return null;
} @Override
public Uri insert(Uri arg0, ContentValues arg1) {
database.insert("tab", "_id", arg1);
// database.close();
return null;
} @Override
public boolean onCreate() {
database = getContext().openOrCreateDatabase("mycp.db3", Context.MODE_PRIVATE, null);
database.execSQL("create table tab(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)"); return true;
} @Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
Cursor cursor = database.query("tab", null, null, null, null, null, null);
return cursor;
} @Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
return 0;
} }
public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
write();
}
});
} public void write() {
ContentValues values;
values = new ContentValues();
values.put("name", "Java");
getContentResolver().insert(Myprovider.URI, values); values = new ContentValues();
values.put("name", "Swift");
getContentResolver().insert(Myprovider.URI, values); values = new ContentValues();
values.put("name", "Python");
getContentResolver().insert(Myprovider.URI, values); values = new ContentValues();
values.put("name", "C#"
);
getContentResolver().insert(Myprovider.URI, values);
}

}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jikexueyuan.contentwriter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jikexueyuan.contentwriter.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="Myprovider" android:exported="true"
android:authorities="com.jikexueyuan.cp" />

</application> </manifest>

【ContentReader】

public class MainActivity extends Activity {
Uri URI = Uri.parse("content://com.jikexueyuan.cp"); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Cursor cursor = getContentResolver().query(URI, null, null, null, null);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String value = cursor.getString(cursor.getColumnIndex("name"));
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
cursor.moveToNext();
}
}
}

101、使用ContentProvider在应用间传递数据的更多相关文章

  1. 小菜学习Winform(五)窗体间传递数据

    前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...

  2. contentprovider提供程序间共享数据的统一接口

    contentprovider提供程序间共享数据的统一接口

  3. C#中使用SendMessage在进程间传递数据的实例

    原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...

  4. StoryBoard学习(5):使用segue页面间传递数据

    StoryBoard学习(5):使用segue页面间传递数据 函数: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sen ...

  5. 使用Bundle在Activity间传递数据

    使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...

  6. WinForm 窗体间传递数据

    前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...

  7. 在微信小程序页面间传递数据总结

    在微信小程序页面间传递数据 原文链接:https://www.jianshu.com/p/dae1bac5fc75 在开发微信小程序过程之中,遇到这么一些需要在微信小程序页面之间进行数据的传递的情况, ...

  8. iPhone应用程序间传递数据

    前一篇简单的介绍了iPhone应用程序间通信,主要是通过在被调用应用的Info.plist中加入URL方案,在应用中通过openUrl来实现程序的调用.而应用程序间的数据传递则可以更具url来实现,例 ...

  9. Android程序中Acticity间传递数据

    在Android开发过程中,在不同的Acitivity之间传递数据的情况是非常常见的.我花费了一点时间来总结Acitivity之间的数据传递,记录下来. 1.简单传递键值对 这种传递方式非常简单,只需 ...

随机推荐

  1. UNDERSTANDING POSTGRESQL.CONF: CHECKPOINT_SEGMENTS, CHECKPOINT_TIMEOUT, CHECKPOINT_WARNING

    While there are some docs on it, I decided to write about it, in perhaps more accessible language – ...

  2. WCF Restful JQuery 跨域解决方法

    <?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET ...

  3. share point 读取 List数据

    SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite oSite = new SPSite(siteUrl)) { forea ...

  4. 非root模式下安装mysql php小记

    假设你的home目录为/home/work mysql-server 安装 1. 下载mysql.tar.gz wget http://dev.mysql.com/get/Downloads/MySQ ...

  5. jQuery插件 -- 表单验证插件jquery.validate.js, jquery.metadata.js

    原文地址:http://blog.csdn.net/zzq58157383/article/details/7718352   最常使用JavaScript的场合就是表单的验证,而jQuery作为一个 ...

  6. DataList与Repeater嵌套绑定

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="home.aspx.cs&quo ...

  7. eclipse注解——作者,创建时间,版本

    总结: /** * @author liangyadong * @date ${date} ${time} * @version 1.0 */

  8. java单例模式和双例模式

    今天朋友找我给做道题,双例模式,我是没听说过,都说是单例模式和多例模式, 也不知道双例模式什么时候用,就简单写了一个案例,不知道对不对,个人感觉蛮对的,双例就是单例+单例,废话不说了!!!! /* * ...

  9. hibernate.properties官方属性用例(可用于hibernate.cfg.xml属性参考)

    ######################### Query Language ######################### ## define query language constant ...

  10. js 图片切换效果

    效果如下: 代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&quo ...