1.短信数据库分析分析SMS表
2.导出短信
1
2
3
|
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_SMS" />
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_number = (EditText) findViewById(R.id.phone_num);
btn_export = (Button) findViewById(R.id.btn_export);
btn_export.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new AsyncTask<Integer, Integer, String>() {
private ProgressDialog dialog;
// UI声明
protected void onPreExecute() {
dialog = ProgressDialog.show(MainActivity.this, "",
"正在到处短信,请稍后....");
super.onPreExecute();
}
// 后台执行 ,
protected String doInBackground(Integer... params) {
String input_number = edt_number.getText().toString();
String result = "";
if (input_number.equals("")) {// 判断号码是否为空,如果为空的话
List<String> listnumber = getAllNumber();// 获取全部的号码
for (String number : listnumber) {// 遍历号码
// 调用写入内存卡的方法,传入参数
if (file_write("AllNumber.txt", get_sms(number))) {
result += "号码" + number
+ "的所有的短信AllNumber.txt中\n";
} else {
result += "号码" + number + "未成功\n";
}
}
} else {
if (file_write(input_number + ".txt",
get_sms(input_number))) {
result = "号码" + input_number + "的短信为"
+ input_number + ".txt中";
} else {
result = "号码" + input_number + "未成功";
}
}
return result;
}
// 搜索完毕后,结果处理
protected void onPostExecute(String result) {
dialog.dismiss();// 进度条消失
new AlertDialog.Builder(MainActivity.this)
.setMessage(result).create().show();// 显示完成的的提示框
super.onPostExecute(result);
}
}.execute(0);
}
});
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
private boolean file_write(String filename, String content) {
// 判断SD是否可用
if (!android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return false;
}
String filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/" + filename;// 保存文件名
File file = new File(filepath);// 创建文件类 对象为file
try {
if (!file.exists()) { // 判断文件是否存在,不存在则新建
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file, true);// 获取文件输出流
fos.write(content.getBytes());// 写入文件
fos.close();// 关闭文件输出流
} catch (Exception e) {
Log.i(TAG, "file write w " + e.toString());
return false;
}
return true;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
private String get_sms(String number) {
StringBuilder sms_Builder = new StringBuilder();
ContentResolver cr = getContentResolver();
// 查询短信数据库
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
Uri uri = Uri.parse(SMS_URI_ALL);
Cursor cur = cr.query(uri, projection,
"address like '%" + number + "'", null, "date desc");// 查询获取指定号码的短信
if (cur.moveToFirst()) {// 判断是查询结果
String name;
String phoneNumber;
String smsbody;
String date;
String type;
do {// 遍历查询结果
name = cur.getString(2);
phoneNumber = cur.getString(1);
smsbody = cur.getString(3);
if (smsbody == null)
smsbody = "";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(Long.parseLong(cur.getString(4)));
date = dateFormat.format(d);
int typeId = cur.getInt(5);
if (typeId == 1) {
type = "接受";
} else if (typeId == 2) {
type = "发送";
} else {
type = "草稿";
}
sms_Builder.append(name + ",");
sms_Builder.append(phoneNumber + ",");
sms_Builder.append(smsbody + ",");
sms_Builder.append(date + ",");
sms_Builder.append(type);
sms_Builder.append("\n");
} while (cur.moveToNext());
} else {
sms_Builder.append("no result!");// 无查询结果
}
cur.close();// 关闭数据库
return sms_Builder.toString();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
private List<String> getAllNumber() {
List<String> list = new ArrayList<String>();
String address = "";
ContentResolver cr = getContentResolver();
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
Uri uri = Uri.parse(SMS_URI_ALL);
Cursor cur = null;
try {
cur = cr.query(uri, projection, null, null, "address desc");
if (cur.moveToFirst()) {
do {
String tmpString = cur.getString(1);
if (!address.equals(tmpString)) {
address = tmpString;
list.add(tmpString);
}
} while (cur.moveToNext());
}
} catch (Exception e) {
Log.i(TAG, "getallnumber w " + e.toString());
}
cur.close();
return list;
}
|