我试图在同一活动中要求2个运行时权限。 第一个将按钮触发并询问READ_CONTACTS,如果允许则通过onActivityResult继续。 另一个运行时权限将询问SEND_SMS,如果允许,将转到onRequestPermissionsResult。 现在,据我所知,它是如何工作的 - 我做错了。 2运行时权限需要通过其中一个选项运行,我需要使用Switch Case或某些东西来构建一个处理2个权限的方法,这样它就不会崩溃。 我需要知道如何处理这两个事件,所以我不会允许联系人权限,活动将尝试运行SMS方法。
这是我无法摆脱的onActivityResult覆盖。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();
retrieveContactNumber();
}
}
catch (Exception e){
e.getStackTrace();
Toast.makeText(SmsActivity.this, "Hi, I Tried :P", Toast.LENGTH_SHORT).show();
}
}
private void retrieveContactNumber() {
Cursor cursorID = getContentResolver().query(uriContact, new String[]{ContactsContract.Contacts._ID}, null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactID);
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
txtCbNum = (EditText) findViewById(R.id.txtNum);
txtCbNum.setText(contactNumber, TextView.BufferType.EDITABLE);
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
private void requestSmsPermission() {
if (ContextCompat.checkSelfPermission(SmsActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SmsActivity.this,
new String[]{Manifest.permission.SEND_SMS},
PERMISSION_SEND_SMS);
} else {
contactNumber = txtCbNum.getText().toString();
sendSms(contactNumber, dibur);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == PERMISSION_SEND_SMS) {
contactNumber = txtCbNum.getText().toString();
sendSms(contactNumber, dibur);
} else if (requestCode == MY_PERMISSIONS_REQUEST_READ_CONTACTS) {
try {
// need to insert the contact number retrieve here
} catch (Exception e) {
e.getStackTrace();
Toast.makeText(SmsActivity.this, "Hi, I Tried :P", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(SmsActivity.this, "Permission denied, Can't send SMS.", Toast.LENGTH_LONG).show();
}
}
private void sendSms(String phoneNumber, String message) {
try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(SmsActivity.this, "Sms sent successfully to " + contactNumber, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(SmsActivity.this, "SMS failed, please try again." + contactNumber, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
答案 0 :(得分:1)
您可以通过作为第二个参数传递的整数来区分两个onRequestPermissionsResult()
个调用。该值作为参数提供给ActivityCompat.requestPermissions(this, ..., RESULT_PERMS_RECORD_VIDEO);
。
因此,例如,如果在一个地方你有:
...
(包含ActivityCompat.requestPermissions(this, ..., RESULT_PERMS_TAKE_PICTURE);
的权限名称数组)
在另一个地方你有:
...
(可能是 @Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
if (requestCode==RESULT_PERMS_TAKE_PICTURE) {
// do something
}
else if (requestCode==RESULT_PERMS_RECORD_VIDEO) {
// do something else
}
}
)
然后你可以:
10:00, 23:00, 1:00, 9:00