您的位置:首页 > 其它

手机中预置联系人(姓名,号码,头像)

2014-06-03 19:42 459 查看
此方法比较适合预置联系人的数目不是特别多的情况

【注意】如果您不需要限制预置联系人的删除/编辑操作, 去掉第三步”新增函数“

中的语句: contactvalues.put(RawContacts.IS_SDN_CONTACT, -1);

File:packages/apps/Contacts/src/com/mediatek/contacts/simservice/SIMProcessorService.java

1.引入包

+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import com.android.contacts.R;
+import android.content.OperationApplicationException;
+import android.database.Cursor;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.net.Uri;
+import android.os.RemoteException;
+import android.provider.ContactsContract.PhoneLookup;
+import android.content.ContentProviderOperation;
+import android.provider.ContactsContract.RawContacts
+import android.content.ContentValues;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.CommonDataKinds.StructuredName;
+import android.provider.ContactsContract.CommonDataKinds.Photo;
+import com.android.contacts.common.model.account.AccountType;

2.增加变量

private static boolean sIsRunningNumberCheck = false;
private static final int INSERT_PRESET_NUMBER_COUNT = xxx;           //预置联系人的个数
private static final String   INSERT_PRESET_NAME[] = {"xxx1","xxx2",...};  //各预置联系人的姓名
private static final String   INSERT_PRESET_NUMBER[] = {"xxx1","xxx2",...};  //各预置联系人的号码
private static final int INSERT_PRESET_PHOTO[] = {R.drawable.contacts_photo,....}//各预置联系人头像
3.增加函数(将预置联系人信息写入数据库中)

private void importDefaultReadonlyContact() {
new Thread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "isRunningNumberCheck before: " + sIsRunningNumberCheck);
if (sIsRunningNumberCheck) {
return;
}
sIsRunningNumberCheck = true;
             for(int i = 0;i < INSERT_PRESET_NUMBER_COUNT; i++)
             {
Log.i(TAG, "isRunningNumberCheck after: " + sIsRunningNumberCheck);
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri
.encode(INSERT_PRESET_NUMBER[i]));
Log.i(TAG, "getContactInfoByPhoneNumbers(), uri = " + uri);
Cursor contactCursor = getContentResolver().query(uri, new String[] {
PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_ID
}, null, null, null);
try {
if (contactCursor != null && contactCursor.getCount() > 0) {
return;
} else {
final ArrayList<ContentProviderOperation> operationList = new
ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = ContentProviderOperation
                       .newInsert(RawContacts.CONTENT_URI);
ContentValues contactvalues = new ContentValues();
contactvalues.put(RawContacts.ACCOUNT_NAME,
AccountType.ACCOUNT_NAME_LOCAL_PHONE);
contactvalues.put(RawContacts.ACCOUNT_TYPE,
AccountType.ACCOUNT_TYPE_LOCAL_PHONE);
contactvalues.put(RawContacts.INDICATE_PHONE_SIM,
       ContactsContract.RawContacts.INDICATE_PHONE);
contactvalues.put(RawContacts.IS_SDN_CONTACT, -1);
builder.withValues(contactvalues);
builder.withValue(RawContacts.AGGREGATION_MODE,
RawContacts.AGGREGATION_MODE_DISABLED);
operationList.add(builder.build());
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Phone.RAW_CONTACT_ID, 0);
builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
builder.withValue(Phone.TYPE, Phone.TYPE_MOBILE);
builder.withValue(Phone.NUMBER, INSERT_PRESET_NUMBER[i]);
builder.withValue(Data.IS_PRIMARY, 1);
operationList.add(builder.build());
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(StructuredName.RAW_CONTACT_ID, 0);
builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(StructuredName.DISPLAY_NAME, INSERT_PRESET_NAME[i]);
operationList.add(builder.build());
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Photo.RAW_CONTACT_ID,0);
builder.withValue(Data.MIMETYPE,Photo.CONTENT_ITEM_TYPE);
Bitmap contactPhoto = BitmapFactory.decodeResource(getResources(),INSERT_PRESET_PHOTO[i]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
contactPhoto.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] photo = baos.toByteArray();
builder.withValue(Photo.PHOTO, photo);
operationList.add(builder.build());
try {
getContentResolver().applyBatch(
ContactsContract.AUTHORITY, operationList);
} catch (RemoteException e) {
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) {
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
}
}
         } finally {
// when this service start,but the contactsprovider has not been started yet.
// the contactCursor perhaps null, but not always.(first load will weekup the provider)
// so add null block to avoid nullpointerexception
if (contactCursor != null) {
contactCursor.close();
}
}
             }//for
Log.i(TAG, "isRunningNumberCheck insert: " + sIsRunningNumberCheck);
sIsRunningNumberCheck = false;
}
}).start();
}
4.onStart中调用这个函数:

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
+            importDefaultReadonlyContact();
processIntent(intent);
}


参考文档:

MTK FAQ

android 存储联系人(save contact) 总结
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐