Java 如何在联系人列表中添加新联系人时通知我的应用程序

Java 如何在联系人列表中添加新联系人时通知我的应用程序,java,android,Java,Android,我正在开发一个需要获取联系人列表的应用程序。获得所有联系需要时间。所以我想在本地存储联系人列表。但我需要在手机中添加新联系人时更新。如何通知我的应用程序新联系人已添加到设备中? 我使用此代码从设备获取联系人:` public void getContact() { ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,

我正在开发一个需要获取联系人列表的应用程序。获得所有联系需要时间。所以我想在本地存储联系人列表。但我需要在手机中添加新联系人时更新。如何通知我的应用程序新联系人已添加到设备中? 我使用此代码从设备获取联系人:`

 public void getContact()
{
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    Log.e("curcouuntLOL",cur.getCount()+"");

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {

            String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME));
            String photoUri = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(

                    ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                modelPhone=new ModelPhone();

                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {


                    String phoneNo = pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));




                    Toast.makeText(PhoneNumberlistActivity.this, "Name: " + name
                            + ", Phone No: " + phoneNo +" ,photoUri :" + photoUri, Toast.LENGTH_SHORT).show();

                }
                pCur.close();
            }
        }
    }



}`
这样你就可以:

public void generateNotification(String yourVariable) {
        Intent offlineIntent = new Intent(this, NotificationReceiverActivity.class);
        offlineIntent.setAction("Go Offline");

        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);

        long when = System.currentTimeMillis();

        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent notificationIntent = new Intent(getApplicationContext(), ActivityHome.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);


        PendingIntent mainPIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;

        Notification noti = new Notification.Builder(this)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(alertMessage)
                .setSmallIcon(R.mipmap.logo)
                .setLargeIcon(largeIcon)
                .setAutoCancel(true)
                .setSound(alarmSound).setAutoCancel(true).setWhen(when)
                .setContentIntent(mainPIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(m, noti);

    }

请参阅
ContentResolver#registerContentObserver
感谢回复@pskink,如何使用它或您是否有任何链接?“如何使用”什么?任何链接或任何东西,我可以从中获得详细信息?它是否适用于所有android版本?请参阅-1这并没有回答问题。在显示通知时,也不应使用随机数来分配通知。这样,您就无法在需要时删除或修改通知。