【问题标题】:Uri Query SMS to get todays sent SMS countUri 查询 SMS 以获取今天发送的 SMS 计数
【发布时间】:2018-07-29 08:02:34
【问题描述】:

我正在从code 发送SMS 并更新Systems SMS Sent 消息;与:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
{
    int subscriptionId = SmsManager.getDefaultSmsSubscriptionId();
    SmsManager MySmsManager = SmsManager.getSmsManagerForSubscriptionId(subscriptionId);
    ArrayList<String> msgArray = MySmsManager.divideMessage(DefaultMsgTemplate);
    MySmsManager.sendMultipartTextMessage(SendSMSTo, null,msgArray, null, null);
    CurrentSmsParts = msgArray.size();

    Log.d("SMS DETAILS : ", "\nFOUND LOLLIPOP MR1 OR ABOVE...");
    Log.d("SMS DETAILS : ", "\nDETECTED DEFAULT SMS SIM..."+subscriptionId);
    Log.d("SMS DETAILS : ", "\nSENT MSG USING SIM..."+subscriptionId);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
{
    SmsManager MySmsManager = SmsManager.getDefault();
    ArrayList<String> msgArray = MySmsManager.divideMessage(DefaultMsgTemplate);
    MySmsManager.sendMultipartTextMessage(SendSMSTo, null,msgArray, null, null);
    CurrentSmsParts = msgArray.size();

    Log.d("SMS DETAILS : ", "\nFOUND BELOW LOLLIPOP MR1...");
    Log.d("SMS DETAILS : ", "\nDETECTED DEFAULT SMS SIM WHICHEVER AVAILABLE...");
    Log.d("SMS DETAILS : ", "\nSENT MSG USING SIM1 OR SIM2 WHICHEVER IS AVAILABLE...");
}


ContentValues values = new ContentValues();
values.put("address", SendSMSTo);
values.put("body", DefaultMsgTemplate);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);

SMSDone++;

这里SMSDone 是我的counter,通过我的应用程序计算today sent SMSs...; But Users are also able to send SMSs, without my application..!!

所以..,

  1. 如何查询 SMS uri 以获取两张 SIM 卡/号码今天发送的 SMS 号码?
  2. 我在已发送短信中插入短信;但是如何从我的应用程序中插入 SIM 卡号码?

【问题讨论】:

  • “今天”是什么意思?过去的 24 小时?还是从午夜开始? SIM卡号存储在Telephony.Sms.SUBSCRIPTION_ID"sub_id")列中。
  • @MikeM。我的意思是从午夜开始。换句话说,仅适用于今天的日期。 Like $For today 4 SMSs from SIM1 & & & SMSs from SIM2 via Uri query
  • 好吧,我假设你知道如何query(),因为你知道如何insert(),是吗?消息时间存储在"date" 列中,以纪元毫秒为单位。简单示例:获取一个Calendar 实例——将自动设置为当前日期/时间——然后将set()HOUR_OF_DAYMINUTESECONDMILLISECOND 设置为0。然后使用date + " &gt; " + cal.getTimeInMillis()where 子句进行查询。知道了?然后,您可以通过"sub_id" 列确定哪些消息来自哪个 SIM 卡。如果您希望将它们分成不同的批次,也可以将该列集成到查询中。
  • @MikeM.,是的,我查询过联系方式 uri 和 sms,但我不是很流利;我还使用 HH、MM、SS 在日历实例中设置时间,用于设置警报和其他情况。我明白了,但我坚持并要求用相同的代码回答这个问题;这样我就可以接受它作为答案,并且对将来搜索此内容的其他人也有帮助...
  • @MikeM.,是的.. 这当然是多余的。 HOUR_OF_DAY, MINUTE, SECOND 只为 0..!!非常感谢。享受编码的美好时光..

标签: java android sms


【解决方案1】:

致谢:Mike M

按照他的指示,通过早期 uri 查询的基础知识,这是我实现的最终答案....

Log.d("Executing :", "from here....\n");

try 
{
Calendar c1 = Calendar.getInstance();
c1.setTime(new Date());

Calendar c2 = Calendar.getInstance();
c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
c2.set(Calendar.MONTH, c1.get(Calendar.MONTH));
c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
c2.set(Calendar.HOUR_OF_DAY, 0);
c2.set(Calendar.MINUTE, 0);
c2.set(Calendar.SECOND, 0);

long TodayStartedAt = c2.getTimeInMillis();

Log.d("Today started at ", "here" + TodayStartedAt);

final Uri SMS_INBOX = Uri.parse("content://sms/sent");
Cursor cursor = getContentResolver().query(SMS_INBOX, null, "date>=" + TodayStartedAt, null, null);
List < String > items = new ArrayList < String > ();

int counter = 0;

while (cursor.moveToNext()) 
{
    String Date = cursor.getString(cursor.getColumnIndex("date"));
    String SmsBody = cursor.getString(cursor.getColumnIndex("body"));
    String PhoneNumber = cursor.getString(cursor.getColumnIndex("address"));
    String FromSIM = cursor.getString(cursor.getColumnIndex("sub_id"));

    Log.d("Counter : ", counter + "\n");

    Log.d("Date : ", Date + "\n");
    Log.d("PhoneNumber : ", PhoneNumber + "\n");
    Log.d("FromSIM : ", FromSIM + "\n");
    Log.d("SmsBody : ", SmsBody + "\n");

    counter++;
}
cursor.close();
} 
catch (Exception e) 
{
Log.e("exception :", "is", e);
}

感谢@Mike M。并发布,因为它可能会在未来帮助其他人......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多