PING!PING!PING!连接世界!

突发奇想,想看看安卓上能不能实现ping命令,一百度,已经有现成的程序了,但是没有代码。没代码就自己写咯,想想还是挺激动的呢。

         废话不多说,上图:


PING!PING!PING!连接世界!PING!PING!PING!连接世界!
界面文件代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center|top"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.Jackhcunyang.pingtest.MainActivity" >

    <EditText
        android:id="@+id/editText"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonname" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"/>

</LinearLayout>

 我把ping命令的主要部分放在按钮监听器类里了,所以主Activity里并没有什么:下面是主Activity代码:

package com.Jackhcunyang.pingtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView t;
	EditText e;
	public static String ACTIVITY_TAG = "PingTest";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏

		setContentView(R.layout.activity_main);
		TextView t = (TextView) this.findViewById(R.id.textView);
		this.t = t;
		Button b = (Button) this.findViewById(R.id.button);
		EditText e = (EditText) this.findViewById(R.id.editText);
		this.e = e;
		ButtonListener bl = new ButtonListener(this);
		b.setOnClickListener(bl);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

而后就是监听器代码了:

package com.Jackhcunyang.pingtest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

/**
 * @author 春阳
 * 
 */
public class ButtonListener implements android.view.View.OnClickListener {
	private MainActivity m;

	private static Runtime run = Runtime.getRuntime();
	private static Process proc = null;
	private static String ss=null;
	Handler  mHandler = new Handler();
	public ButtonListener(MainActivity m) {
		this.m = m;
	}

	public void onClick(View v) {
		mHandler = new Handler() {
			   @Override
			   public void handleMessage(Message msg)
			   {
			    super.handleMessage(msg);
			    m.t.setText(ss);
			    m.t.invalidate();
			   }
			  };
		new Thread(new Runnable() {
			public void run() {
				BufferedReader br = null;
				String s = m.e.getText().toString();// 得到输入框的内容
				try {
					proc = run.exec("ping -c 4 -w 200 " + s);
					Log.i(MainActivity.ACTIVITY_TAG,"ping -c 4 -w 200 " + s);// 写入日志
					int status = proc.waitFor();
					System.out.println(status);
					proc.getInputStream();
					if (status == 0) {
						s = "连接成功";
						Log.i(MainActivity.ACTIVITY_TAG,s);
					} else {
						s = "连接失败";
						Log.i(MainActivity.ACTIVITY_TAG,s);
					}
					br = new BufferedReader(new InputStreamReader(
							proc.getInputStream()));
					String line = null;
					StringBuilder sb = new StringBuilder();
					while ((line = br.readLine()) != null) {
						sb.append(line + "\n");
					}
					System.out.println(sb.toString());
					ss=s + "\r\n" + sb.toString();
					Log.i(MainActivity.ACTIVITY_TAG,ss);//日志
					mHandler.sendMessage(mHandler.obtainMessage());//传递信息让handler执行
				} catch (IOException | InterruptedException e) {
					System.out.println("IOException|InterruptedException");
				}
			}
		}).start();
		
	}
}

 
 
 

相关推荐