4

在联系人列表更改时,调用此服务,如下所述。我没有使用此代码获得任何新添加的联系人。有什么替代方法吗?我的 MyContactService如下:

    public class MyContactService extends Service {
    String ContryCode = "91";
    String TAG ="MyContactService";

    MobileContactUID M_UID;
    ArrayList<ContentProviderOperation> ops;

    public MyContactService() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @SuppressLint("NewApi")
    @Override
    public void onCreate() {
        M_UID = new MobileContactUID(getBaseContext());
         ops =new ArrayList<ContentProviderOperation>();
        // ContryCode = Pref.getValue(getBaseContext(), Const.PREF_Contrycode,
        // "");

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new getAllContactsBackTask().execute();
        return super.onStartCommand(intent, flags, startId);
    }

    class getAllContactsBackTask extends AsyncTask<String, String, String> {
        String erroMessage = "";


        protected String doInBackground(String... args) {
            try {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, null, null, null);
                //getApplication().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,new MyContentObserver());
                while (phones.moveToNext()) {
                    int phone_type = phones.getInt(phones
                            .getColumnIndex(Phone.TYPE));


                        String id = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
                        String phone_name = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phone_mob = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String phone_image = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

                        Log.print(TAG, "My id->"+id);
                        Log.print(TAG, "name :-"+phone_name);
                        Log.print(TAG, "phone number :-"+phone_mob);


                        phone_mob = phone_mob.replaceAll("[^0-9]", "");
                        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                                  .withValue(Data.RAW_CONTACT_ID, id)
                                  .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                                  .withValue(Phone.NUMBER, phone_mob)
                                  .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
                                  .withValue(Phone.LABEL, "free directory assistance")
                                  .build());




                        try {
                            System.out.println("<--------doInBackground is called ------->");
                            ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

                            Uri myContactUri = res[0].uri;
                            int lastSlash = myContactUri.toString().lastIndexOf("/");
                            int length = myContactUri.toString().length();
                            //int contactID = Integer.parseInt((String) myContactUri.toString().subSequence(lastSlash+1, length));

                            int contactID = (int) ContentUris.parseId(res[0].uri);


                            Log.error(TAG, "contactID====>"+contactID);



                        } catch (Exception e) {
                            e.printStackTrace();

                            Log.error(this.getClass()
                                    + " :: doInBackground() ::", e);

                        }




                }
                phones.close();

            } catch (Exception e) {
            }
            return erroMessage;
        }

        protected void onPostExecute(String result) {



            stopSelf();

        }

    }


}

任何建议都会很明显..在此先感谢。

4

0 回答 0