Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以停止JNI呼叫吗_Java_Android_C_Java Native Interface - Fatal编程技术网

Java 可以停止JNI呼叫吗

Java 可以停止JNI呼叫吗,java,android,c,java-native-interface,Java,Android,C,Java Native Interface,我开发了一个c库。我使用JNI从JAVA代码调用库的c函数: 通过以下方式从java调用JNI函数: public class UnboundedService extends Service { static { System.loadLibrary("hellondk"); } private native String invokeNativeFunction(); private class JniBackGround extends

我开发了一个c库。我使用JNI从JAVA代码调用库的c函数:

通过以下方式从java调用JNI函数:

public class UnboundedService extends Service {

    static {
        System.loadLibrary("hellondk");
    }
    private native String invokeNativeFunction();

    private class JniBackGround extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... ss) {
            final String str = invokeNativeFunction();          
            return str;
        }
    }
    ...
    @Override
    public void onStart(Intent intent, int startId) {
        JniBackGround task = new JniBackGround();
        task.execute(new String[] { "http://www.any.com" });
    }
}

现在我的C函数需要很长时间才能完成执行。我想添加一个按钮来停止执行。我如何用JAVA阻止它?

如果fx存在某种inf循环,而它是真的,那么他可以在这个新的InvokEnableActivestopFunction中始终使用whilesomeFlag并将someFlag设置为false。。。若他正在使用一些我怀疑的curl库调用,因为url并没有传递给native,他可以在那个里取消curl请求。。。诸如此类。。。
–Selvin

您需要从单击listener的按钮调用invokeNativeStopFunction。。。它将具有与本机类似的主体:{…}或仅调用AsyncTask。cancel@Selvin闻起来像是答案…@Selvin我试过task.canceltrue但它不会停止C函数的执行task@MaartenBodewes-owlstead这是一个有点讽刺意味的评论,他没有向我们展示本机代码中invokeNativeFunction内部的情况。。。但是如果fx存在某种inf循环,而它是真的,那么他可以在这个新的invokenativestopffunction中始终使用whilesomeFlag并将someFlag设置为false。。。若他正在使用一些我怀疑的curl库调用,因为url并没有传递给native,他可以在那个里取消curl请求。。。等等等等…@Selvin你最后的评论很好,帮助我找到了解决办法。非常感谢。请你把它作为答案,我会接受的
public class UnboundedService extends Service {

    static {
        System.loadLibrary("hellondk");
    }
    private native String invokeNativeFunction();

    private class JniBackGround extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... ss) {
            final String str = invokeNativeFunction();          
            return str;
        }
    }
    ...
    @Override
    public void onStart(Intent intent, int startId) {
        JniBackGround task = new JniBackGround();
        task.execute(new String[] { "http://www.any.com" });
    }
}