简体   繁体   English

检索三星Galaxy S9上的所有者信息

[英]Retrieving owner information on Samsung Galaxy S9

I am trying to programmatically obtain the user's profile information in an Android application. 我正在尝试以编程方式在Android应用程序中获取用户的个人资料信息。 This works fine on a Pixel phone but doesn't return any results on a Samsung phone. 在Pixel手机上可以正常工作,但在三星手机上不会返回任何结果。 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 the Pixel this returns the contact id of the phone's owner. 在Pixel上,这将返回手机所有者的联系人ID。 On the Galaxy the cursor is empty. 在Galaxy上,光标为空。 I'm assuming this is because Samsung is using some proprietary version of contacts that is not exposed through the standard Android API. 我认为这是因为三星正在使用某些专有版本的联系人,而该版本未通过标准Android API公开。 Can anyone confirm? 谁能确认? Is there an alternative for Samsung devices? 三星设备有替代品吗?

Yep you will certainly end up with null values in the following cases: 是的,在以下情况下,您肯定会得到空值:

  1. You haven't created the user profile in your contacts session yet. 您尚未在联系人会话中创建用户个人资料。

  2. If you haven't linked your mail account with the profile. 如果您尚未将邮件帐户与个人资料相关联。

You might end up with SecurityException and to avoid that i have modified the code as per the documentaion 您可能会遇到SecurityException并避免按照文档修改我的代码

You will surely receive a warning saying cursor finalized without prior close() , it is a best practice to close the cursors if you are not going to use urther. 您肯定会收到一条警告,指出光标在没有事先close()的情况下完成 ,如果您不打算再使用它,最好的方法是关闭光标。

don't forget to include permissions in manifest child section. 不要忘记在清单子节中包括权限。

MANIFEST 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 difficulty in the indentation of my code please go through this google drive link . 如果您在缩进我的代码时发现困难,请通过此google驱动器链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM