首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检索三星Galaxy S9上的所有者信息

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

Stack Overflow用户
提问于 2018-09-27 01:16:21
回答 1查看 542关注 0票数 4

我正在尝试以编程方式在Android应用程序中获取用户的配置文件信息。这在Pixel手机上运行良好,但在三星手机上不会返回任何结果。例如:

代码语言:javascript
复制
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));
}

在Pixel上,这将返回电话所有者的联系id。在Galaxy上,光标是空的。我猜想这是因为三星使用了一些专有版本的联系人,而这些联系人并没有通过标准的Android API暴露出来。有人能证实吗?有没有三星设备的替代品?

EN

回答 1

Stack Overflow用户

发布于 2018-09-30 20:16:27

是的,在以下情况下,您肯定会得到空值:

  1. 您尚未在联系人会话中创建用户配置文件。
  2. 如果您尚未将邮件帐户与配置文件链接在一起。

您最终可能会使用SecurityException,为了避免这种情况,我已经根据documentaion修改了代码

你肯定会收到一条警告,说cursor finalized without prior close(),如果你不打算使用更多的游标,最好关闭游标。

不要忘记在清单子节中包含权限。

清单文件:

代码语言:javascript
复制
      <?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>

活动代码:

代码语言:javascript
复制
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.
    }
}


}

如果你在我的代码缩进中发现困难,请通过这个google drive link

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52523035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档