gpt4 book ai didi

android - OnDestroy 中的调用方法不起作用

转载 作者:行者123 更新时间:2023-11-29 16:38:29 31 4
gpt4 key购买 nike

我试图在我的 OnDestroy () 中调用一个服务,但是日志都不起作用,有人知道为什么吗?

@Override
protected void onDestroy() {
Log.e("onDestroy", "here");
startService(new Intent(this, ServiceOff.class));
super.onDestroy();
}

如果我在 onStop 上调用该服务,它会运行,即使它在我关闭应用程序时也被调用,但由于显而易见的原因 onStop 仍然可以打开应用程序,因此无法在那里调用它。

重要的是,我通过拉窗口模式并拖动侧面来关闭应用

完整的 Activity 代码:

public class editar_perfil extends AppCompatActivity {

FloatingActionButton ok;
TextInputEditText senha, new_senha;
ProgressBar login_progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editar_perfil);

reference();
}

private void reference() {
ok = findViewById(R.id.ok);
senha = findViewById(R.id.senha);
new_senha = findViewById(R.id.new_senha);
login_progress = findViewById(R.id.login_progress);

ok.setOnClickListener(v ->{
if(conectV(this)) {
if (ge(senha).length() > 7 && ge(new_senha).length() > 7) {
String paramentros = "session=" + session +
"&id=" + meu_id +
"&senha=" + ge(senha) +
"&new_senha=" + ge(new_senha);

stateAll(false, senha, new_senha, ok);

showProgress(true, login_progress, this);
new SolicitaDados(paramentros).execute(url + "new_senha.php");

} else {
Toast.makeText(this, "Tamanho de senha inválido!", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(this, "Sem conexão com a internet!", Toast.LENGTH_SHORT).show();
}
});
}

public class SolicitaDados extends AsyncTask<String, Void, String> {

private String parametros;

private SolicitaDados(String paramentros) {
this.parametros = paramentros;
Log.e("script", paramentros);
}

@Override
protected String doInBackground(String... urls) {
return Conexao.postDados(urls[0], this.parametros);
}

@Override
protected void onPostExecute(String resultado) {
if (resultado != null) {
Log.e("res", resultado);
if(resultado.contains("ok")){
Toast.makeText(editar_perfil.this, "Senha alterada com sucesso!", Toast.LENGTH_SHORT).show();
finish();

}else{
stateAll(true, senha, new_senha, ok);
showProgress(false, login_progress, editar_perfil.this);
Toast.makeText(editar_perfil.this, "Senha antiga incorreta, tente novamente!", Toast.LENGTH_SHORT).show();
}
}else{
stateAll(true, senha, new_senha, ok);
showProgress(false, login_progress, editar_perfil.this);
Toast.makeText(editar_perfil.this, "Erro inesperado, tente novamente!", Toast.LENGTH_SHORT).show();
}
}
}

@Override
protected void onDestroy() {
Log.e("onDestroy", "here");
startService(new Intent(this, ServiceOff.class));
super.onDestroy();
}
}

最佳答案

来自 onDestroy文档:

Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

可能您的 Activity 仍然存在,这就是没有调用 onDestroy 的原因。

我不知道完整的代码,所以很难说出你想要实现什么,也许 onPause() 或 onStop() 可以解决问题。

关于android - OnDestroy 中的调用方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51961379/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com