Meta-Daten von empfangenen/ausgehenden SMS auslesen

S

sem_thesis

Fortgeschrittenes Mitglied
0
Hallloooo!:)



Heute geht's um einen neuen Feature
--> SMS Mining

Ich möchte einen Service, der auf ausgehende und hereinkommende SMS reagiert und die folgenden Werte speichert:
- Datum
- Zeit
- Kontaktperson
- Nummer der Kontaktperson
- # Zeichen des SMS

Inhalt der SMS wird nicht ausgelesen.


Jetzt habe ich mich auf StackOverFlow und das Programm 'SMS Backup' (https://github.com/jberkel/sms-backup-plus/tree/master/src/com/zegoggles/smssync) angeschaut um Tipps zu holen.


Den ganzen Service habe ich unterteilt in 3 Teile:
1. SMS History Collection
2. Incoming SMS
3. Outgoing SMS


2. Teil
Mit dem 2. Teil habe ich schon angefangen - ich kann den Inhalt + Absender von empfangener SMS anzeigen.

Die Klasse SMSReceiver.java hört auf die Variable 'android.provider.Telephony.SMS_RECEIVED'

Code:
public class SMSReceiver extends BroadcastReceiver {

    private static final String TAG = "SMSReceiver";
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";


    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            
      
            /* The SMS-Messages are 'hiding' within the extras of the Intent. */
            Bundle bundle = intent.getExtras();

            if (bundle != null) {
                    /* Get all messages contained in the Intent*/
                    Object[] pdus = (Object[])bundle.get("pdus");

                    final SmsMessage[] messages = new SmsMessage[pdus.length];


                    /* Feed the StringBuilder with all Messages found. */

                    for (int i = 0; i < pdus.length; i++) {
                        messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    }
                    
                    if (messages.length > -1) {
                        
                        Log.i(TAG, "SMS Received!");
                        Log.d(TAG, "Sender: " + messages[0].getDisplayOriginatingAddress());
                        Log.d(TAG, "Content: " + messages[0].getDisplayMessageBody());
                        Log.d(TAG, "Number of Characters: " + pdus.length);
                    }
            }
        }
    }
    
    
}
3. Teil

Hier habe ich erstmals einfach nur versucht den Namen der Kontaktperson anzeigen zu lassen - und schon das hat nicht funktioniert :huh:

Code:
public MySMSCollector()    {
        
     String selection = String.format("%s > ? AND %s <> ?", "date", "type");
     String sortOrder = "date";
                
     final Cursor c = getContentResolver().query(
                        Uri.parse("content://sms"),
                        null,
                        selection,
                        null,
                        sortOrder);
      int i = 1;

      if(c.moveToFirst()) {
                    
        while(!c.isAfterLast()) {    
                                        
                int p = c.getInt(c.getColumnIndex("person"));
                Log.d(TAG, "Person: " + p);
                        
                 c.moveToNext();            // move to the next entry
                 i++;
                        
         }
                    
   }
   c.close();
}
Hat jemand eine Idee/Vorschlag zum Teil 3 (oder sonstige Bemerkungen)?


PS: Die folgenden 2 Methoden der CursorToMessage.java Klasse enthalten die nötige Information:

https://github.com/jberkel/sms-back...m/zegoggles/smssync/CursorToMessage.java#L306
https://github.com/jberkel/sms-back...m/zegoggles/smssync/CursorToMessage.java#L161
 
Zuletzt bearbeitet:
Ich habe mich nun dem 1. Teil - SMS History Collection gewidmet.

Ich kann nun Name, Datum, Zeit & Personname von der SMS Datenbank (sms//inbox) auslesen (da drin sind alle incoming sms enthalten).
Mit dem Body klappt es leider nicht.

Code:
public class SMSService extends Service    {
    
    
    ///////////////// Declaration of variables //////////////////////
    
    
    private static final String TAG = SMSService.class.getSimpleName();
    public MySMSCollector q;

    
    // Specify formatting of data and time --> looks awesome now :D
    SimpleDateFormat formatdate = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat formattime = new SimpleDateFormat("HH:mm:ss");
    
    
    
    @Override
    public void onCreate() {
        super.onCreate();
        
        /* Info */
          Log.d(TAG, "SMSService created");
          

          /* Create Collector */
          q = new MySMSCollector();
          
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    
    public class MySMSCollector    {
        
        public MySMSCollector()    {
          
            
            Cursor rawContacts = getContentResolver().query(
                    RawContacts.CONTENT_URI,    // the URI for raw contact provider
                    null,                    
                    null,                        // selection = null, retrieve all entries
                    null,                        // selection is without parameters
                    null);                        // do not order
            
            
            /* First: Let's get all the incoming messages
             * For this we query the sms database
             * We choose the appropriate category: inbox 
             */
            
            final Cursor c = getContentResolver().query(
                        Uri.parse("content://sms/inbox"),
                        null,
                        null,
                        null,
                        null);
                
                Log.d(TAG, "------------------------");
                int i = 1;
                if(c.moveToFirst())    {
                    
                    while(!c.isAfterLast()) {    
                        
                        int ID = c.getInt(c.getColumnIndex("thread_id"));
                        long then = c.getLong(c.getColumnIndex("date"));
                        int p = c.getInt(c.getColumnIndex("person"));
                        int b = c.getInt(c.getColumnIndex("body"));
                        
                        
                        // Start
                        Log.d(TAG, "Entry Nr. " + i);
                        
                        // Get Thread-ID
                        Log.d(TAG, "Thread ID: " + ID);                        
                        
                        // Get Person-Name
                        if(p != 0)
                        {
                            rawContacts.moveToFirst();
                            
                            do
                            {
                                if(rawContacts.getInt(rawContacts.getColumnIndex(RawContacts.CONTACT_ID)) == p)
                                {
                                    Log.d(TAG, "Person: " + rawContacts.getString(rawContacts.getColumnIndex(Contacts.DISPLAY_NAME)));
                                }
                            }    while(rawContacts.moveToNext());
                            
                        }    else    {
                            Log.d(TAG, "Unknown Person");

                        }
                        
                        // Get Date                        
                        String date = formatdate.format(then);
                        String time = formattime.format(then);
                        Log.d(TAG, "Date: " + date);
                        Log.d(TAG, "Time: " + time);
                        
                        
                        
                        // Get Type
                        Log.d(TAG, "Type: Incoming");
                        
                        // Get Body
                        if(b != 0)
                        {
                            Log.d(TAG, "Body: " + c.getString(b));
                        }
                        
                        c.moveToNext();            // move to the next entry
                        i++;
                        
                    }
                    
                }
                
                c.close();
            
        }    
            
    }
    
}


Die BODY-Variable ist immer 0 (Wieso?). Somit wird dessen Wert nie geloggt.
 
War ein ganz dummer Fehler.
So hat's gepasst:
Code:
int b = c.getInt(c.getColumnIndex("body"));
Der restliche Service läuft nun auch.
Falls sich jemand für den Code interessiert --> einfach PM'en :)

Ansonsten ist das Problem gelöst und der Thread geschlossen :)
 

Ähnliche Themen

H
Antworten
2
Aufrufe
1.311
Hcman
H
A
Antworten
10
Aufrufe
1.022
swa00
swa00
S
Antworten
17
Aufrufe
556
jogimuc
J
Zurück
Oben Unten