Ale*_*lex 0 android android-activity
我正在开发一个无线电流应用程序和一个主屏幕小部件.问题是当我杀死应用程序时,我想修改主屏幕小部件中的图像,如下所示:
@Override
protected void onDestroy() {
//update widget
WidgetIntentReceiver.updateWidgetFromActivityDestroy(getApplicationContext());
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
System.out.println("End onDestroy -> stop stream");
super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)
但是并不总是调用修改窗口小部件的代码(不显示"System.out.println").我已经读过onDestroy并不总是被调用.但我没有找到另一种解决方案?
谢谢.
is when I kill the application.这里的关键字是kill.通过杀死你的意思是强制关闭应用程序,据我所知.如果是这样,那么你无法改变一些东西.因为当您强制关闭应用程序或者即使操作系统因某些原因(内存限制,电池优化等)而终止您的应用程序时 - 也无法执行某些操作,因为整个应用程序进程(使用您的程序包名称)被简单地杀死通过Process.killProcess (int pid)没有任何回调的东西.onDestroy()在这样的时刻,行为是完全不确定的.它写在文档中:
[..] There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the PROCESS GOES AWAY.
所以不要忘记onDestroy()它是一种与Activity's生命周期紧密相关的方法,而不是整个应用程序生命周期.作为一种解决方法,你应该创建一些Base Activity并在其中OnPause()执行你想要的东西,然后让你的应用程序中的所有其他活动继承它.
其他(不应在生产代码中使用的不良解决方案)是创建一个背景Service,它将循环检查您的应用程序是否正在运行,actvityManager.getRunningAppProcesses();如果没有,则修改小部件.
| 归档时间: |
|
| 查看次数: |
478 次 |
| 最近记录: |