gpt4 book ai didi

java - 同一部手机上 Android 应用程序和 Java 应用程序之间的套接字

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:17 25 4
gpt4 key购买 nike

这个问题是关于:https://stackoverflow.com/questions/39661583/android-linux-deploy-access-phone-sensors

我有一个 root 的三星 galaxy s3,我已经使用 linux deploy https://play.google.com/store/apps/details?id=ru.meefik.linuxdeploy&hl=en_GB 在手机上安装了 ubuntu。 .我知道这意味着 ubuntu 在“chrooted”(https://en.wikipedia.org/wiki/Chroot)环境中运行,无法直接访问手机 android 部分中的文件。

我想尝试在两个操作系统之间建立通信,以便我可以从访问手机功能的 ubuntu 操作系统调用方法。

我想我可以通过在充当套接字服务器的 android 部分上编写一个简单的应用程序来做到这一点。然后我会有一个简单的客户端 java 程序,它在 ubuntu 中运行,连接到 android 应用程序上的那个套接字。

这是我目前所拥有的:

android MainActivity.java 文件:

package com.example.sam.myfirstapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void go(View view){
try{
ServerSocket serverSock = new ServerSocket(4242);

while(true){
Socket sock = serverSock.accept();
PrintWriter writer = new PrintWriter(sock.getOutputStream());
String advice ="This is my sent message";
writer.println(advice);
writer.close();

}
} catch (IOException ex){
ex.printStackTrace();
}
}
}

activity_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_start"
android:onClick="go"/>
</LinearLayout>

在ubuntu上运行的java文件是:

import java.io.*;
import java.net.*;

public class hello_world {

public void go() {
System.out.println("In go method");
try{
Socket s = new Socket("192.168.0.10", 4242);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
System.out.println(advice);
reader.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}



public static void main(String[] args){
System.out.println("hello, world!");

hello_world myhello = new hello_world();
myhello.go();
}
}

服务器和客户端似乎都可以编译。我在 android 中运行应用程序并单击按钮启动服务器。然后我转到 ubuntu 并运行 java 程序。

java程序给出连接被拒绝的异常。

操作系统之间的这种通信方法是否可行?如果是这样,出了什么问题,我该如何纠正?

最佳答案

尝试在 Service 中运行您的服务器.

关于java - 同一部手机上 Android 应用程序和 Java 应用程序之间的套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39698209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com