android获取所有短信

package com.example.call;


import java.text.SimpleDateFormat;
import java.util.Date;
import com.example.contacts.R;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import android.view.Menu;
public class SmsConentActivity extends Activity {
protected static final String ACTIVITY_TAG = "MyAndroid";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_conent);
final String SMS_URI_INBOX = "content://sms/inbox";// 收信箱
try {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
Uri uri = Uri.parse(SMS_URI_INBOX);
Cursor cursor = cr.query(uri, projection, null, null, "date desc");
while (cursor.moveToNext()) {
// -----------------------信息----------------
int nameColumn = cursor.getColumnIndex("person");// 联系人姓名列表序号
int phoneNumberColumn = cursor.getColumnIndex("address");// 手机号
int smsbodyColumn = cursor.getColumnIndex("body");// 短信内容
int dateColumn = cursor.getColumnIndex("date");// 日期
int index_Type = cursor.getColumnIndex("type");// 收发类型 1表示接受
// 2表示发送
String nameId = cursor.getString(nameColumn);
String phoneNumber = cursor.getString(phoneNumberColumn);
String smsbody = cursor.getString(smsbodyColumn);
int intType = cursor.getInt(index_Type);
Date d = new Date(Long.parseLong(cursor.getString(dateColumn)));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd " + "hh:mm:ss");
String date = dateFormat.format(d);
String strType = null;
if (intType == 1) {
strType = "接收";
} else if (intType == 2) {
strType = "发送";
} else {
strType = "null";
}
// ---------------------匹配联系人名字--------------------------
Uri personUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
phoneNumber);
Cursor localCursor = cr.query(personUri, new String[] {
PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_ID,
PhoneLookup._ID }, null, null, null);
if (localCursor.getCount() != 0) {
localCursor.moveToFirst();
String name = localCursor.getString(localCursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME));
long photoid = localCursor.getLong(localCursor
.getColumnIndex(PhoneLookup.PHOTO_ID));
long contactid = localCursor.getLong(localCursor
.getColumnIndex(PhoneLookup._ID));
Log.d(SmsConentActivity.ACTIVITY_TAG, "电话号码" + phoneNumber
+ ",\n" + smsbody + "\n时间:" + date + "\n姓名:" + name
+ "\n类型:" + strType + "\n");


}
}
} catch (SQLiteException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sms_conent, menu);
return true;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值