Android 在文件上运行的后台进程在删除该文件后仍在运行
因此,我在Android中有一个流程如下:Android 在文件上运行的后台进程在删除该文件后仍在运行,android,service,process,ffmpeg,runtime.exec,Android,Service,Process,Ffmpeg,Runtime.exec,因此,我在Android中有一个流程如下: process = Runtime.getRuntime().exec(commandString); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { Log.d("ShutDownHook Called"); process.destroy();
process = Runtime.getRuntime().exec(commandString);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
Log.d("ShutDownHook Called");
process.destroy();
}
}));
其中,命令字符串包含文件路径和一些其他参数。
这是一个CPU密集的进程,可能需要长达数小时的时间(电影转码)。该文件是ffmpeg静态文件。问题是,在某些情况下,即使我关闭了应用程序,进程仍会留在后台。当我用任务管理器关闭应用程序时,我的一部手机就会出现这种情况。也就是说,不会调用onDestroy()方法,也不会调用应用程序类中的onTerminate(),也不会调用上面的shutdown钩子
我还创建了一个带有通知的后台服务,因此当退出应用程序时,该服务应该留在后台。即使使用这种控制,当我使用任务管理器终止应用程序时,服务也会重新启动,我会丢失对ffmpeg类、异步任务、进程等的所有引用,并且我无法停止进程,因为我有空指针。
无论如何,我可以确保该服务不会被篡改的应用程序杀死,所以我可以杀死进程与通知栏从我的服务
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
Log.d("HelperActivity", "onServiceDisconnected");
mIsBound = false;
mServer = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("HelperActivity", "onServiceConnected");
mIsBound = true;
NotificationService.LocalBinder mLocalBinder = (NotificationService.LocalBinder)service;
mServer = mLocalBinder.getServerInstance();
//mServer.test();
}
};
//This is the onCreate of the application class
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this,
NotificationService.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Log.d("onCreate", "Called");
singleton = this;
}
我一直在使用ffmpeg支持。当然,添加了新功能,但运行ffmpeg命令的基本概念是相同的
另一件奇怪的事情是,当我启动应用程序时,我删除了上一个文件,但进程仍在后台运行,因为我的转码性能减半。每次启动应用程序时,都会将文件从资产复制到内部存储器
File ffmpegFile = new File(FileUtils.getFFmpeg(context));
if (ffmpegFile.exists()) {
Log.d("File exists!");
ffmpegFile.delete();
}
我检查过文件被删除了,但是我的CPU仍然被大量使用。
很抱歉在发帖过程中出现任何错误,这是我的第一个错误