我们的 Android 服务使用 HIDL 服务与我们的驱动程序/硬件交互,但在长时间运行期间,我们看到很多这样的打印“binder 线程池(1 个线程)饥饿 xxx 毫秒”,时间为 120ms < xxx > 310ms。一段时间后,我们看到从 HIDL 服务复制的内存是错误地址,当我们访问它时会给出 SIGSEGV。线程池饥饿和内存损坏是否相关?有人见过类似的问题吗?这是 Android O 预览版。
multithreading android android-source android-os-handler android-8.0-oreo
我需要退出我的应用程序,我已经引用了 Stack Overflow 和其他网站上的所有链接,我已经使用了,,finishAffinity()
仍然无法实现它。我正在重写主活动中的 onBackPressed 方法并终止应用程序。finish()
System.exit(0)
实际上,当我启动应用程序后按后退按钮时,它工作得很好。但是当我继续进行其他活动并返回 MainActivity 时,它不起作用。如果我使用finishAffinity()
它,则打开我的相机活动,这是我在项目中的活动之一。如果我使用finish()
它就是打开我的第二页活动。
我将发布我的代码以供参考。
MainActivity.Java
@Override
public void onBackPressed()
{
if (back_pressed + 2000 > System.currentTimeMillis())
{
Log.e("called", "back pressed");
finish();
}
else {
Toast.makeText(getBaseContext(), "Press twice to exit!", Toast.LENGTH_SHORT).show();
}
back_pressed = System.currentTimeMillis();
}
Run Code Online (Sandbox Code Playgroud) Android操作系统在内存不足时会终止进程.场景:Android杀死应用程序进程,我通过Android启动器或最近任务列表(长按主页按钮)重新打开它.我可以使用以下方法检查Android是否在最近查看的活动的onCreate()方法中杀死了我的应用程序进程:
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// Re-initialise things that killing the app would have destroyed
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果Android杀死应用程序进程并使用打包在PendingIntent中的Intent通过Notification重新打开它,我不知道如何确定应用程序进程是否被Android杀死.随后我不会重新初始化杀死应用程序进程会破坏的内容.
有没有办法确定Android是否在从通知中打开新活动时杀死了应用程序进程?
我找到了一个解决这个问题的hacky解决方案.使用:Android:在点击通知时始终启动顶级活动我可以在堆栈顶部打开活动,如果Android杀死应用程序进程并处理重新初始化,则会传递savedInstanceState.然后,每个活动负责使用原始通知意图中的额外内容将用户重定向到相应的活动.此方案的意图设置如下:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Run Code Online (Sandbox Code Playgroud)
我是否可以在Intent上设置一个Action,Category或Flag,它将模拟重新打开应用程序进程,就好像是由用户完成但是在新的Intent/Activity上?
编辑:澄清最后一个问题(虽然看起来我的婴儿对Android的理解让我失望所以它可能没有意义):我是否可以在Intent上设置Action,Category或Flag,就像上面的代码段一样,这将允许我确定应用程序进程是否已被操作系统杀死?
android android-intent android-notifications android-pendingintent android-os-handler
android ×3