本文整理匯總了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();
}
}