In iOS APP, each program runs in its own sandbox. Once the program is deleted, the application data will be cleared. Therefore, most programs that need to save data will use iCloud to back up the data. However, if it is an authoring APP, like notes, if you want to export to the computer, you must transfer it again, which is very troublesome. So many APPs have started to have built-in FTP servers. Once started, the computer only needs to access the data in the APP through the FTP client link.
In fact, there are many similar APPs in Android. In order to facilitate sharing application data in the APP with PC, FTP or WebDAV services will also run in the APP. But Android does not have the kind of sandboxing problem with iOS, although Android also has sandboxes. Usually most mobile phones do not obtain root permissions, and sensitive application data will be placed in the sandbox, that is, the APP internal data directory, located in /data/data//. This path can be obtained through (). If the mobile phone does not have root permissions, no one can access the data except the APP itself. However, Android can choose to store data in an external sandbox, that is, the external data directory of the APP, which can obtain the path through () and even other evil APPs can create folders in external storage...
There are many built-in APPs that exchange data with external server-side operation, such as reading more, Documents5, etc.
Most of the implementations are to start Socket to listen for a fixed port and then process HTTP requests, but for most APP coders, handling HTTP is a very troublesome thing. To handle header, processing POST and GET, processing file uploads and ordinary forms, etc., if you do not use third-party libraries, it is very difficult to write this function well.
There is AndroidAsync in third-party implementations. Although I haven't read too much source code, I guess this library is probably used.
However, it can also be used as a client method, and it is very simple to run as a listening service:
AsyncHttpServer server = new AsyncHttpServer(); ("/", new HttpServerRequestCallback() { @Override public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { ("Hello!!!"); } }); (5000);
For most students who have done WEB, they may think of IIS, Tomcat, Apache and other things when they mention server-side programs. However, IIS is based on the Windows platform, and IIS relies on the system driver level. It is impossible to transplant it, and it will never be ported in this life. Tomcat is a JavaEE container running on a JVM virtual machine. Although Android also uses the JAVA language, its virtual machine is ART (dalvik before 4.4), and Apache was developed in C/C++. It is still very promising to port to Android. Dear readers, you can search for related tutorials online, how to cross-compile Apache to ARM, and you can also be a stubborn player, many of which have been compiled.
Here I would like to talk about how to run httpd for arm on Android. You can first put the compiled httpd into the raw folder and determine whether it is in the specified location when MainActivity is started. If it does not, it will be released. I usually run it in a separate service, so that even if the activity is destroyed, the service will still run in the background, which is also a necessary feature of the server.
private File httpd; @Override public void onCreate() { (); httpd = new File(getFilesDir(), "httpd"); if (!()) { try { InputStream ins = getResources().openRawResource(); (httpd, ins); ().exec("chmod 777 " + ()); } catch (Exception e) { (TAG, "onCreate: ", e); } } }
There is a Runtime class in Android. This class is mainly used to allow Android applications to interact with the running environment where they are located. You can directly get an instance of this class by calling the static method of () and then call exec to execute the command. Next, I created a binary execution class and made a simple encapsulation of it.
public class BinExecuter { /** * Process PID */ private int pid; /** * Executable binary file path */ private String bin; /** * Startup parameters */ private String paras; /** * Process instance */ private Process process; /** * Get PID * @return */ public int getPid() { return pid; } /** * Constructor * @param bin Executable binary file path * @param paras startup parameters */ public BinExecuter(String bin, String paras) { = bin; = paras; } /** * Start the process */ public void start() { try { process = ().exec(bin + " " + paras); Field f = ().getDeclaredField("pid"); (true); pid = (process); (false); } catch (Exception ex) { (); } } /** * End the process */ public void stop() { if (pid > 0) { try { ().exec("kill -9 " + pid); } catch (Exception ex) { (); } } } }
But this is not enough. After the program like httpd is started, the console will output. For example, if a client requests a certain URL, or an error occurs, it will be displayed on the console. There is no console window on Android, so how to capture console output? It is simple and redirected to the input stream.
InputStream outs = (); InputStreamReader isrout = new InputStreamReader(outs); BufferedReader brout = new BufferedReader(isrout); String line; try { while ((line = ()) != null) { (line); } } catch (Exception ex) { (); }
Note that there is a big slander here. The main thread will be blocked. Just start another thread. Renovate this class and increase the monitoring of console output to make it slightly stronger.
/** author:yahch**/ public interface BinExecuteCallback { void onConsoleResponse(String text); } private BinExecuteCallback binExecuteCallback; public void setBinExecuteCallback(BinExecuteCallback binExecuteCallback) { = binExecuteCallback; }
The application methods in an Aria2 server I developed some time ago are as follows:
@Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { ariaConfig = (AriaConfig) ("config"); if (ariaConfig != null) { (TAG, ()); binExecuter = new BinExecuter((), ()); (new () { @Override public void onConsoleResponse(String text) { sendMessage(ARIA2_SERVICE_BIN_CONSOLE, text); } }); } } else { stopSelf(); } return (intent, flags, startId); } private void sendMessage(String name, String message) { MessageEvent genericEvent = new MessageEvent(name, message); ().post(genericEvent); }
Through EventBus, you can throw the console messages intercepted from the service into the Activity, and of course you can also use broadcasts. I think EventBus is more useful.
Now the GO language is also full of flowers. GO is born for the server and has a very powerful cross-platform capability. There are already many programs compiled on Github for ARM versions, such as frp, caddy, filebrowser, etc., which can be ported to Android. What we need to do is give it a shell, control it to run and stop, and configure some parameters.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.