1

I am working on a app where need get contact list. It is taking time to get all contact. So i am thinking to store contact list locally. But i need update when new contact added in phone. How to notify my app that new contact added into device? I use this code to get contact from device: `

 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();
            }
        }
    }



}`
Suman
  • 1,307
  • 15
  • 32

1 Answers1

-3

This way u can :

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);

    }
Nik Patel
  • 627
  • 7
  • 21
  • -1 This doesn't answer the question. You also shouldn't use a random number to assign to the notification when shown. This way you can't remove nor modify the notification when you want to. – Rolf ツ Apr 17 '16 at 07:09