101、使用ContentProvider在应用间传递数据
【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在应用间传递数据的更多相关文章
- 小菜学习Winform(五)窗体间传递数据
前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...
- contentprovider提供程序间共享数据的统一接口
contentprovider提供程序间共享数据的统一接口
- C#中使用SendMessage在进程间传递数据的实例
原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...
- StoryBoard学习(5):使用segue页面间传递数据
StoryBoard学习(5):使用segue页面间传递数据 函数: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sen ...
- 使用Bundle在Activity间传递数据
使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...
- WinForm 窗体间传递数据
前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...
- 在微信小程序页面间传递数据总结
在微信小程序页面间传递数据 原文链接:https://www.jianshu.com/p/dae1bac5fc75 在开发微信小程序过程之中,遇到这么一些需要在微信小程序页面之间进行数据的传递的情况, ...
- iPhone应用程序间传递数据
前一篇简单的介绍了iPhone应用程序间通信,主要是通过在被调用应用的Info.plist中加入URL方案,在应用中通过openUrl来实现程序的调用.而应用程序间的数据传递则可以更具url来实现,例 ...
- Android程序中Acticity间传递数据
在Android开发过程中,在不同的Acitivity之间传递数据的情况是非常常见的.我花费了一点时间来总结Acitivity之间的数据传递,记录下来. 1.简单传递键值对 这种传递方式非常简单,只需 ...
随机推荐
- SpringMVC常用注解,返回方式,路径匹配形式,验证
常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...
- python3图片裁剪+转换pdf+压缩
本地大量长图,要发送给别人,所以要对图片进行裁剪+转换pdf+压缩 import zipfile import os from concurrent.futures import ThreadPool ...
- python join与split函数的用法举例
python join 和 split方法: join用来连接字符串,split恰好相反,拆分字符串的. 来看有关join.split方法的例子 1,join用法的例子 复制代码 代码示例: > ...
- SSDB
一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis. 特性 替代 Redis 数据库, Redis 的 100 倍容量 LevelDB 网络支持, 使用 C/C++ 开发 Re ...
- 如何进行oracle capability i/o(压力测试数据库服务器i/o性能)
一 .oracle 有关 IO 信息的相关统计函数 Oracle i/o stack包含hbas,存储相关的交换机.存储物理磁盘.那么oracle建议在应用程序部署的时候,建议去验证i/o避免存在问题 ...
- BI案例:BI在连锁零售业应用(ZT)
第一部分:连锁零售企业上BI的必要性. 目前国内的连锁零售行业的发展趋势,呈现出产业规模化,经营业态多样化,管理精细化的特点.所谓管理精细化就是"精耕细作搞管理,领先一步订系统". ...
- HTML5 CANVAS 高级
加载图片 获取图像有三种方式: a : createImageData(),没有效率,一个像素一个像素的绘制: b : var img= document.getElementById("i ...
- Verilog杂谈
1. Testbech总是用reg去驱动DUT的input端口,因为需要在仿真期间设置和保持输入端的值(例如在initial中设置初值,在always中设置激励值): 2. 避免对局部reg在定义时赋 ...
- 如何把python最小化安装在客户机上面
因为想尝试在我们的桌面软件中加入python支持,所以想简化python的库,到时候直接放到客户机上面,并且放到我们的目录下,尽量免去不必要的东西,也不要影响机子,不过当写好的程序放到测试机子上后,老 ...
- 【freemaker】之自定义变量,特殊变量 globals ,循环对象取值
entity public class Employee { private Integer id; private String name; private Integer age; private ...