如何在android中添加超过5000的批量联系人?

时间:2015-05-26 08:38:09

标签: android performance android-intent android-contentprovider android-contacts

我正在尝试在android中添加多个联系人,因为我使用了一个服务,因此我的UI不会受到影响。但问题是我得到一个错误,屈服点不能一次接受超过500。现在我在大块尝试它仍然无法在一个GO中添加超过120个联系人!!请回复。

import java.util.ArrayList;

import android.app.IntentService;
import android.content.ContentProviderOperation;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.util.Log;
import android.widget.Toast;

public class MyServices extends IntentService {

    long mNumber;
    String cMobile;
    static int rawContactID;
    public static String CC = "countrycode";
    public static String PHONENUMBER = "";
    ArrayList<ContentProviderOperation> ops;

    public MyServices() {
        super("adding Contatcts");

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // String cc = intent.getStringExtra(CC);
        String cNumber = intent.getStringExtra(PHONENUMBER);
        // Log.e("CC", String.valueOf(cc));

        try {

            mNumber = Long.parseLong(cNumber);

        } catch (Exception e) {

        }

        int j = 1001;
        ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(RawContacts.ACCOUNT_NAME, null).build());
        for (int i = 1; i < 220; i++) {
            rawContactID = ops.size();

            if (rawContactID % 100 == 1) {
                doOpration();
                ops = new ArrayList<ContentProviderOperation>();
            }

            String newName = "WhatsAppDating".concat("  " + j);
            j++;
            long k = mNumber;
            long m = k + i;
            Log.e("adding", String.valueOf(m));
            String fNumber = String.valueOf("+91" + m);

            // Adding insert operation to operations list
            // to insert display name in the table ContactsContract.Data
            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(
                            ContactsContract.Data.RAW_CONTACT_ID, i)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(StructuredName.DISPLAY_NAME, newName).build());

            // Adding insert operation to operations list
            // to insert Mobile Number in the table
            // ContactsContract.Data
            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(
                            ContactsContract.Data.RAW_CONTACT_ID, i)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            Phone.CONTENT_ITEM_TYPE)
                    .withValue(Phone.NUMBER, fNumber)
                    .withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)
                    .build());

        }

    }

    private void doOpration() {
        try {
            Log.e("One 100 done", String.valueOf(ops.size()));
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(),
                "Contacts are successfully added", Toast.LENGTH_SHORT).show();
        return;
    }

}

0 个答案:

没有答案