我有一个活动调用另一个等待结果的活动(startActivityForResult()
)。第二个活动只是对用户的解释,而不管他们做什么,我想打开一个原生屏幕,让用户允许缺少的权限。我可以正常打开第二个活动,但onActivityResult()
永远不会被调用。
在同一个项目中,还有另外一对活动也是如此,它们运行良好。这两个在代码方面是相同的,但它们无论如何都不起作用。
第一项活动
public class RingtonesActivity extends Activity {
private boolean falta2Per = false;
private boolean faltaSoPerEsc = false;
private boolean faltaSoPerConf = false;
private static final int REQUEST_CODE_PERMISSION_RING = 0x0009;
private static final int REQUEST_CODE_WRITE_PERMISSION = 0x001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
…
}
private View.OnClickListener onClickToque() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
new MudarToquesTask().execute();
finish();
}
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PERMISSION_RING) {
if (ContextCompat.checkSelfPermission(RingtonesActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(RingtonesActivity.this, Manifest.permission.WRITE_SETTINGS)
!= PackageManager.PERMISSION_GRANTED) {
LogUtil.i("RINGTONES - Pedindo as 2 permissões.");
falta2Per = true;
//ped as 2 perm
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WRITE_SETTINGS},
REQUEST_CODE_WRITE_PERMISSION);
} else {
if (ContextCompat.checkSelfPermission(RingtonesActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
faltaSoPerEsc = true;
LogUtil.i("RINGTONES - Pedindo permissão de esc.");
//ped a perm de esc em area ext
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_WRITE_PERMISSION);
}
if (ContextCompat.checkSelfPermission(RingtonesActivity.this, Manifest.permission.WRITE_SETTINGS)
!= PackageManager.PERMISSION_GRANTED) {
faltaSoPerConf = true;
LogUtil.i("RINGTONES - Pedindo permissão p/ alt as conf");
//ped a perm p/ alt as conf
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_SETTINGS},
REQUEST_CODE_WRITE_PERMISSION);
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_WRITE_PERMISSION:
if (grantResults.length > 0) {
if (falta2Per) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
LogUtil.i("RINGTONES - Ambas as per concedidas.");
showDefaultToast(R.drawable.blink_emot, getString(R.string.agora_funciona));
} else {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
LogUtil.i("RINGTONES - Per de esc negada.");
showDefaultToast(R.drawable.lagrima_emot, "Desculpe, mas você negou a permissão de escrita.\nO app não podera realizar essa tarefa.");
}
if (grantResults[1] != PackageManager.PERMISSION_GRANTED) {
LogUtil.i("RINGTONES - Per p/ alt conf negada.");
showDefaultToast(R.drawable.lagrima_emot, "Desculpe, mas você negou a permissão para alterar as configurações.\nO app não podera realizar essa tarefa.");
}
}
falta2Per = false;
}
if (faltaSoPerEsc) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LogUtil.i("RINGTONES - Perm de esc concedida.");
showDefaultToast(R.drawable.blink_emot, getString(R.string.agora_funciona));
} else {
LogUtil.i("RINGTONES - Per de esc negada.");
showDefaultToast(R.drawable.lagrima_emot, "Desculpe, mas você negou a permissão de escrita.\nO app não podera realizar essa tarefa.");
}
faltaSoPerEsc = false;
}
if (faltaSoPerConf) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LogUtil.i("RINGTONES - Perm p/ alt as conf concedida.");
showDefaultToast(R.drawable.blink_emot, getString(R.string.agora_funciona));
} else {
LogUtil.i("RINGTONES - Per p/ alt as conf negada.");
showDefaultToast(R.drawable.lagrima_emot, "Desculpe, mas você negou a permissão para alterar as configurações.\nO app não podera realizar essa tarefa.");
}
faltaSoPerConf = false;
}
} else {
LogUtil.i("RINGTONES - Ambas as per negadas");
showDefaultToast(R.drawable.lagrima_emot, "Desculpe, mas você negou a(s) permissões. O app não podera realizar essa tarefa.");
}
break;
}
}
/**
* Task p/ altera o toque ou o som do sms numa thread aparte
*/
private class MudarToquesTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
if (ContextCompat.checkSelfPermission(RingtonesActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(RingtonesActivity.this, Manifest.permission.WRITE_SETTINGS)
== PackageManager.PERMISSION_GRANTED) {
...
} else {
LogUtil.i("RINGTONES - Falta ao menos uma das 2 permissões. Exibindo tela de explicações.");
hasPermission = false;
startActivityForResult(new Intent(getApplicationContext(), PermissionExplainRingtonesActivity.class),
REQUEST_CODE_PERMISSION_RING);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
...
}
}
}
}
第二项活动
public class PermissionExplainRingtonesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission_explain_ringtones);
inits();
}
private void inits() {
Button btnEntendi = (Button) findViewById(R.id.btn_entendi);
btnEntendi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});
}
@Override
public void onBackPressed() {
setResult(RESULT_OK);
super.onBackPressed();
}
}
答案 0 :(得分:1)
您的AsyncTask
在后台呼叫startActivityForResult
。尝试更改代码并在UI线程中调用它,可能在onPostExecute
的{{1}}中。
答案 1 :(得分:1)
问题是你从另一个线程调用startActivityForResult
这就是为什么它会破坏,就像其他人提到的那样。但你甚至不需要使用AsyncTask
,因为如果应用程序已经没有权限,你要做的就是检查权限并启动活动。
这不是一个很长的UI阻塞任务,因此可以在主线程上完成。
答案 2 :(得分:1)
如果您正在执行一些繁重的操作,并且不想阻止主UI线程,则应该使用AsyncTask。在您的情况下,为了检查授予的权限,您不需要在后台线程上执行此操作,可以在主线程上完成。
因为从另一个线程(后台线程)调用startActivityForResult而未调用onActivityResult ()
的原因