Domanda

Sto creando questo post per ottenere aiuto.Sto sviluppando un'applicazione che invia incoming text sms. Che cosa sto facendo recuperare incoming message body, date and time e inviarlo come nuovo messaggio.Per lo scopo di invio sto usando sms manager.Sono in grado di ottenere più corpo di messaggi utilizzando checkboxes e creare un list di messaggi selezionati.Ma il problema è ottenere la data e l'ora.

Code for Array Elenco dei messaggi selezionati:

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;
}
.

Codice per inviare il corpo del messaggio selezionato:

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();
         }
.

Guarda una volta e ti preghiamo di fornirmi modifiche corrette se possibile

È stato utile?

Soluzione

hey prova come in questo modo:

  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();
            }
        }
.

Aggiornamento: e cambia anche Date format come:

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

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

Altri suggerimenti

È necessario definire un altro membro all'interno del tuo modello.Ecco come puoi leggere la data e impostarlo sul modello tramite il costruttore / setter.

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;
}
.

All'interno della tua logica di invio del codice SMS, solo leggilo dal modello e aggiungilo al tuo corpo SMS

list.get(i).getSMSDate();
.

non ha testato questo ma ottieni il punto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top