embedded linux ,android

2010年11月16日 星期二

資料儲存與Content Provider

在使用手機時,user需要儲存一些資訊,如果這些資訊是比較固定的,此時若能儲存起來,就可免去每次輸入這些資訊的手續,在android有三種資料儲存方式:
Shared Preferences(xml形式)、檔案儲存與資料庫儲存。
Android 並沒有一個共同的儲存空間讓所有應用程式存取,因此若要讓其它的應用程式使用我的資料,就必須使用Content Provider,用來分享資料用的。


DataStorageDemo.java
package wandroide.blogspot.com;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class DataStorageDemo extends Activity implements OnClickListener
{
    private TextView mTextView         = null;
    private EditText mEditText         = null;         
    private Button   mSharedPrefButton = null;
    private Button   mFileButton       = null;
    private Button   mSqliteButton     = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
           
        mTextView         = (TextView)findViewById(R.id.ltextview);
    mEditText         = (EditText)findViewById(R.id.ledittext);          
 mSharedPrefButton = (Button)findViewById(R.id.lsharedprefbutton);
 mFileButton       = (Button)findViewById(R.id.lfilebutton);
 mSqliteButton     = (Button)findViewById(R.id.lsqlitebutton);
      
 mSharedPrefButton.setOnClickListener(this);
 mFileButton.setOnClickListener(this);
 mSqliteButton.setOnClickListener(this);
    }
    
    public void onClick(View v) 
    {
 if(v == mSharedPrefButton)
 {
     //write
     SharedPreferences pref = getSharedPreferences("PREF_DATA", MODE_WORLD_READABLE);
       SharedPreferences.Editor preEdt = pref.edit();
       preEdt.putString("PREF_EDITTEXT", mEditText.getText().toString());
       preEdt.commit();
       //read
       String pref_edittext = pref.getString("PREF_EDITTEXT", "hello shared preference");
            mTextView.setText(pref_edittext);
       mEditText.setText("");
 }
 else if(v == mFileButton)
 {
     
 }
 else if(v == mSqliteButton)
 {
          
 }    
    }
}
ContentProviderDemo.java
package wnadroide.blogspot.com;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class ContentProviderDemo extends Activity implements OnClickListener
{    
    private TextView mTextView              = null;
    private Button   mGetPrefButton         = null;
    private Button   mContentProviderButton = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTextView      = (TextView)findViewById(R.id.ltextview);
        mGetPrefButton = (Button)findViewById(R.id.lgetprefbutton);
        mContentProviderButton = (Button)findViewById(R.id.lcontentproviderbutton);
       
        mGetPrefButton.setOnClickListener(this);
        mContentProviderButton.setOnClickListener(this);
    }
  
    public void onClick(View v) 
    {
 if(v == mGetPrefButton)
        {
     Context otherApp = null;
     try {
      otherApp = createPackageContext("wandroide.blogspot.com", 0);
     } catch (Exception e) {
      Log.d("wandroide", e.toString());
      finish();
     }
     SharedPreferences pref = otherApp.getSharedPreferences("PREF_DATA",MODE_WORLD_READABLE);
     String pref_edittext = pref.getString("PREF_EDITTEXT", "get不到");
     mTextView.setText(pref_edittext);
 }
 else if(v == mContentProviderButton)
 {
     Cursor c = getContentResolver().query(Uri.parse("content://wade.dbprovider"),
                null, null, null, null);
         
     if(c.getCount() != 0) 
     {
         c.moveToFirst();
      String name = c.getString(c.getColumnIndex("name"));
      if ( name == null ) return;
      mTextView.setText(name);
     }
     
   }
    }
}
DBProvider.java
package wnadroide.blogspot.com;

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 DBProvider extends ContentProvider {
    Context mCtx;
    SQLiteDatabase db;

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
 return -1;
    }

    @Override
    public String getType(Uri uri) {
 return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
 return null;
    }

    @Override
    public boolean onCreate() {
 mCtx = getContext();
 DoDB dbHelper;
 dbHelper = new DoDB(mCtx);
 db = dbHelper.getWritableDatabase();
 return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
   String[] selectionArgs, String sortOrder) {
 Cursor c = db.rawQuery("select * from nametable;", null);
 return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
   String[] selectionArgs) {
 return 0;
    }
}
DoDB.java
package wnadroide.blogspot.com;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DoDB extends SQLiteOpenHelper {
    private final static String DATABASE_NAME = "namedb";
    private final static int DATABASE_VERSION = 1;
    private final static String TABLE_NAME = "nametable";
    public final static String FIELD_NAME = "name";
  
    public DoDB(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
 String sql = "CREATE TABLE " + TABLE_NAME + " (" + FIELD_NAME + ")";
 db.execSQL(sql);
 ContentValues cv = new ContentValues();
 cv.put(FIELD_NAME, "aaaaa");
 db.insert(TABLE_NAME, null, cv);
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    }
 
    public Cursor select()
    {
 return null;
    }
}
AndroidManifest.xml
<provider android:name=".DBProvider" 
          android:authorities="wade.dbprovider">
</provider>

沒有留言:

張貼留言