Java – Retrieve owner information on Samsung Galaxy S9

Retrieve owner information on Samsung Galaxy S9… here is a solution to the problem.

Retrieve owner information on Samsung Galaxy S9

I’m trying to get a user’s profile information programmatically in an Android app. This works fine on Pixel phones, but doesn’t return any results on Samsung phones. For example:

String contactId = null;

 getting contacts ID
Cursor cursorID = getContentResolver().query(ContactsContract.Profile.CONTENT_URI,
            new String[]{ContactsContract.Contacts._ID},
            null, null, null);

if (cursorID.moveToFirst()) {
    contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}

On Pixel, this returns the contact ID of the phone owner. On Galaxy, the cursor is empty. I’m assuming this is because Samsung is using some contacts with proprietary versions that are not exposed through the standard Android API. Can anyone confirm it? Is there an alternative to Samsung devices?

Solution

Yes, you will definitely get null values if:

  1. You haven’t been in yours yet
    Contact session.

  2. If you haven’t already, you have your mail account with
    Brief introduction.

You may end up with SecurityException, in order to avoid what I’ve already based documentaion modified the code

You’re sure to get a warning cursor finalized Without prior close() , if you do not plan to use urther, it is better to close the cursor.

Don’t forget to include permissions in the list subsection.

list file:

      <?xml version="1.0" encoding="utf-8"?>
    <manifest 

xmlns:android="http://schemas.android.com
      /apk/res/android"
     package="com.example.ganesh.contacts">
     <uses-permission 

android:name="android.permission.READ_CONTACTS" 
   />

<application
        android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
       <activity android:name=". MainActivity">
        <intent-filter>
            <action 
         android:name="android.intent.action.MAIN" />

<category 

android:name="android.intent.category.LAUNCHER" 
            />
        </intent-filter>
                  </activity>
               </application>

</manifest>

Activity code:

public class MainActivity extends 
     AppCompatActivity implements 
      View.OnClickListener {
        String contactId = null;
        Button button;
        TextView textView;
         @Override
         protected void onCreate(Bundle 
             savedInstanceState) {
         super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
         button=findViewById(R.id.button);
         textView=findViewById(R.id.textView);
        button.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    onReadContacts();
}

private void onReadContacts() {
     Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {

 Permission is not granted
         Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_CONTACTS)) {
             Show an explanation to the user *asynchronously* -- don't block
             this thread waiting for the user's response! After the user
             sees the explanation, try again to request the permission.
        } else {
             No explanation needed; request the permission
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS},
                    101);

 101 is an
             app-defined int constant. The callback method gets the
             result of the request.
        }
    } else {
         Permission has already been granted
        Cursor c = getApplication().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
        c.moveToFirst();
        textView.setText(c.getString(c.getColumnIndex("display_name")));
        Cursor cursorID = getContentResolver().query(ContactsContract.Profile.CONTENT_URI,
                new String[]{ContactsContract.Contacts._ID},
                null, null, null);

if (cursorID.moveToFirst()) {
            contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
            Toast.makeText(this,contactId,Toast.LENGTH_LONG).show();
        }

c.close();
        cursorID.close();

}
}
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 101
                : {
             If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Cursor c = getApplication().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
                c.moveToFirst();
                textView.setText(c.getString(c.getColumnIndex("display_name")));
                Cursor cursorID = getContentResolver().query(ContactsContract.Profile.CONTENT_URI,
                        new String[]{ContactsContract.Contacts._ID},
                        null, null, null);

if (cursorID.moveToFirst()) {
                    contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
                    Toast.makeText(this,contactId,Toast.LENGTH_LONG).show();
                }

c.close();
                cursorID.close();

 permission was granted, yay! Do the
                 contacts-related task you need to do.
            } else {
                 permission denied, boo! Disable the
                 functionality that depends on this permission.

}
            return;
        }

 other 'case' lines to check for other
         permissions this app might request.
    }
}

}

If you find that my code is having difficulty indenting
Please go through this Google Drive link

Related Problems and Solutions