我想在androidstudio中单击按钮时与python建立连接

2024-05-08 23:01:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着点击一个按钮来连接python 但它不运行,除了它只运行在主

这是运行连接的代码

public class link {

    public static void main(String a[]) {
        try {
            Process p = Runtime.getRuntime().exec(
                "python C:/Users/ASUS/Desktop/sendingCode/jaad/app/test.py");
            Scanner in = new Scanner(new InputStreamReader(
                p.getInputStream()));
            String line = "";
            while ((line = in.nextLine()) != null) {
                // display each output line form python script
                System.out.println(line);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

一旦我把以前的代码对按钮,它不会与我工作,我不知道为什么

recordBtn=(Button)findViewById(R.id.recordBtn);
recordBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            Process p = Runtime.getRuntime().exec(
                        "python C:/Users/ASUS/Desktop/sendingCode/jaad/app/test.py");
            Scanner in = new Scanner(new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = in.nextLine()) != null) {
                // display each output line form python script
                System.out.println(line);
            }
        } catch (Exception e) {
                System.out.println(e.getMessage());
        }
    }
});

Tags: 代码innewstringlinepublicoutprocess