私は別の待機結果(startActivityForResult()
)を呼び出すアクティビティを持っています。この2番目のアクティビティは、ユーザーの説明に過ぎず、ユーザーの操作に関係なく、ユーザーに不足しているアクセス許可を許可するためのネイティブ画面を開きたいとします。私はこの第二の活動を正常に開くことができますが、onActivityResult()
は決して呼び出されません。onActivityResult()は呼び出されていませんが、別の場所で動作します
同じプロジェクトには、同じことを行う別のアクティビティペアがあり、正常に動作します。これらの2つはコード用語では同じですが、とにかく動作しません。
最初の活動
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();
}
}
?私が覚えているように、アクティビティの操作はメインスレッドで行われるはずです。メインスレッドの場合は、 'onPostExecute'から呼び出さなければなりません。 –