【问题标题】:Sockets between Android app and Java application on the same phone同一部手机上的 Android 应用程序和 Java 应用程序之间的套接字
【发布时间】:2016-09-26 08:39:58
【问题描述】:

此问题与:https://stackoverflow.com/questions/39661583/android-linux-deploy-access-phone-sensors

我有一个根深蒂固的三星 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 部分编写一个简单的应用程序来做到这一点。然后,我将有一个在 ubuntu 中运行的简单客户端 java 程序,该程序连接到 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 程序给出了连接被拒绝的异常。

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

【问题讨论】:

  • 你怎么能确定客户端的ip,192.168.0.10?我会尝试,127.0.0.1 和 localhost
  • 是的,我先尝试了这些,但连接被拒绝。所以我在我的路由器上查找了手机使用的 IP 并尝试了,但仍然连接被拒绝错误。
  • 服务器工作正常吗?
  • 我想是的..我按下按钮时没有任何异常
  • 这并不意味着您的服务器代码在您尝试连接客户端时正在运行。

标签: java android linux sockets ubuntu


【解决方案1】:

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

【讨论】:

  • 你能举个例子吗?我试过:public void go(View view){ Intent intent = new Intent(this, HelloIntentService.class); startService(intent); try { TimeUnit.MINUTES.sleep(2); stopService(intent); } catch(Exception ex){ stopService(intent); } } with ..
  • public class HelloIntentService extends IntentService { public HelloIntentService() { super("HelloIntentService"); } @Override protected void onHandleIntent(Intent intent) {try{ ServerSocket serverSock = new ServerSocket(4242);while(true){Socket sock = serverSock.accept();PrintWriter writer = new PrintWriter(sock.getOutputStream());String advice ="message"; writer.println(advice); writer.close(); } } catch (IOException ex){ ex.printStackTrace(); } } }
  • 你能连接到 chroot 环境中的任何东西吗?
猜你喜欢
  • 2015-05-29
  • 1970-01-01
  • 2012-06-13
  • 2022-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多