【问题标题】:Retrieving owner information on Samsung Galaxy S9检索三星 Galaxy S9 的所有者信息
【发布时间】:2018-10-05 21:11:34
【问题描述】:

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

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 公开的专有版本的联系人。任何人都可以确认吗?三星设备有替代品吗?

【问题讨论】:

  • 你确定要求运行时权限吗?
  • 我想是的。它在一部手机上运行良好。在清单中,我请求了一堆权限,包括:
  • 您必须在运行时请求权限。看这篇文章developer.android.com/training/permissions/requesting
  • 是的,我打算这样做,但在测试时,我已手动授予对两部手机上联系人的访问权限。在这样做之前,我遇到了一个安全异常。授予访问权限后,我在任何一部手机上都不再出现异常,在 Pixel 手机上我得到了个人资料信息,在三星手机上我只得到零结果。

标签: java android


【解决方案1】:

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

  1. 您尚未在您的 联系人会话尚未。

  2. 如果您尚未将您的邮件帐户与 个人资料。

您最终可能会得到SecurityException 并避免我按照documentaion 修改了代码

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

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

清单文件:

      <?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>

活动代码:

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.
    }
}


}

如果您发现我的代码缩进有困难 请通过这个谷歌驱动器link

【讨论】:

  • @Nick Cardoso 它是从我的 android studio 复制和粘贴的。所以它的格式是这样的..
猜你喜欢
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2011-04-14
  • 2018-07-14
相关资源
最近更新 更多