If you want to ping in Android, it seems that you can only make the underlying call in case of a non-root machine.
Because the root permission is required to send ICMP packets in Java.
The process can only be solved by creating a process, which in Java has two ways:
1. Call Processbuilder's constructor to execute start ()
2. Execute using the Runtime.getruntime (). Exec () method
After use, it is found that the two are different but not very large, two examples illustrate:
1. Execute start () after calling Processbuilder's constructor:
Process process = new Processbuilder ("/system/bin/ping"). Redirecterrorstream (True). Start (); OutputStream stdout = Process.getoutputstream (); InputStream stdin = Process.getinputstream (); BufferedReader reader = new BufferedReader (new InputStreamReader (stdin)); BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (stdout));
2. Execute using the Runtime.getruntime (). Exec () method:
Process process = Runtime.getruntime (). EXEC ("/system/bin/ping"); OutputStream stdout = Process.getoutputstream (); I Nputstream stderr = Process.geterrorstream (); InputStream stdin = Process.getinputstream (); BufferedReader reader = new BufferedReader (new InputStreamReader (stdin)); BufferedReader err= New BufferedReader (New InputStreamReader (stderr)); BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (stdout));
There is no difference in the efficiency of the two, perhaps I did not find. The difference between the two Tests is whether the stream of errors can be redirected.
With Processbuilder, the error output stream can be transferred to the standard output stream by Redirecterrorstream (true), so that all outputs of the process can be read out using a single process.getinputstreamreader ().
When using the Runtime.getruntime (). Exec () method, the wrong output stream is also obtained through Process.geterrorstream ().
Share a class that is destroyed after the execution of a process of its own collection:
Import Java.io.inputstream;import Java.io.outputstream;public class ProcessModel {/** * process shutdown via Android bottom-up * * @param process */public static void killprocess (process process) {int pid = GETPROCESSID (process.tostr ing ()); if (pid! = 0) {try {android.os.Process.killProcess (PID); } catch (Exception e) {try {Process.destroy (); } catch (Exception ex) {}}}/** * Gets the ID of the current process * * @param str * @ return */public static int getprocessid (String str) {try {int i = str.indexof ("=") + 1; Int J = str.indexof ("]"); String cStr = Str.substring (i, J). Trim (); Return Integer.parseint (CSTR); } catch (Exception e) {return 0; }}/** * Close all flows of the process * * @param process */public static void Closeallstream (process process) { try {InputStream in = Process.getinputstream (); if (in = null) In.close (); } catch (Exception e) {e.printstacktrace (); try {InputStream in = Process.geterrorstream (); if (in = null) In.close (); } catch (Exception e) {e.printstacktrace (); } try {OutputStream out = Process.getoutputstream (); if (out! = null) out.close (); } catch (Exception e) {e.printstacktrace (); }}/** * destroys a process * * @param process */public static void Processdestroy (process process) { if (process! = null) {try {if (Process.exitvalue ()! = 0) {Closeallstream (process); KillProcess (process); }} catch (Illegalthreadstateexception e) {closeallstream (process); KillProcess (process); }}}/** * asynchronously destroyed by thread * * @param process */public static void Asyncprocessdestroy (final Process process) {thread thread = new Thread (new Runnable () {@Override public void run () { Processdestroy (process); } }); Thread.setdaemon (TRUE); Thread.Start (); }}
Strangely, when a large number of processes are created using threads, the process will not be created until a certain number (approximately 1000 or so) is reached;
I do not know how to solve this situation, I think is to get a thread pool inside put 20 created processes, and the external thread reuse and the process of creating, I do not know if this is feasible?
We hope to discuss the solution, thank you.