【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. pyzmq missing when running ipython notebook

    Q: I can run iPython, but when I try to initiate a notebook I get the following error: ~ ipython not ...

  2. unity, sceneview 中拾取球体gizmos

    http://answers.unity3d.com/questions/745560/handle-for-clickable-scene-objects.html http://www.jians ...

  3. noip2011普及组——数字反转

    数字反转 时间限制:1s 内存限制:128MB[问题描述]给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零 ...

  4. Mozilla Brick:一个Web组件Polyfill库

    Web组件是一个W3C规范,它旨在使Web开发人员能够定义具有非常丰富的视觉效果和高可交互性且易于组合的小组件.Brick库提供了新的自定义HTML标签,从而抽象了用户常用接口模式.在浏览器本身支持类 ...

  5. MySQL数据库update更新子查询

    比如: ? 1 2 3 4 UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test. ...

  6. jquery的$.extend和$.fn.extend作用及区别

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); (1)类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax ...

  7. 简单了解ddos攻击

    1.一种为流量攻击,主要是针对网络带宽的攻击,即大量攻击包导致网络带宽被阻塞,合法网络包被虚假的攻击包淹没而无法到达主机: 2.另一种为资源耗尽攻击,主要是针对服务器主机的攻击,即通过大量攻击包导致主 ...

  8. 初试体验java多线程

    现在个人电脑以及服务器都是多核cpu,如何提高多核cpu的利用率,就要用到多线程技术了. public class TestThread1 { ; static class PThread exten ...

  9. SQL开发利器SQL Prompt

    SQL开发利器SQL Prompt 5.1完全破解+使用教程 - keepfool - 博客园 http://www.cnblogs.com/keepfool/archive/2012/05/27/2 ...

  10. [navicat] Navicat for Oracle Cannot load OCI DLL

    1. 本地安装的是64位的Oracle,但由于Navicat仅支持32位的,因此我们还需下载一个32位的客户端. 2.