1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
package sms.add.contact;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.ContentUris;
import android.content.ContentValues;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.os.Bundle;
import android.net.Uri;
import android.telephony.SmsMessage;
public class SMSReceiver extends BroadcastReceiver
{
private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION_RECEIVE_SMS))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } if (messages.length > -1)
{
final String messageBody = messages[0].getMessageBody();
if (messageBody.substring(0,8) == "#contact#")
{
//messageBody structure : "#contact#;nom;prenom;phone;adresse;cp;ville;email"
String infoContact[] = messageBody.split(";");
//création de l'id
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, "com.anddroid.contacts.sim");
values.put(RawContacts.ACCOUNT_NAME, "SIM");
Uri rawContactUri = context.getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
// infoContact[1] = "nom";
// infoContact[2] = "prenom";
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, infoContact[1]+" "+infoContact[2]);
values.put(StructuredName.GIVEN_NAME, infoContact[2]);
values.put(StructuredName.FAMILY_NAME, infoContact[1]);
context.getContentResolver().insert(Data.CONTENT_URI, values);
// infoContact[3] = "phone";
values.clear();
values.put(Data.CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, infoContact[3]);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
context.getContentResolver().insert(Data.CONTENT_URI, values);
// infoContact[4] = "adresse";
// infoContact[5] = "cp";
// infoContact[6] = "ville";
values.clear();
values.put(Data.CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME);
values.put(StructuredPostal.STREET, infoContact[4]);
values.put(StructuredPostal.POSTCODE, infoContact[5]);
values.put(StructuredPostal.CITY, infoContact[6]);
context.getContentResolver().insert(Data.CONTENT_URI, values);
// infoContact[7] = "email";
values.clear();
values.put(Data.CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.DATA1, infoContact[7]);
values.put(Email.TYPE, Email.TYPE_HOME);
context.getContentResolver().insert(Data.CONTENT_URI, values);
}
}
}
}
}
} |
Partager