5

这很奇怪,我已经搜索了论坛。

在我的主类中,我有一个按钮 onClick 将启动联系人应用程序,如下所示。当我单击该按钮时,会显示他的联系人列表,但只要我点击一个联系人,就会引发安全异常。

public void selectContacts(View v) {
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

        case PICK_CONTACT:
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                while (c.moveToNext()) {
                    String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                    String phoneNumber = getPhoneNumber(id);
                    listOfContacts.add(phoneNumber);

                }
            } else {
                System.out.println("User didn't pick a contact");
                finish();
            }
            break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

我检查了清单,并尝试了在应用程序、活动等中放置使用权限标签的所有组合。但没有任何效果。这是我的清单。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.company.letsmeet"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permision android:name="android.permission.SEND_SMS" />
    <uses-permision android:name="android.permission.RECEIVE_SMS" />
    <uses-permision android:name="android.permission.INTERNET" />
    <uses-permision android:name="android.permission.READ_CONTACTS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LetsmeetActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

安全例外:

05-04 11:26:39.950: E/AndroidRuntime(3861): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/7 }} to activity {in.company.letsmeet/in.company.letsmeet.LetsmeetActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.SemcContactsProvider2 uri content://com.android.contacts/contacts/7 from pid=3861, uid=10093 requires android.permission.READ_CONTACTS

任何帮助将不胜感激。我正在 Android 2.1 Sony experia S10 mini 上尝试这个。

4

2 回答 2

1

所以你猜怎么着,我在 Manifest GUI 中添加了 READ_CONTACTS 权限,它就可以工作了。上帝我在这上面浪费了一整天。希望其他人不用。

于 2012-05-04T17:29:37.927 回答
0

试试下面的代码。这可能对你有帮助 -

public class PickDemo extends Activity {
private static final int PICK_REQUEST=1337;
private static Uri CONTENT_URI=null;

static {
int sdk=new Integer(Build.VERSION.SDK).intValue();

if (sdk>=5) {
  try {
    Class<?> clazz=Class.forName("android.provider.ContactsContract$Contacts");

    CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
  }
  catch (Throwable t) {
    Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
  }
}
else {
  CONTENT_URI=Contacts.People.CONTENT_URI;
}
  }

  @Override
  public void onCreate(Bundle icicle) {
super.onCreate(icicle);

if (CONTENT_URI==null) {
  Toast
    .makeText(this, "We are experiencing technical difficulties...",
              Toast.LENGTH_LONG)
    .show();
  finish();

  return;
}

setContentView(R.layout.main);

Button btn=(Button)findViewById(R.id.pick);

btn.setOnClickListener(new View.OnClickListener() {
  public void onClick(View view) {
    Intent i=new Intent(Intent.ACTION_PICK, CONTENT_URI);

    startActivityForResult(i, PICK_REQUEST);
  }
});
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    if (requestCode==PICK_REQUEST) {
  if (resultCode==RESULT_OK) {
      startActivity(new Intent(Intent.ACTION_VIEW,
                                data.getData()));
  }
}
  }
}

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1"
      android:versionName="1.0"
      package="com.commonsware.android.contacts.pick"
      xmlns:android="http://schemas.android.com/apk/res/android">

  <uses-sdk android:minSdkVersion="3"
        android:targetSdkVersion="6" />
  <supports-screens android:largeScreens="true"
                android:normalScreens="true"
                android:smallScreens="false" />
  <application android:icon="@drawable/cw"
           android:label="@string/app_name">
    <activity android:label="@string/app_name"
          android:name=".PickDemo">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
    </activity>
  </application>
</manifest>

而且,只需参考Commonsware 的示例。希望这对您有所帮助。

于 2012-05-04T06:18:04.900 回答