请帮我找到在android中存储短信日志的代码。我必须在应用程序上工作来存储calllog和smslog。我得到了calllog的代码,但是smslog我无法得到正确的代码。我只需要从电话中获取 smslog 的名称、号码、msgtext、日期和时间。请帮我获取一些代码。感谢您的帮助。
问问题
161 次
1 回答
0
这是您的解决方案...
调用此方法...根据需要捕获异常,这将在 LogCat 上打印所有短信详细信息...它还返回找到的短信数量...
public int getAllData() throws Exception {
// Declarations
int dataCount = 0;
// Details to get
String type = null;
String number = null;
String body = null;
String _date = null;
String date = null;
// Query details
Uri dbUri = Uri.parse("content://sms");
String[] projection = { "type",
"address",
"body",
"date" };
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
// Getting results of query
Cursor smsInfoCursor = getContentResolver().query(dbUri, projection, selection, selectionArgs, sortOrder);
// Displaying data
Log.i(TAG, "Displaying SMS info");
if (smsInfoCursor.getCount()>0) {
smsInfoCursor.moveToFirst();
do {
// type
type = smsInfoCursor.getString(0);
if (type == null)
type = "";
Log.v("type", type);
// number
number = smsInfoCursor.getString(1);
if (number == null)
number = "";
Log.v("number", number);
// body
body = smsInfoCursor.getString(2);
if (body == null)
body = "";
Log.v("body", body);
// date time of message
date = smsInfoCursor.getString(3);
if (date == null)
date = "";
Log.v("Date", date);
Log.v("-----", "-----");
} while(smsInfoCursor.moveToNext());
}
dataCount = smsInfoCursor.getCount();
}
smsInfoCursor.close();
return dataCount;
}
您还必须在清单中添加以下权限...
<uses-permission android:name="android.permission.READ_SMS" />
您还可以根据需要修改选择参数...
希望这有效... :)
于 2012-05-29T10:03:28.610 回答