在android读取短信并进行相应的管理时,可以进行短信箱删除,但是这个删除先得到TheardId跟这个threadID进行删除短信

在getContentResolver进行查询。代码如下

用到权限:
<uses-permission android:name="android.permission.READ_SMS"/>

<uses-permission android:name="android.permission.WRITE_SMS"/>这个是写短信用到的。

写道 package com.jdjw.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
/**
*
* 读取第一条短信并删除
*
*/
public class TestSMSActivity extends Activity {

private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = this;
/*
* 删除第一条短信
long id = getThreadId();
Uri mUri=Uri.parse("content://sms/conversations/" + id);
mContext.getContentResolver().delete(mUri, null, null);
*/
getSMSInfo();
}
/**
* 读取第一条短信并删除
* @return
*/
private long getThreadId() {
long threadId = 0;
String SMS_READ_COLUMN = "read";
String SORT_ORDER = "date DESC";//时间降序查找
int count = 0;
Cursor cursor = mContext.getContentResolver().query(
Uri.parse("content://sms/inbox"),
new String[] { "_id", "thread_id", "address", "person", "date",
"body" }, null, null, SORT_ORDER);

if (cursor != null) {
try {
count = cursor.getCount();
if (count > 0) {
cursor.moveToFirst();
threadId = cursor.getLong(1);
}
} finally {
cursor.close();
}
}
Log.i("threadId", String.valueOf(threadId));

return threadId;
}

/**
* 得到全部短信
*/
private void getSMSInfo(){
Cursor cur = mContext.getContentResolver().query(Uri.parse("content://sms/inbox"), new String[]{
"_id","address","person","date","body","type"
},null,null, "date DESC");

StringBuilder smsBuilder = new StringBuilder();
if (cur.moveToFirst()) {
String name;
String phoneNumber;
String smsbody;
String date;
String type;

int nameColumn = cur.getColumnIndex("person");
int phoneNumberColumn = cur.getColumnIndex("address");
int smsbodyColumn = cur.getColumnIndex("body");
int dateColumn = cur.getColumnIndex("date");
int typeColumn = cur.getColumnIndex("type");
do{
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneNumberColumn);
smsbody = cur.getString(smsbodyColumn);

SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(Long.parseLong(cur.getString(dateColumn)));
date = dateFormat.format(d);

int typeId = cur.getInt(typeColumn);
if(typeId == 1){
type = "接收";
} else if(typeId == 2){
type = "发送";
} else {
type = "";
}

smsBuilder.append("[");
smsBuilder.append(name+",");
smsBuilder.append(phoneNumber+",");
smsBuilder.append(smsbody+",");
smsBuilder.append(date+",");
smsBuilder.append(type);
smsBuilder.append("] ");

if(smsbody == null) smsbody = "";
}while(cur.moveToNext());
}
System.out.println(smsBuilder.toString());

}

}

更多相关文章

  1. android 发送短信的两种方式,以及接收报告和发送报告
  2. Android(安卓)- 小功能 - Android短信功能实现源码
  3. Android(安卓)文件的上传
  4. android发送短信例子
  5. Android(安卓)UI系列-----ImageView的scaleType属性
  6. EditText弹出软件盘时不进行全屏
  7. android 反编译
  8. Android(安卓)Contacts(二)—— SMS 短信 与 Contacts 联系人关联
  9. Android事件分发机制练习---打造属于自己的瀑布流

随机推荐

  1. The connection to adb is down, and a s
  2. android简单应用(一)
  3. Android PK ios,是谁胜谁负
  4. Android中图片的处理(放大缩小,去色,转换格
  5. APPS大乱斗:4大Android文件浏览器横评(一)
  6. 【Android 多语言切换简单实例分享】
  7. 在Ubuntu上下载、编译和安装Android最新
  8. Android系统:见证“开源”的力量
  9. Eclipse如何打开Package Explorer
  10. android apk安装原理分析