(6)Content Providers

An Overview of Android Content Providers

An Android content provider provides you with access to sharable data structures commonly called databases.The basic procedure is as follows:

1. Get permission to open the database.

2. Query the data.

3. Access the data.

Data can be in Android internal memory or in external memory such as an SD card, or even on an external server that is remote to the Android device itself.

SQLite:open source DBMS.

The "Lite" or "Light" part delineates that this is a "lightweight" version of the DBMS.

SQLite is a part of Android.

Contacts Database Contact Providers

the Contacts database interfaces that work with Android 1.x phones are different than the ones that work on the Android 2.x phones.

CONTENT_URI:  points to the data location.

Defining Security Permissions

We need to get permissions to use certain areas of the OS so that Android can implement a robust level of security within its OS infrastructure.

 (1)Contact DataBase


AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

\layout\contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
      <Button 
        android:tag="query"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click to query contacts db"/>
     <Button 
        android:tag="add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click to add contacts db"/>
      <Button 
        android:tag="update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click to update contacts db"/>
       <Button 
        android:tag="delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click to delete contacts db"/>
</LinearLayout>
public class Contacts extends Activity implements OnClickListener {
	private View rootView;
	private Uri queryUri;
	private Uri addUri;
	private Uri updateUri;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.contacts, null);
		this.setContentView(rootView);
		
		Button btnQuery=(Button)rootView.findViewWithTag("query");
		btnQuery.setOnClickListener(this);
		
		Button btnAdd=(Button)rootView.findViewWithTag("add");
		btnAdd.setOnClickListener(this);
		
		Button btnUpdate=(Button)rootView.findViewWithTag("update");
		btnUpdate.setOnClickListener(this);
		
		Button btnDelete=(Button)rootView.findViewWithTag("delete");
		btnDelete.setOnClickListener(this);
		
		
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String tag=(String)v.getTag();
		if("query".equals(tag)){
			this.queryContacts();
		}else if("add".equals(tag)){
			this.addContacts("test1", "123123");
		}else if("update".equals(tag)){
			this.updateContacts("111111");
		}else if("delete".equals(tag)){
			
		}
	}
	
	private void queryContacts(){
		String[] cols=new String[]{People.NAME,People.NUMBER};
		queryUri=People.CONTENT_URI;
		Cursor cursor=managedQuery(queryUri,cols,null,null,null);
		if(cursor.moveToFirst()){
			String name=null;
			String number=null;
			do{
				name=cursor.getString(cursor.getColumnIndex(People.NAME));
				number=cursor.getString(cursor.getColumnIndex(People.NUMBER));
				Toast.makeText(this, name+" "+number, Toast.LENGTH_SHORT).show();
			}while(cursor.moveToNext());
		}
	}
	
	private void addContacts(String newName,String newNumber){
		ContentValues myContent=new ContentValues();
		myContent.put(People.NAME, newName);
		addUri=this.getContentResolver().insert(People.CONTENT_URI, myContent);
		Uri contentUri=Uri.withAppendedPath(addUri, People.Phones.CONTENT_DIRECTORY);
		myContent.clear();
		myContent.put(People.Phones.TYPE, People.TYPE_MOBILE);
		myContent.put(People.NUMBER, newNumber);
		updateUri=this.getContentResolver().insert(contentUri, myContent);
		Toast.makeText(this, "New Contact:"+newName+" "+newNumber, Toast.LENGTH_LONG).show();
	}
	
	private void updateContacts(String newNumber){
		if(updateUri==null){
			Toast.makeText(this, "You need to create a new contact to update!",Toast.LENGTH_LONG).show();
		}else{
			ContentValues number=new ContentValues();
			number.put(People.Phones.TYPE, People.TYPE_MOBILE);
			number.put(People.NUMBER, newNumber);
			this.getContentResolver().update(updateUri, number, null, null);
			  Toast.makeText(this, "Updated phone number to: " + newNumber,Toast.LENGTH_SHORT).show();
		}
	}
	
	private void deleteContacts(){
		if(updateUri==null){
			 Toast.makeText(this, "You need to create a new contact to delete!", Toast.LENGTH_LONG).show();
		}else{
			this.getContentResolver().delete(addUri, null, null);
			Toast.makeText(this, "Deleted contact at: " + addUri.toString(),Toast.LENGTH_SHORT).show();
			addUri=null;
			updateUri=null;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值