OnActivityResult没有被调用,我想知道是不是因为我刚刚添加了一个权限请求(它之前工作正常)。下面是代码,它在请求许可后启动了startactivityforresult的意图。
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.v("showEmails","build<");
return true;
}
if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
return true;
}
if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
Snackbar.make(findViewById(android.R.id.content), "permission_rationale", Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, new View.OnClickListener() {
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onClick(View v) {
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
}
});
} else {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
Log.v("showEmails","requestPermissions");
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
ImageView ivShare = (ImageView) findViewById(R.id.ivShare);
ivShare.setImageURI(imageUri);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(Intent.EXTRA_TEXT, textIntent);
emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
emailIntent.setFlags(0);
startActivityForResult(emailIntent, GMAILSENT);
}
}
}