本文整理汇总了Java中org.xmlpull.v1.XmlSerializer.flush方法的典型用法代码示例。如果您正苦于以下问题:Java XmlSerializer.flush方法的具体用法?Java XmlSerializer.flush怎么用?Java XmlSerializer.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.xmlpull.v1.XmlSerializer
的用法示例。
在下文中一共展示了XmlSerializer.flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generatePublicXml
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
private void generatePublicXml(ResPackage pkg, Directory out,
XmlSerializer serial) throws AndrolibException {
try {
OutputStream outStream = out.getFileOutput("values/public.xml");
serial.setOutput(outStream, null);
serial.startDocument(null, null);
serial.startTag(null, "resources");
for (ResResSpec spec : pkg.listResSpecs()) {
serial.startTag(null, "public");
serial.attribute(null, "type", spec.getType().getName());
serial.attribute(null, "name", spec.getName());
serial.attribute(null, "id", String.format("0x%08x", spec.getId().id));
serial.endTag(null, "public");
}
serial.endTag(null, "resources");
serial.endDocument();
serial.flush();
outStream.close();
} catch (IOException | DirectoryException ex) {
throw new AndrolibException("Could not generate public.xml file", ex);
}
}
示例2: testSerializeValue
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
@Test
public void testSerializeValue() throws IOException, BlockLoadingException {
Block block = mBlockFactory.obtainBlockFrom(
new BlockTemplate().ofType("frankenblock").withId("364"));
block.setPosition(37, 13);
Input input = block.getInputByName("value_input");
Block inputBlock = mBlockFactory.obtainBlockFrom(
new BlockTemplate().ofType("output_foo").withId("VALUE_GOOD"));
input.getConnection().connect(inputBlock.getOutputConnection());
ByteArrayOutputStream os = new ByteArrayOutputStream();
XmlSerializer serializer = getXmlSerializer(os);
block.serialize(serializer, /* root block */ true, IOOptions.WRITE_ALL_DATA);
serializer.flush();
String expected = BlockTestStrings.frankenBlockStart("block", "364")
+ BlockTestStrings.VALUE_GOOD
+ BlockTestStrings.FRANKENBLOCK_DEFAULT_VALUES
+ BlockTestStrings.BLOCK_END;
assertThat(os.toString()).isEqualTo(expected);
}
示例3: save
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
/**
* Method that saves list of document into XML file
* @throws IOException
*/
@Override
public void save() throws IOException {
Log.d("DocManager","ActualSave");
File toWrite = new File(context.getFilesDir(), URI);
FileOutputStream fos = new FileOutputStream(toWrite);
XmlSerializer xmlSerializer = Xml.newSerializer();
xmlSerializer.setOutput(fos, "UTF-8");
xmlSerializer.startDocument(null, true);
xmlSerializer.startTag(null, "documentList");
for(int i=0; i < documents.size(); ++i){
xmlSerializer.startTag(null, "document");
addTag(xmlSerializer, "uri", documents.get(i).getUri());
addTag(xmlSerializer, "title", documents.get(i).getTitle());
addTag(xmlSerializer, "author", documents.get(i).getAuthor());
addTag(xmlSerializer, "importDate", String.valueOf(documents.get(i).getImportDate().getTime()));
addTag(xmlSerializer, "lastReadPosition", String.valueOf(documents.get(i).getLastReadPosition()));
Log.d("CicloFir",String.valueOf(documents.get(i).getLastReadPosition()));
//addTag(xmlSerializer, "length", String.valueOf(documents.get(i).getLength()));
// Insert list of bookmarks
for(int j=0; j < documents.get(i).getBookmark().size(); ++j){
addTag(xmlSerializer, "bmName", documents.get(i).getBookmark().get(j).getName());
addTag(xmlSerializer, "bmPosition", String.valueOf(documents.get(i).getBookmark().get(j).getIndex()));
}
xmlSerializer.endTag(null, "document");
}
xmlSerializer.endTag(null, "documentList");
xmlSerializer.endDocument();
xmlSerializer.flush();
fos.close();
}
示例4: save
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
/***
* Public Method that save current application setting to XML file
* @throws IOException
*/
public void save() throws IOException {
//SettingManager systemSetts = new SettingManager();
TextSetting textSetts = (TextSetting) /*systemSetts.*/settings.get(0);
ArrayMap<String, String> textSpecs = textSetts.getSpecs();
ThemeSetting themeSetts = (ThemeSetting) /*systemSetts.*/settings.get(1);
ArrayMap<String, String> themeSpecs = themeSetts.getSpecs();
FattsSetting fattsSetts = (FattsSetting) /*systemSetts.*/settings.get(2);
ArrayMap<String, String> fattsSpecs = fattsSetts.getSpecs();
File toWrite = new File(context.getFilesDir(), context.getResources().getString(R.string.uri_setting));
FileOutputStream fos = new FileOutputStream(toWrite);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, true);
//serializer.setFeature(FEATURE_PROCESS_NAMESPACES, false);
serializer.startTag(null, "settings");
serializer.startTag(null, "textsetting");
genTag(serializer, "size", textSpecs.get("size"));
genTag(serializer, "spacing", textSpecs.get("spacing"));
genTag(serializer, "lineSpacing", textSpecs.get("lineSpacing"));
serializer.endTag(null, "textsetting");
serializer.startTag(null, "themesetting");
genTag(serializer, "font", themeSpecs.get("originalFont"));
genTag(serializer, "palette", themeSpecs.get("palette"));
genTag(serializer, "highlight", themeSpecs.get("highlight"));
serializer.endTag(null, "themesetting");
serializer.startTag(null, "fattssetting");
genTag(serializer, "id", fattsSpecs.get("id"));
genTag(serializer, "language", fattsSpecs.get("language"));
genTag(serializer, "gender", fattsSpecs.get("gender"));
genTag(serializer, "speed", fattsSpecs.get("speed"));
serializer.endTag(null, "fattssetting");
serializer.endDocument();
serializer.flush();
fos.close();
}
示例5: writeXml
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
public static void writeXml(Writer output, XmlContentWriter writer) throws IOException {
try {
XmlSerializer serializer = PARSER_FACTORY.newSerializer();
serializer.setOutput(output);
writer.write(serializer);
serializer.flush();
} catch (XmlPullParserException e) {
throw new IllegalStateException("Unable to construct XmlSerilaizer");
}
}
示例6: testSerializeShadowBlock
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
@Test
public void testSerializeShadowBlock() throws IOException, BlockLoadingException {
Block block = mBlockFactory.obtainBlockFrom(
new BlockTemplate().shadow().ofType("empty_block")
.withId(BlockTestStrings.EMPTY_BLOCK_ID)
.atPosition(37, 13));
ByteArrayOutputStream os = new ByteArrayOutputStream();
XmlSerializer serializer = getXmlSerializer(os);
block.serialize(serializer, /* root block */ true, IOOptions.WRITE_ALL_DATA);
serializer.flush();
assertThat(os.toString()).isEqualTo(BlockTestStrings.EMPTY_SHADOW_WITH_POSITION);
}
示例7: testSerializeStatement
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
@Test
public void testSerializeStatement() throws IOException, BlockLoadingException {
Block block = mBlockFactory.obtainBlockFrom(
new BlockTemplate().ofType("frankenblock").withId("364"));
block.setPosition(37, 13);
Input input = block.getInputByName("NAME");
Block inputBlock = mBlockFactory.obtainBlockFrom(
new BlockTemplate().ofType("frankenblock").withId("3"));
input.getConnection().connect(inputBlock.getPreviousConnection());
ByteArrayOutputStream os = new ByteArrayOutputStream();
XmlSerializer serializer = getXmlSerializer(os);
block.serialize(serializer, /* root block */ true, IOOptions.WRITE_ALL_DATA);
serializer.flush();
String expected = BlockTestStrings.frankenBlockStart("block", "364")
+ BlockTestStrings.FRANKENBLOCK_DEFAULT_VALUES_START
+ "<statement name=\"NAME\">"
+ "<block type=\"frankenblock\" id=\"3\">"
+ BlockTestStrings.FRANKENBLOCK_DEFAULT_VALUES
+ BlockTestStrings.BLOCK_END
+ "</statement>"
+ BlockTestStrings.FRANKENBLOCK_DEFAULT_VALUES_END
+ BlockTestStrings.BLOCK_END;
assertThat(os.toString()).isEqualTo(expected);
}
示例8: writeObject
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
try {
XmlSerializer writer = XmlPullParserFactory.newInstance().newSerializer();
writer.setOutput(out, "utf-8");
writer.startDocument("utf-8", true);
dumpXulDataNode(this, writer);
writer.endDocument();
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: getAllSMS
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
/**
* 获取手机短信并保存到xml中
* <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_SMS"/>}</p>
*/
public static void getAllSMS() {
// 1.获取短信
// 1.1获取内容解析者
ContentResolver resolver = Utils.getApp().getContentResolver();
// 1.2获取内容提供者地址 sms,sms表的地址:null 不写
// 1.3获取查询路径
Uri uri = Uri.parse("content://sms");
// 1.4.查询操作
// projection : 查询的字段
// selection : 查询的条件
// selectionArgs : 查询条件的参数
// sortOrder : 排序
Cursor cursor = resolver.query(uri, new String[]{"address", "date", "type", "body"}, null, null, null);
// 设置最大进度
int count = cursor.getCount();//获取短信的个数
// 2.备份短信
// 2.1获取xml序列器
XmlSerializer xmlSerializer = Xml.newSerializer();
try {
// 2.2设置xml文件保存的路径
// os : 保存的位置
// encoding : 编码格式
xmlSerializer.setOutput(new FileOutputStream(new File("/mnt/sdcard/backupsms.xml")), "utf-8");
// 2.3设置头信息
// standalone : 是否独立保存
xmlSerializer.startDocument("utf-8", true);
// 2.4设置根标签
xmlSerializer.startTag(null, "smss");
// 1.5.解析cursor
while (cursor.moveToNext()) {
SystemClock.sleep(1000);
// 2.5设置短信的标签
xmlSerializer.startTag(null, "sms");
// 2.6设置文本内容的标签
xmlSerializer.startTag(null, "address");
String address = cursor.getString(0);
// 2.7设置文本内容
xmlSerializer.text(address);
xmlSerializer.endTag(null, "address");
xmlSerializer.startTag(null, "date");
String date = cursor.getString(1);
xmlSerializer.text(date);
xmlSerializer.endTag(null, "date");
xmlSerializer.startTag(null, "type");
String type = cursor.getString(2);
xmlSerializer.text(type);
xmlSerializer.endTag(null, "type");
xmlSerializer.startTag(null, "body");
String body = cursor.getString(3);
xmlSerializer.text(body);
xmlSerializer.endTag(null, "body");
xmlSerializer.endTag(null, "sms");
System.out.println("address:" + address + " date:" + date + " type:" + type + " body:" + body);
}
xmlSerializer.endTag(null, "smss");
xmlSerializer.endDocument();
// 2.8将数据刷新到文件中
xmlSerializer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: getAllSMS
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
/**
* 获取手机短信并保存到xml中
* <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_SMS"/>}</p>
*/
public static void getAllSMS() {
// 1.获取短信
// 1.1获取内容解析者
ContentResolver resolver = Utils.getContext().getContentResolver();
// 1.2获取内容提供者地址 sms,sms表的地址:null 不写
// 1.3获取查询路径
Uri uri = Uri.parse("content://sms");
// 1.4.查询操作
// projection : 查询的字段
// selection : 查询的条件
// selectionArgs : 查询条件的参数
// sortOrder : 排序
Cursor cursor = resolver.query(uri, new String[]{"address", "date", "type", "body"}, null, null, null);
// 设置最大进度
int count = cursor.getCount();//获取短信的个数
// 2.备份短信
// 2.1获取xml序列器
XmlSerializer xmlSerializer = Xml.newSerializer();
try {
// 2.2设置xml文件保存的路径
// os : 保存的位置
// encoding : 编码格式
xmlSerializer.setOutput(new FileOutputStream(new File("/mnt/sdcard/backupsms.xml")), "utf-8");
// 2.3设置头信息
// standalone : 是否独立保存
xmlSerializer.startDocument("utf-8", true);
// 2.4设置根标签
xmlSerializer.startTag(null, "smss");
// 1.5.解析cursor
while (cursor.moveToNext()) {
SystemClock.sleep(1000);
// 2.5设置短信的标签
xmlSerializer.startTag(null, "sms");
// 2.6设置文本内容的标签
xmlSerializer.startTag(null, "address");
String address = cursor.getString(0);
// 2.7设置文本内容
xmlSerializer.text(address);
xmlSerializer.endTag(null, "address");
xmlSerializer.startTag(null, "date");
String date = cursor.getString(1);
xmlSerializer.text(date);
xmlSerializer.endTag(null, "date");
xmlSerializer.startTag(null, "type");
String type = cursor.getString(2);
xmlSerializer.text(type);
xmlSerializer.endTag(null, "type");
xmlSerializer.startTag(null, "body");
String body = cursor.getString(3);
xmlSerializer.text(body);
xmlSerializer.endTag(null, "body");
xmlSerializer.endTag(null, "sms");
System.out.println("address:" + address + " date:" + date + " type:" + type + " body:" + body);
}
xmlSerializer.endTag(null, "smss");
xmlSerializer.endDocument();
// 2.8将数据刷新到文件中
xmlSerializer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: xmlExport
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
private void xmlExport(OutputStream out) throws IOException {
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(out, "UTF-8");
serializer.startDocument(null, true);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "netguard");
serializer.startTag(null, "application");
xmlExport(PreferenceManager.getDefaultSharedPreferences(this), serializer);
serializer.endTag(null, "application");
serializer.startTag(null, "wifi");
xmlExport(getSharedPreferences("wifi", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "wifi");
serializer.startTag(null, "mobile");
xmlExport(getSharedPreferences("other", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "mobile");
serializer.startTag(null, "screen_wifi");
xmlExport(getSharedPreferences("screen_wifi", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "screen_wifi");
serializer.startTag(null, "screen_other");
xmlExport(getSharedPreferences("screen_other", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "screen_other");
serializer.startTag(null, "roaming");
xmlExport(getSharedPreferences("roaming", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "roaming");
serializer.startTag(null, "lockdown");
xmlExport(getSharedPreferences("lockdown", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "lockdown");
serializer.startTag(null, "apply");
xmlExport(getSharedPreferences("apply", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "apply");
serializer.startTag(null, "notify");
xmlExport(getSharedPreferences("notify", Context.MODE_PRIVATE), serializer);
serializer.endTag(null, "notify");
serializer.startTag(null, "filter");
filterExport(serializer);
serializer.endTag(null, "filter");
serializer.startTag(null, "forward");
forwardExport(serializer);
serializer.endTag(null, "forward");
serializer.endTag(null, "netguard");
serializer.endDocument();
serializer.flush();
}
示例12: getAllSMS
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
/**
* 获取手机短信并保存到 xml 中
* <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_SMS"/>}</p>
*/
public static void getAllSMS(Context context) {
// 1.获取短信
// 1.1 获取内容解析者
ContentResolver resolver = context.getContentResolver();
// 1.2 获取内容提供者地址 sms,sms 表的地址:null 不写
// 1.3 获取查询路径
Uri uri = Uri.parse("content://sms");
// 1.4.查询操作
// projection : 查询的字段
// selection : 查询的条件
// selectionArgs : 查询条件的参数
// sortOrder : 排序
Cursor cursor = resolver.query(uri, new String[]{"address", "date", "type", "body"}, null, null, null);
// 设置最大进度
int count = cursor.getCount();//获取短信的个数
// 2.备份短信
// 2.1 获取 xml 序列器
XmlSerializer xmlSerializer = Xml.newSerializer();
try {
// 2.2 设置 xml 文件保存的路径
// os : 保存的位置
// encoding : 编码格式
xmlSerializer.setOutput(new FileOutputStream(new File("/mnt/sdcard/backupsms.xml")), "utf-8");
// 2.3 设置头信息
// standalone : 是否独立保存
xmlSerializer.startDocument("utf-8", true);
// 2.4 设置根标签
xmlSerializer.startTag(null, "smss");
// 1.5.解析 cursor
while (cursor.moveToNext()) {
SystemClock.sleep(1000);
// 2.5 设置短信的标签
xmlSerializer.startTag(null, "sms");
// 2.6 设置文本内容的标签
xmlSerializer.startTag(null, "address");
String address = cursor.getString(0);
// 2.7 设置文本内容
xmlSerializer.text(address);
xmlSerializer.endTag(null, "address");
xmlSerializer.startTag(null, "date");
String date = cursor.getString(1);
xmlSerializer.text(date);
xmlSerializer.endTag(null, "date");
xmlSerializer.startTag(null, "type");
String type = cursor.getString(2);
xmlSerializer.text(type);
xmlSerializer.endTag(null, "type");
xmlSerializer.startTag(null, "body");
String body = cursor.getString(3);
xmlSerializer.text(body);
xmlSerializer.endTag(null, "body");
xmlSerializer.endTag(null, "sms");
System.out.println("address:" + address + " date:" + date + " type:" + type + " body:" + body);
}
xmlSerializer.endTag(null, "smss");
xmlSerializer.endDocument();
// 2.8 将数据刷新到文件中
xmlSerializer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: captureElement
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
/**
* Serializes the current element and all child nodes as a String.
* @param parser The parser to pull from.
* @return The composed element string.
* @throws XmlPullParserException
* @throws IOException
*/
public static String captureElement(XmlPullParser parser)
throws XmlPullParserException, IOException
{
int event = parser.getEventType();
if (event != XmlPullParser.START_TAG) {
throw new XmlPullParserException("Expected call to begin at START_TAG");
}
int depth = 0;
StringWriter sw = new StringWriter();
XmlSerializer serializer = PARSER_FACTORY.newSerializer();
serializer.setOutput(sw);
String namespace, prefix;
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
++depth;
namespace = parser.getNamespace();
prefix = parser.getPrefix();
if (namespace != null && prefix != null) {
serializer.setPrefix(prefix, namespace);
}
serializer.startTag(namespace, parser.getName());
int attrCount = parser.getAttributeCount();
for (int i = 0; i < attrCount; ++i) {
namespace = parser.getAttributeNamespace(i);
prefix = parser.getAttributePrefix(i);
if (namespace != null && prefix != null) {
serializer.setPrefix(prefix, namespace);
}
serializer.attribute(namespace, parser.getAttributeName(i),
parser.getAttributeValue(i));
}
break;
case XmlPullParser.TEXT:
case XmlPullParser.IGNORABLE_WHITESPACE:
serializer.text(parser.getText());
break;
case XmlPullParser.CDSECT:
serializer.cdsect(parser.getText());
break;
case XmlPullParser.END_TAG:
namespace = parser.getNamespace();
if (namespace != null) {
serializer.setPrefix(parser.getPrefix(), namespace);
}
serializer.endTag(namespace, parser.getName());
--depth;
break;
}
if (depth <= 0) {
serializer.flush();
return sw.toString();
}
event = parser.next();
}
throw new IOException("Unexpected end of document.");
}
示例14: testSerializeShadowValue
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
@Test
public void testSerializeShadowValue() throws IOException, BlockLoadingException {
Block block = mBlockFactory.obtainBlockFrom(
new BlockTemplate().ofType("frankenblock").withId("364").atPosition(37, 13));
Input input = block.getInputByName("value_input");
Block inputBlock = mBlockFactory.obtainBlockFrom(
new BlockTemplate().ofType("output_foo").withId("VALUE_REAL"));
input.getConnection().connect(inputBlock.getOutputConnection());
inputBlock = mBlockFactory.obtainBlockFrom(
new BlockTemplate().shadow().ofType("output_foo").withId("VALUE_SHADOW"));
input.getConnection().setShadowConnection(inputBlock.getOutputConnection());
ByteArrayOutputStream os = new ByteArrayOutputStream();
XmlSerializer serializer = getXmlSerializer(os);
block.serialize(serializer, /* root block */ true, IOOptions.WRITE_ALL_DATA);
serializer.flush();
String expected = BlockTestStrings.frankenBlockStart("block", "364")
+ BlockTestStrings.VALUE_SHADOW_GOOD
+ BlockTestStrings.FRANKENBLOCK_DEFAULT_VALUES
+ BlockTestStrings.BLOCK_END;
assertThat(os.toString()).isEqualTo(expected);
block = mBlockFactory.obtainBlockFrom(
new BlockTemplate().ofType("frankenblock").withId("777"));
block.setPosition(37, 13);
input = block.getInputByName("value_input");
inputBlock = mBlockFactory.obtainBlockFrom(
new BlockTemplate().shadow().ofType("simple_input_output").withId("SHADOW1"));
input.getConnection().connect(inputBlock.getOutputConnection());
input = inputBlock.getOnlyValueInput();
inputBlock = mBlockFactory.obtainBlockFrom(
new BlockTemplate().shadow().ofType("simple_input_output").withId("SHADOW2"));
input.getConnection().connect(inputBlock.getOutputConnection());
os.reset();
block.serialize(serializer, /* root block */ true, IOOptions.WRITE_ALL_DATA);
serializer.flush();
expected = BlockTestStrings.frankenBlockStart("block", "777")
+ BlockTestStrings.VALUE_NESTED_SHADOW
+ BlockTestStrings.FRANKENBLOCK_DEFAULT_VALUES
+ BlockTestStrings.BLOCK_END;
assertThat(os.toString()).isEqualTo(expected);
}
示例15: getAllSMS
import org.xmlpull.v1.XmlSerializer; //导入方法依赖的package包/类
/**
* 获取手机短信并保存到xml中
* <p>需添加权限<uses-permission android:name="android.permission.READ_SMS"/>
* <p>需添加权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
*/
public static void getAllSMS(Context context) {
//1.获取短信
//1.1获取内容解析者
ContentResolver resolver = context.getContentResolver();
//1.2获取内容提供者地址 sms,sms表的地址:null 不写
//1.3获取查询路径
Uri uri = Uri.parse("content://sms");
//1.4.查询操作
//projection : 查询的字段
//selection : 查询的条件
//selectionArgs : 查询条件的参数
//sortOrder : 排序
Cursor cursor = resolver.query(uri, new String[]{"address", "date", "type", "body"}, null, null, null);
//设置最大进度
int count = cursor.getCount();//获取短信的个数
//2.备份短信
//2.1获取xml序列器
XmlSerializer xmlSerializer = Xml.newSerializer();
try {
//2.2设置xml文件保存的路径
//os : 保存的位置
//encoding : 编码格式
xmlSerializer.setOutput(new FileOutputStream(new File("/mnt/sdcard/backupsms.xml")), "utf-8");
//2.3设置头信息
//standalone : 是否独立保存
xmlSerializer.startDocument("utf-8", true);
//2.4设置根标签
xmlSerializer.startTag(null, "smss");
//1.5.解析cursor
while (cursor.moveToNext()) {
SystemClock.sleep(1000);
//2.5设置短信的标签
xmlSerializer.startTag(null, "sms");
//2.6设置文本内容的标签
xmlSerializer.startTag(null, "address");
String address = cursor.getString(0);
//2.7设置文本内容
xmlSerializer.text(address);
xmlSerializer.endTag(null, "address");
xmlSerializer.startTag(null, "date");
String date = cursor.getString(1);
xmlSerializer.text(date);
xmlSerializer.endTag(null, "date");
xmlSerializer.startTag(null, "type");
String type = cursor.getString(2);
xmlSerializer.text(type);
xmlSerializer.endTag(null, "type");
xmlSerializer.startTag(null, "body");
String body = cursor.getString(3);
xmlSerializer.text(body);
xmlSerializer.endTag(null, "body");
xmlSerializer.endTag(null, "sms");
System.out.println("address:" + address + " date:" + date + " type:" + type + " body:" + body);
}
xmlSerializer.endTag(null, "smss");
xmlSerializer.endDocument();
//2.8将数据刷新到文件中
xmlSerializer.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}