实现一个不停发包的Android应用(类似于电脑上的Ping命令)
2023-09-11 14:20:30 时间
代码如下:
package com.example.ping; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.support.v7.app.ActionBarActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends ActionBarActivity { TextView info=null; int i=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.info = (TextView) findViewById(R.id.info); RunHandler(); } public void startPing(String ip) { try { Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 " + ip,null,null); int status = p.waitFor(); InputStream input = p.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(input)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } if (status == 0) { i++; info.setText("Ping www.baidu.com 进行中... " + i); } else { info.setText("Fail: IP addr not reachable"); } } catch (IOException e) { info.setText("Fail: IOException"+e.getMessage()); } catch (InterruptedException e) { info.setText("Fail: InterruptedException"+e.getMessage()); } } @SuppressLint("HandlerLeak") private void RunHandler(){ // 实时更新UI final Handler mHandler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); startPing("www.baidu.com"); } }; Runnable mRunnable = new Runnable() { public void run() { while (true) { try { Thread.sleep(1000); mHandler.sendMessage(mHandler.obtainMessage()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; new Thread(mRunnable).start(); } @Override 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; } @Override 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); } }
配置文件AndroidManifest.xml中的配置如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ping" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="20" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:screenOrientation="portrait" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
相关文章
- Android Studio “懒人”必备插件android layout id converter
- Android 异常 android.os.NetworkOnMainThreadException
- android R 文件 丢失的处理 如何重新生成
- Android USB 开发详解
- android常见错误-The container 'Android Dependencies' references non existing library
- Android studio的错误:radle sync failed: Cause: failed to find target android-21 :
- 《Android平板电脑开发秘籍》——3.1节技巧:理解菜单和ActionBar之间的区别
- 《Android平板电脑开发秘籍》——3.9节技巧:创建一个下拉列表式的ActionBar
- Android---- 获取当前应用的版本号和当前android系统的版本号
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
- Android 代码混淆
- android -- 蓝牙 bluetooth (五)接电话与听音乐
- Android API 中文(76)——AdapterView.OnItemLongClickListener
- 【VLC-Android】vlc-android简例
- Android Fragment详解(四):管理Fragment
- Android图片缩放方法
- 阅读android中frameworks层代码的几种方式
- Android Studio升级或更新项目,经常造访的“Could not find com.android.tools.build :gradle: x.x.x”的原因分析
- 价格战拉上了Android平板电脑
- 架构师成长之路-基于android fragment通信的面向对象的万能接口
- Android手机自动化测试工具有哪几种?
- 【Android开发经验】怎样查看android-support-v4支持包中的源代码
- Android学习笔记之:android更新ui的几种经常用法
- Android ListView 之 自定义Adapter 实现列表条目功能,以及 OnItemClickListener(点击事件) OnScrollListener(滑动事件)事件监听
- 【winows7+android-ndk-r9+Cygwin 】cocos2dx 2.*游戏移植Android平台完全手册
- 【Android】The application has stopped unexpectedly.Please try again.
- Android Studio 关联第三方模拟器_ADB命令配置和VT设置教程链接