让两个线程在后台继续 运行
Making two threads to continue running in background
我正在编写一个涉及一个 AudioTrack 和一个 AudioRecord 的 android 应用程序。它们 运行 在两个不同的线程中,它们都需要继续 运行 并且在低 memory/CPU 时不被杀死,即使应用程序在后台 运行ning 也是如此。我对此进行了搜索,我知道我可能会使用 AsyncTask(就像在音乐流应用程序中一样)。但是,我不确定该怎么做,因为首先创建 运行s AudioTrack 的线程(一旦按下主 activity 上的某个按钮),然后该线程将创建另一个线程运行s AudioRecord(通过创建一个 Runnable)。
I searched about this and I know I could probably use AsyncTask (just like in music streaming app)
没有。你的问题不是线程,而是进程。
both of them need to continue to run and not being killed when low memory/CPU even if the app is running in the background
严格来说,那是不可能的。您将获得的最接近的方法是使用 Service
,后者又使用 startForeground()
来指示它具有前景优先级。这将最大限度地减少 Android 由于内存不足条件而终止您的进程的可能性,但不会消除它。
另请注意,您需要 CPU 开机才能执行音频任务。反过来,这将需要 WakeLock
。这将严重损害用户的电池,以至于您应该建议用户将设备保持在充电器上。这在未来尤为重要,即将发布的 "M" Android 版本的 "Doze mode" 将阻止您使用 WakeLocks
如果设备处于空闲状态、不动且未移动在充电器上。
我正在编写一个涉及一个 AudioTrack 和一个 AudioRecord 的 android 应用程序。它们 运行 在两个不同的线程中,它们都需要继续 运行 并且在低 memory/CPU 时不被杀死,即使应用程序在后台 运行ning 也是如此。我对此进行了搜索,我知道我可能会使用 AsyncTask(就像在音乐流应用程序中一样)。但是,我不确定该怎么做,因为首先创建 运行s AudioTrack 的线程(一旦按下主 activity 上的某个按钮),然后该线程将创建另一个线程运行s AudioRecord(通过创建一个 Runnable)。
I searched about this and I know I could probably use AsyncTask (just like in music streaming app)
没有。你的问题不是线程,而是进程。
both of them need to continue to run and not being killed when low memory/CPU even if the app is running in the background
严格来说,那是不可能的。您将获得的最接近的方法是使用 Service
,后者又使用 startForeground()
来指示它具有前景优先级。这将最大限度地减少 Android 由于内存不足条件而终止您的进程的可能性,但不会消除它。
另请注意,您需要 CPU 开机才能执行音频任务。反过来,这将需要 WakeLock
。这将严重损害用户的电池,以至于您应该建议用户将设备保持在充电器上。这在未来尤为重要,即将发布的 "M" Android 版本的 "Doze mode" 将阻止您使用 WakeLocks
如果设备处于空闲状态、不动且未移动在充电器上。