ContentProvider:内容提供者。她主要完成这么一件事:共享数据。将一个应用的数据共享给其他应用
代码如下:
1、SQLiteProvider
package com.njupt.practice_contentprovider1.provider; import com.njupt.practice_contentprovider1.dao.DBOpenHelper; 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 SQliteProvider extends ContentProvider{ private UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); private DBOpenHelper helper; public static final int PERSON = 1; public static final int PERSON_ID = 2; @Override public boolean onCreate() { helper = new DBOpenHelper(getContext()); matcher.addURI("jd_love_ztmm","person",PERSON); matcher.addURI("jd_love_ztmm", "person/#", PERSON_ID); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = helper.getReadableDatabase(); switch(matcher.match(uri)){ case PERSON_ID: long id = ContentUris.parseId(uri); selection = selection == null ? "id = " + id : selection + " AND id = " + id; case PERSON: return db.query("person", null, selection, selectionArgs, null, null, sortOrder); default: throw new RuntimeException("Uri不能识别: " + uri); } } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = helper.getWritableDatabase(); switch(matcher.match(uri)){ case PERSON: long id = db.insert("person", "id", values); return ContentUris.withAppendedId(uri, id); default: throw new RuntimeException("Uri不能识别: " + uri); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = helper.getWritableDatabase(); switch(matcher.match(uri)){ case PERSON_ID: long id = ContentUris.parseId(uri); selection = selection == null ? "id = " + id : selection + " AND id = " + id; case PERSON: return db.delete("person", selection, selectionArgs); default: throw new RuntimeException("Uri不能识别: " + uri); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = helper.getWritableDatabase(); switch(matcher.match(uri)){ case PERSON_ID: long id = ContentUris.parseId(uri); selection = selection == null ? "id = " + id : selection + "AND id = " + id; case PERSON: return db.update("person", values, selection, selectionArgs); default: throw new RuntimeException("Uri不能识别: " + uri); } } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } }
2、ProviderTest
测试类
package com.njupt.pc1; import android.content.ContentProvider; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.test.AndroidTestCase; public class ProviderTest extends AndroidTestCase{ public void testQuery(){ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://jd_love_ztmm/person"); Cursor c = resolver.query(uri,null,null,null,null); while(c.moveToNext()){ Integer id = c.getInt(0); String name = c.getString(1); Integer balance = c.getInt(2); System.out.println(new Person(id,name,balance)); } } public void testDelete(){ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://jd_love_ztmm/person/1"); resolver.delete(uri, null, null); } public void testUpdate(){ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://jd_love_ztmm/person/2"); ContentValues values = new ContentValues(); values.put("name", "刘诗诗是java程序员的女神"); values.put("balance", "65000"); resolver.update(uri, values, null, null); } public void testInsert(){ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://jd_love_ztmm/person"); ContentValues values = new ContentValues(); values.put("name","刘诗诗是C++程序员的女神" ); values.put("balance", "70000"); resolver.insert(uri, values); } }
3、DBTest
package com.njupt.practice_contentprovider1.dao; import java.util.List; import java.util.Random; import com.njupt.practice_contentprovider1.domain.Person; import android.test.AndroidTestCase; public class DBTest extends AndroidTestCase { public void testCreateDataBase() { DBOpenHelper helper = new DBOpenHelper(getContext()); helper.getWritableDatabase(); } public void testInsert() { PersonDao dao = new PersonDao(getContext()); Person p = new Person("我爱章泽天", 55000); dao.insert(p); } public void testDelete() { PersonDao dao = new PersonDao(getContext()); dao.delete(104); } public void testUpdate() { PersonDao dao = new PersonDao(getContext()); Person p = new Person(105, "章泽天爱我", 46000); dao.update(p); } public void testQuery() { PersonDao dao = new PersonDao(getContext()); Person p = dao.query(105); System.out.println(p); } public void testInsertMany() { PersonDao dao = new PersonDao(getContext()); String strs[] = new String[]{"天天","章泽天","我爱天天","天天爱我"}; for(int i = 0 ; i < 100 ; ++i){ dao.insert(new Person(strs[new Random().nextInt(50) %4]+ i , new Random().nextInt(10000))); } } public void testQueryAll(){ PersonDao dao = new PersonDao(getContext()); List<Person> persons = dao.queryAll(); for(Person p : persons){ System.out.println(p); } } public void testQueryCount(){ PersonDao dao = new PersonDao(getContext()); System.out.println(dao.queryCount()); } public void testQueryPage(){ PersonDao dao = new PersonDao(getContext()); List<Person> persons = dao.queryPage(2, 20); for(Person p : persons){ System.out.println(p); } } public void testRemit(){ PersonDao dao = new PersonDao(getContext()); dao.remit(4, 3, 3000); } }
4、AndroidManifest.xml
在编写完ContentProvider的代码以后,别忘了在清单文件AndroidManifest.xml中进行注册
<provider android:name="com.njupt.practice_contentprovider1.provider.SQliteProvider" android:authorities="jd_love_ztmm" android:exported="true"/>
Android Device Chooser中显示Target unknown解决方法
手机插在电脑上准备调试程序来着,通过eclipse运行时,弹出的Android Device Chooser中显示设备名是?????,Target未知,无法继续运行。
可以通过以下步骤解决(Ubuntu):
1. 查看设备的vendor ID:
ubuntu@ubuntu:~$ lsusb
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 002: ID 24ae:2000
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 004: ID 0bb4:0cfe High Tech Computer Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
很容易根据名字认出来手机是哪个,我的手机对应的ID是0bb4。
2.添加手机设备到usb列表中
ubuntu@ubuntu:~$ sudo vim /etc/udev/rules.d/51-android.rules
输入:
SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666", GROUP="plugdev"
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev"
3. 保存,在命令行中运行:
ubuntu@ubuntu:~$ sudo chmod a+r /etc/udev/rules.d/51-android.rules
ubuntu@ubuntu:~$ sudo service udev reload
然后再重启一下eclipse,右键运行,问题解决了。
由于要使用SimpleAdapter,所以要新建一个布局文件tab_info.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:id="@+id/id"
android:textSize="30px"
android:layout_height="wrap_content"
android:layout_width="50px"/>
<TextView
android:id="@+id/name"
android:textSize="30px"
android:layout_height="wrap_content"
android:layout_width="130px"/>
<TextView
android:id="@+id/birthday"
android:textSize="30px"
android:layout_height="wrap_content"
android:layout_width="180px"/>
</TableRow>
</TableLayout>
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
在MySQLiteDemo.java中
package com.li.sqlite;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MySQLiteDemo extends Activity {
private ListView listView ;
private SimpleAdapter simpleAdapter = null ;
private LinearLayout loadLayout = null ; // 读取的脚标的视图
private TextView loadInfo = null ; // 进行信息提示
private List<Map<String,Object>> all = null ;
private LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT); // 表示新组件的布局参数
private SQLiteOpenHelper helper = null ;
private LinearLayout mylayout = null ;
private int currentPage = 1 ;
private int lineSize = 15 ;
private int allRecorders = 0 ;
private int pageSize = 1 ; // 默认在一共只有1页
private int lastItem = 0 ; // 保存最后一项
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mylayout = (LinearLayout) super.findViewById(R.id.mylayout) ;
this.loadLayout = new LinearLayout(this) ; // 定义脚标的线性布局管理器
this.loadInfo = new TextView(this) ; // 文本组件
this.loadInfo.setText("数据加载中ing...") ; // 定义提示文字
this.loadInfo.setGravity(Gravity.CENTER) ; // 文字居中显示
this.loadInfo.setTextSize(30.0f) ; // 文字大小
this.loadLayout.addView(this.loadInfo,this.layoutParams) ; // 增加组件
this.loadLayout.setGravity(Gravity.CENTER) ;
this.showAllData() ; // 数据显示
this.pageSize = (this.allRecorders + this.lineSize - 1)
/ this.lineSize; // 计算总页数
System.out.println("pageSize = " + this.pageSize) ;
System.out.println("allRecorders = " + this.allRecorders);
}
private void showAllData(){ // 读取全部的数据
MySQLiteDemo.this.helper = new MyDatabaseHelper(MySQLiteDemo.this);
MytabCursor cur = new MytabCursor( // 实例化查询
MySQLiteDemo.this.helper.getReadableDatabase()) ; // 取得SQLiteDatabase对象
this.allRecorders = cur.getCount() ; // 取得全部记录数
this.listView = new ListView(MySQLiteDemo.this) ;
MySQLiteDemo.this.all = cur.find(MySQLiteDemo.this.currentPage,MySQLiteDemo.this.lineSize) ;
this.listView.addFooterView(this.loadLayout) ; // 增加读取数据的布局文件
this.simpleAdapter = new SimpleAdapter(MySQLiteDemo.this, // 上下文对象
MySQLiteDemo.this.all, // 所有要操作的数据
R.layout.tab_info, // 布局管理器
new String[] { "id", "name", "birthday" }, // map中的key
new int[] { R.id.id, R.id.name, R.id.birthday }) ;
this.listView.setAdapter(this.simpleAdapter); // 布局管理中的id
this.listView.setOnScrollListener(new OnScrollListenerImpl()) ;
this.mylayout.addView(listView) ;
}
private class OnScrollListenerImpl implements OnScrollListener{
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
MySQLiteDemo.this.lastItem = firstVisibleItem + visibleItemCount - 1 ; // 统计是否到最后
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (MySQLiteDemo.this.lastItem == MySQLiteDemo.this.simpleAdapter
.getCount()// 已经是最底部
&& MySQLiteDemo.this.currentPage < MySQLiteDemo.this.pageSize // 还有数据没读取完
&& scrollState == OnScrollListener.SCROLL_STATE_IDLE ) { // 不再滑动
MySQLiteDemo.this.currentPage ++ ;
MySQLiteDemo.this.listView.setSelection(MySQLiteDemo.this.lastItem) ; // 设置显示位置
MySQLiteDemo.this.appendData() ; // 增加数据
}
}
}
private void appendData(){ // 增加数据
MytabCursor cur = new MytabCursor( // 实例化查询
MySQLiteDemo.this.helper.getReadableDatabase()) ; // 取得SQLiteDatabase对象
List<Map<String, Object>> newData = cur.find(this.currentPage,
this.lineSize);
this.all.addAll(newData) ; // 集合改变
this.simpleAdapter.notifyDataSetChanged() ; // 通知记录改变
}
}
在MytabCursor.java中
package com.li.sqlite;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MytabCursor {
private static final String TABLENAME = "mytab" ;
private SQLiteDatabase db = null ;
public MytabCursor(SQLiteDatabase db) {
this.db = db ;
}
public int getCount() { // 返回记录数
int count = 0;
String sql = "SELECT COUNT(id) FROM " + TABLENAME; // 查询SQL
Cursor result = this.db.rawQuery(sql, null);
for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) { // 采用循环的方式检索数据
count = result.getInt(0);
}
return count;
}
public List<Map<String,Object>> find(int currentPage,int lineSize){
List<Map<String,Object>> all = new ArrayList<Map<String,Object>>() ;
String sql = "SELECT id,name,birthday FROM " + TABLENAME
+ " LIMIT ?,?";
String args[] = new String[] {
String.valueOf((currentPage - 1) * lineSize),
String.valueOf(lineSize) }; // 是设置参数
Cursor result = this.db.rawQuery(sql, args); // 执行查询语句
for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) { // 采用循环的方式检索数据
Map<String,Object> map = new HashMap<String,Object>() ;
map.put("id", result.getInt(0)) ;
map.put("name", result.getString(1)) ;
map.put("birthday",result.getString(2)) ;
all.add(map) ;
}
this.db.close() ;
return all ;
}
}
在MyDatabaseHelper.java中
package com.li.sqlite;
//数据库的辅助操作类
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASENAME = "liyewen.db" ;
private static final int DATABASERVERSION = 1 ; // 设置数据库的版本
private static final String TABLENAME = "mytab" ;
public MyDatabaseHelper(Context context) { // 用户最关心的也肯定只是Context
super(context, DATABASENAME, null, DATABASERVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { // 创建数据表
String sql = "CREATE TABLE " + TABLENAME + "("
+ "id INTEGER PRIMARY KEY ," // 在SQLite中设置为Integer、PRIMARY KEY则ID自动增长
+ "name VARCHAR(50) NOT NULL ,"
+ "birthday DATE NOT NULL" + ")";
db.execSQL(sql) ; // 执行SQL
System.out.println("****************** 创建:onCreate()。");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABLENAME ;
db.execSQL(sql) ;
System.out.println("****************** 更新:onUpgrade()。");
this.onCreate(db) ;
}
}