`
liangyouyu
  • 浏览: 9552 次
  • 性别: Icon_minigender_1
  • 来自: 长春
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论
阅读更多

android 之数据存储有4种方式,下面我们来分别介绍:

  1. sharedpreference:主要用来存储应用程式配置方面的信息;
  2. file主要以文件形式存储数据,以文件形式来读取数据;
  3. SQLite嵌入式的轻量级数据库;
  4. network,邮件保存或获取数据,网络上另一台服务器上保存获取数据;remark:Content Provider不同应用程式间数据存取的接口.

 

1>sharedpreferences:主要用来存储应用程式配置方面的信息;

存数据用法:

SharedPreferences msharedp=getPreferences(Content.MODE_PRIVATE); //这里用这个也可以Activity.MODE_PRIVATE

  • Editor edit=msharedp.edit();
  • edit.putString(“name”,”username”);
  • edit.putString(“pwd”,”123″);
  • edit.Commit();

获取数据用法:

SharedPreferences getsharedp=getPreferences(Activity.MODE_PRIVATE); //这里用这个也可以Content.MODE_PRIVATE

  • String name= getsharedp.getString(“name”,”");
  • String pwd= getsharedp.getString(“pwd”,”");
  • Toast.makeText(“name and pwd is:”+name+pwd);

 

2>file主要以文件形式存储数据,以文件形式来读取数据;

加载文件,获取数据:

  • Properties mpor=new Properties();
  • FileOpenInStream stream=this.openFileInput(“c.txt”);
  • mpor.load(stream);
  • mpor.get(“name”).toString();
  • mpor.get(“pwd”).toString();

保存数据到提定文件中:

  • Properties smpor=new Properties();
  • smpor.put(“name”,”username”);
  • smpor.put(“pwd”,”12345″);
  • FileOpenOutPutStream ostream=this.openFileOutput(“c.txt”,Content.MODE_WORLD_WRITETABLE);
  • smpor.store(ostream,”");

 

3>SQLite嵌入式的轻量级数据库;

1》 SQLiteDatabase mdb=this.openOrCreateDatabase(“databasename”,MODE_PRIVATE); //create database

2》//也可以得用SQLiteOpenHelper,构建一个mSQLiteOpenHelper类继承SQLiteOpenHelper,在函数super(content,”db_name”,”version_name”,null);建一个数据库,其它的用法一样:mdb=mSQLiteOpenHelper.getWriteDatabase();

  • mdb.execSQL(“create_table_str”); //create table
  • mdb.execSQL(“DROP TABLE”,”table_name”); //drop table
  • ContentValues cv=new ContentValues();
  • cv.put(“name”,”username”);
  • cv.put(“pwd”,”12345″);
  • mdb.insert(“table_name”,null,cv); //insert data to table
  • mdb.update(“table_name”,cv,”name=”+name,null); //update data to table
  • mdb.delete(“table_name”,”name=”+names,null) //delete data
  • Cursor cv= mdb.query(“table_name”,new String[]{“name”,”pwd”},null,null,null,null,null); //query data to listview
  • ListAdapter ad=SimpleCursorAdapter(this,android.R.layout.list_simple_list_item_2,cv,int []{android.R.id.text1,android.R.id.text2});
  • mlistView.setAdapter(ad);

 

4> network,邮件保存或获取数据,网络上另一台服务器上保存获取数据

将数据发到邮件保存:

  • Uri uri=Uri.parse(www_iyouyu_net@gail.com);
  • Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
  • intent.putExtra(android.content.Intent.EXTRA_SUBJECT,
  • intent.putExtra(android.content.Intnet.EXTRA_TEXT,”TITLE”);
  • stratActivity(intent);

从网络上获取数据:

  • URL url=new URL(http://www.iyouyu.net/android.txt);
  • URLConnection con=url.openConnection();
  • InputStream is=con.getInputStream();
  • BufferedInputStream bis=new BufferedInputStream(is);
  • ByteArray baf=new ByteArrayBuffer(100);
  • int count=0;
  • while( bis.read())
  • {
  • baf.append((byte)bis.read);
  • }
  • String getdata=new String(baf.toByteArray());

 

以上是ANDROID开发中4种常用的数据存取的方法,当要在不同应用程序中调用数据时用Content Provider,它提供了不同应用程式间数据存取的接口,到此,我们简单的回顾了ANDROID中数据存储常用的方法,在实际应用中要多加使用,总结。

<!-- .entry-content -->
本文链接: android 数据存储
转载自游鱼技术站
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics