package com.example.copymsm; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlSerializer; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.database.Cursor; import android.util.Xml; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { private List<Mess> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList<Mess>(); Cursor result = getContentResolver().query(Uri.parse("content://sms/"), null, null, null, null); while (result.moveToNext()) { Mess m = new Mess(); m.setBody(result.getString(result.getColumnIndex("body"))); m.setDate(result.getString(result.getColumnIndex("date"))); m.setType(result.getString(result.getColumnIndex("type"))); m.setPhone(result.getString(result.getColumnIndex("address"))); list.add(m); } findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Toast.makeText(MainActivity.this, "点击了", 0).show(); File file = new File(Environment .getExternalStorageDirectory() + File.separator + "sms.xml"); try { FileOutputStream fos = new FileOutputStream(file); XmlSerializer xs = Xml.newSerializer(); xs.setOutput(fos, "utf-8"); xs.startDocument("utf-8", true); xs.startTag(null, "message"); for (int i = 0; i < list.size(); i++) { xs.startTag(null, "msm"); xs.startTag(null, "body"); xs.text(list.get(i).getBody()); xs.endTag(null, "body"); xs.startTag(null, "type"); xs.text(list.get(i).getType()); xs.endTag(null, "type"); xs.startTag(null, "date"); xs.text(list.get(i).getDate()); xs.endTag(null, "date"); xs.startTag(null, "phone"); xs.text(list.get(i).getPhone()); xs.endTag(null, "phone"); xs.endTag(null, "msm"); } xs.endTag(null, "message"); xs.endDocument(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); } }
package com.example.copymsm; public class Mess { private String body; private String type; private String date; private String phone; public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public Mess(){} @Override public String toString() { return "Mess [body=" + body + ", type=" + type + ", date=" + date + ", phone=" + phone + "]"; } }
数据库备份短信
package com.example.copymsm1; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class Myhelp extends SQLiteOpenHelper { public Myhelp(Context context) { super(context, "data.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "create table msg(" + "_id integer primary key," + "body varchar(255)," + "type varchar(10)," + "address varchar(50)," + "date varchar(50)" + ")"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
package com.example.copymsm1; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MainActivity extends Activity { private SQLiteOpenHelper help; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); help = new Myhelp(this); db = help.getWritableDatabase(); Cursor result = getContentResolver().query(Uri.parse("content://sms/"), null, null, null, null); while (result.moveToNext()) { String body = result.getString(result.getColumnIndex("body")); String date = result.getString(result.getColumnIndex("date")); String type = result.getString(result.getColumnIndex("type")); String address = result.getString(result.getColumnIndex("address")); ContentValues cv = new ContentValues(); cv.put("body", body); cv.put("type", type); cv.put("date", date); cv.put("address", address); db.insert("msg", null, cv); } } }