سؤال

أنا أقوم بإنشاء هذا المنشور للحصول على المساعدة.أنا تطوير التطبيق الذي يرسل incoming text sms.ما أفعله هو جلب incoming message body, date and time وإرسالها كرسالة جديدة.لإرسال الغرض أنا باستخدام sms manager.أنا قادر على الحصول على نص رسالة متعددة باستخدام checkboxes وخلق list من الرسائل المحددة.ولكن المشكلة هي في الحصول على التاريخ والوقت.

رمز لقائمة مجموعة من الرسائل المحددة:

private List<SMSListModel> getModel() 
{
    if(cursor.getCount()>0)
    {
        for(int i=0;i<cursor.getCount();i++)
        {
            if(cursor.moveToPosition(i))
            {
                list.add(new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body"))));
            }
        }
    }
    return list;
}

رمز لإرسال نص الرسالة المحدد:

if(list.size()>0){
   for(int i=0;i<list.size();i++)
   {
       if(list.get(i).isSelected())
       {
        if(body.equals(""))
               body =list.get(i).getBody();

           else
            body =list.get(i).getBody();
         try
         {
             String mbody = "from"+ "dd/mm/yy" +"hh:mm"+body;
             SmsManager smsManager = SmsManager.getDefault();
             smsManager.sendTextMessage(phoneNo, null, mbody, null, null);
         }                           
         catch (Exception e)
         {
             //Error Occurred if No Messages Selected 
                e.printStackTrace();
         }

انظر مرة واحدة ويرجى تزويدي بالتعديلات المناسبة إن أمكن

هل كانت مفيدة؟

المحلول

يا محاولة مثل هذه الطريقة:

  ArrayList<Object> time = new ArrayList<Object>();

    Uri uri = Uri.parse("content://sms/inbox");
    c = getContentResolver().query(uri, null, null, null, null);
    startManagingCursor(c);

    // Read the sms data and store it in the list
    if (c.getCount() > 0) {
        if (c.moveToFirst()) {
            for (int i = 0; i < c.getCount(); i++) {
                String date = c.getString(c.getColumnIndex("date"));
                Long timestamp = Long.parseLong(date);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(timestamp);
                DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                String messageDate = formatter.format(calendar.getTime());
                time.add(messageDate);  
                c.moveToNext();
            }
        }

تحديث: وأيضا تغيير Date format مثل:

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

String strBody="from "+time.get(position)+body;

نصائح أخرى

تحتاج إلى تحديد عضو آخر داخل النموذج الخاص بك.وإليك كيف يمكنك قراءة التاريخ وتعيينه على النموذج الخاص بك عن طريق منشئ/واضعة.

private List<SMSListModel> getModel() 
{
    if(cursor.getCount()>0)
    {
        for(int i=0;i<cursor.getCount();i++)
        {
            if(cursor.moveToPosition(i))
            {
SMSListModel model=new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body")));

String date =  cursor.getString(cursor.getColumnIndex("date"));
    Long timestamp = Long.parseLong(date);    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    Date finaldate = calendar.getTime();
    String smsDate = finaldate.toString();

model.setSMSDate(smsDate);
                list.add(model);
            }
        }
    }
    return list;
}

داخل منطق رمز إرسال الرسائل القصيرة ، ما عليك سوى قراءته من النموذج وإضافته إلى جسم الرسائل القصيرة

list.get(i).getSMSDate();

لم تختبر هذا ولكن تحصل على هذه النقطة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top