android - How to get the SMS date from messenger?
问题描述
Android default messenger sort SMS in several groups using received time(picture).
I am trying to show sms inbox in custom listview like default messenger using cursor:
// Create Inbox box URI
Uri inboxURI = Uri.parse( "content://sms/inbox" );
// List required columns
String[] reqCols = new String[]{"_id", "address", "body", "date"};
// Get Content Resolver object, which will deal with Content
// Provider
ContentResolver contentResolver = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor cursor = contentResolver.query( inboxURI, reqCols, null, null,
null );
sms = new ArrayList<>();
String date = "";
cursor.moveToLast();
while (cursor.moveToPrevious() || cursor.isLast()) {
//to get phone number
address = cursor.getString( cursor.getColumnIndex( "address" ) );
//to get massage
body = cursor.getString( cursor.getColumnIndexOrThrow( "body" ) );
//to get date of sms
date = cursor.getString( cursor.getColumnIndexOrThrow( "date" ) );
Long timestamp = Long.parseLong( date );
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis( timestamp );
Date finalDate = calendar.getTime();
smsDate = finalDate.toString();
but it is not working like that.It's like "Sun Aug 12 07:13:13 .....". I want to show time same as default messenger. What is the solution?
解决方案
在 Sms 表中,日期以毫秒为单位存储为 INTEGER。所以,使用
long sms_time= cur.getLong(cur.getColumnIndexOrThrow("date")) on Cursor.
这是接收 SMS 的时间(以毫秒为单位)。现在以毫秒为单位获取当前时间。
long now = System.currentTimeMillis();//get Current Time
现在计算 M=Difference In Minutes:
long minutes=(now - sms_time)/(1000*60);
推荐阅读
- git - git 日志 --since=
行为不端 - xml - XSLT:将通配符掩码转换为网络掩码和网络掩码格式
- sql-server - SSDT 项目发布事务部署
- javascript - App.js 拒绝应用样式,拒绝执行脚本
- go - Go 中的微服务消息传递
- algorithm - 渐近时间复杂度 O(sqrt(n)log(n)n)
- smartcard - 使用 CNG 在给定令牌上导入密钥
- c++ - shared_ptr 未正确返回
- python - 使用 Selenium 查找断开的链接。HTTP 302,预计 HTTP 404
- asp.net - 将应用程序角色作为自定义声明添加到 Azure AD 身份