删除文件后,文件上运行的后台进程仍在运行

时间:2015-10-19 20:16:16

标签: 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();
        }
   }));

命令字符串包含文件路径和其他一些参数。 该过程是一个CPU繁重的过程,可能需要持续几个小时(电影转码)。该文件是一个ffmpeg静态文件。问题是在某些情况下即使我杀死了应用程序,该过程也会保留在后台。当我用任务管理器杀死应用程序时,我的一个手机就出现了这种情况。话虽如此,不会调用onDestroy()方法,也不会调用应用程序类中的onTerminate(),也不会调用上面的关闭钩子。

此外,我已经创建了一个带有通知的后台服务,因此当退出应用程序时,服务应该保留在后台。即使有这种控制,当我用任务管理器杀死应用程序时,服务重新启动,我丢失了对我的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;

}

我一直在使用http://hiteshsondhi88.github.io/ffmpeg-android-java/来支持ffmpeg。当然添加了新功能,但运行ffmpeg命令的基本概念是相同的。

另一件奇怪的事情是,当我启动应用程序时,我删除了以前的文件,并且该进程仍然在后台运行,导致我的转码性能减半。每次启动应用程序时,文件都会从资产复制到内部存储。

  File ffmpegFile = new File(FileUtils.getFFmpeg(context));
  if (ffmpegFile.exists()) {
        Log.d("File exists!");
        ffmpegFile.delete();
  }

我检查过该文件,但我的CPU仍然使用了很多。 对于帖子中的任何错误,对不起,这是我的第一个。

0 个答案:

没有答案
相关问题