├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── virtue │ │ │ │ └── socketdemo │ │ │ │ ├── bean │ │ │ │ └── TestBean.java │ │ │ │ ├── utils │ │ │ │ └── RequestUtil.java │ │ │ │ ├── service │ │ │ │ └── MySocketService.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── virtue │ │ │ └── socketdemo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── virtue │ │ └── socketdemo │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── socketlibrary ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ └── values │ │ │ │ └── strings.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── virtue │ │ │ │ └── socketlibrary │ │ │ │ ├── type │ │ │ │ ├── ParseMode.java │ │ │ │ ├── ReceiveType.java │ │ │ │ └── BroadCastType.java │ │ │ │ ├── utils │ │ │ │ ├── ResponseListener.java │ │ │ │ ├── SocketCode.java │ │ │ │ ├── OnReceiveListener.java │ │ │ │ ├── SendBroadCastUtil.java │ │ │ │ └── ChangeTypeUtil.java │ │ │ │ └── manager │ │ │ │ └── Socketer.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── virtue │ │ │ └── socketlibrary │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── virtue │ │ └── socketlibrary │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── .idea ├── copyright │ └── profiles_settings.xml ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── compiler.xml └── misc.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── gradlew └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /socketlibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':socketlibrary' 2 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidSocket 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zvirtuey/AndroidSocket/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /socketlibrary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SocketLibrary 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zvirtuey/AndroidSocket/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zvirtuey/AndroidSocket/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zvirtuey/AndroidSocket/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zvirtuey/AndroidSocket/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zvirtuey/AndroidSocket/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/type/ParseMode.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.type; 2 | 3 | /** 4 | * Created by Virtue on 2017/10/23. 5 | */ 6 | 7 | public enum ParseMode { 8 | AUTO_PARSE,//自动解析(Auto Parse) 9 | MANUALLY_PARSE //手动解析(Manually Parse) 10 | } 11 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/type/ReceiveType.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.type; 2 | 3 | /** 4 | * Created by virtue on 2017/6/19. 5 | */ 6 | 7 | public enum ReceiveType { 8 | SEPARATION_SIGN //分隔符(Separator) 9 | , FIXED_LENGTH //固定长度(Fixed length) 10 | } 11 | -------------------------------------------------------------------------------- /socketlibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/utils/ResponseListener.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.utils; 2 | 3 | /** 4 | * Created by virtue on 2017/6/10. 5 | */ 6 | 7 | public interface ResponseListener { 8 | 9 | //response data 10 | void onSuccess(String data); 11 | 12 | //failed code 13 | void onFail(int failCode); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #88ff0000 7 | #e30d00ff 8 | #ec29b10b 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/utils/SocketCode.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.utils; 2 | 3 | public interface SocketCode { 4 | int SUCCESS = 0; //发送成功(send succeed) 5 | int SEND_FAIL = 1; //发送失败 (send fail) 6 | int TIME_OUT = 2; //发送超时 (overtime) 7 | int SEND_TO_LONG = 3; //发送数据过长 (send so long) 8 | int DISCONNECT = -1; //连接断开 (disconnected) 9 | 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/virtue/socketdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /socketlibrary/src/test/java/com/virtue/socketlibrary/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/utils/OnReceiveListener.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.utils; 2 | 3 | import com.virtue.socketlibrary.manager.Socketer; 4 | 5 | /** 6 | * Created by Virtue on 2017/10/24. 7 | */ 8 | 9 | public interface OnReceiveListener { 10 | //socket connection successful 连接成功 11 | void onConnected(Socketer socketer); 12 | 13 | //socket connection failed 连接失败 14 | void onDisconnected(Socketer socketer); 15 | 16 | //socket data response 数据响应 17 | void onResponse(String data); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/type/BroadCastType.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.type; 2 | 3 | /** 4 | * Created by virtue on 2017/6/10. 5 | */ 6 | 7 | public interface BroadCastType { 8 | String IS_CONNECTED="is_connected"; //网络连接携带参数标记 9 | String NETWORK_CONNECT_STATE="network_connect_state"; //发送网络连接的广播Action 10 | String SERVER_NOTICE_DATA ="server_data"; //服务器主推数据参数标记 11 | String SERVER_NOTICE="server_notice"; //发送服务器主推的广播Action 12 | String RESPONSE_RESULT_DATA ="response_result_data"; //请求响应的数据参数标记 13 | // String RESPONSE_RESULT_LISTENER ="response_result_listener"; //请求结果数据对应的监听器参数标记 14 | // String RESPONSE_RESULT="server_result"; //发送请求获得响应的广播Action 15 | } 16 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Users\virtue\Android\Android_Studio_Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /socketlibrary/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Users\virtue\Android\Android_Studio_Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/virtue/socketdemo/bean/TestBean.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketdemo.bean; 2 | 3 | import java.io.Serializable; 4 | 5 | public class TestBean implements Serializable{ 6 | 7 | 8 | public String protver; 9 | public int pkgtype; 10 | public int command; 11 | public int seq; 12 | public int terminaltype = 0; 13 | public String terminal = ""; 14 | public int result = 0; 15 | public int bodylen = 200; 16 | public Inform body=new Inform() ; 17 | 18 | public class Inform implements Serializable { 19 | public String Phone ; 20 | public int cmd; 21 | public String getPhone() { 22 | return Phone; 23 | } 24 | public void setPhone(String phone) { 25 | Phone = phone; 26 | } 27 | public int getCmd() { 28 | return cmd; 29 | } 30 | public void setCmd(int cmd) { 31 | this.cmd = cmd; 32 | } 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/virtue/socketdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.virtue.androidsocket", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /socketlibrary/src/androidTest/java/com/virtue/socketlibrary/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.virtue.socketlibrary.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/utils/SendBroadCastUtil.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.utils; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | 6 | import com.virtue.socketlibrary.type.BroadCastType; 7 | 8 | public class SendBroadCastUtil { 9 | 10 | /** 11 | * 发送网络状态广播 (send network Network status) 12 | * 13 | * @param mContext 14 | * @param isConnected 15 | */ 16 | public static void sendNetworkStateBroadcast(Context mContext, boolean isConnected) { 17 | Intent intent = new Intent(); 18 | intent.putExtra(BroadCastType.IS_CONNECTED, isConnected); 19 | intent.setAction(BroadCastType.NETWORK_CONNECT_STATE); 20 | mContext.sendBroadcast(intent); 21 | } 22 | 23 | /** 24 | * 发送服务器主推通知信息 (send message from server) 25 | * 26 | * @param mContext 27 | * @param mData 28 | */ 29 | public static void sendServerData(Context mContext, String mData) { 30 | Intent intent = new Intent(); 31 | intent.putExtra(BroadCastType.SERVER_NOTICE_DATA, mData); 32 | intent.setAction(BroadCastType.SERVER_NOTICE); 33 | mContext.sendBroadcast(intent); 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.virtue.androidsocket" 8 | minSdkVersion 15 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | lintOptions { 21 | checkReleaseBuilds false 22 | abortOnError false 23 | } 24 | } 25 | 26 | dependencies { 27 | compile fileTree(include: ['*.jar'], dir: 'libs') 28 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 29 | exclude group: 'com.android.support', module: 'support-annotations' 30 | }) 31 | compile 'com.android.support:appcompat-v7:24.2.1' 32 | testCompile 'junit:junit:4.12' 33 | compile 'com.github.bumptech.glide:glide:3.7.0' 34 | compile project(':socketlibrary') 35 | compile 'com.google.code.gson:gson:2.8.1' 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /socketlibrary/src/main/java/com/virtue/socketlibrary/utils/ChangeTypeUtil.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketlibrary.utils; 2 | 3 | public class ChangeTypeUtil { 4 | 5 | /** 6 | * 将int值转成网络字节 7 | * 8 | * @return 返回网络字节数组 9 | */ 10 | public static byte[] intToByteArray(int a) { 11 | return new byte[]{(byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), 12 | (byte) (a & 0xFF)}; 13 | } 14 | 15 | /** 16 | * 将byte[]转成int 17 | * 18 | * @param b 19 | * @return 20 | */ 21 | public static int byteArrayToInt(byte[] b) { 22 | // return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] 23 | // & 0xFF) << 24; 24 | return (b[0] & 0xFF) << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | b[3] & 0xFF; 25 | 26 | } 27 | 28 | // 将指定byte数组以16进制的形式打印到控制台 29 | public static void printHexString(byte[] b) { 30 | for (int i = 0; i < b.length; i++) { 31 | String hex = Integer.toHexString(b[i] & 0xFF); 32 | if (hex.length() == 1) { 33 | hex = '0' + hex; 34 | } 35 | String ret = ""; 36 | System.out.println(hex.toUpperCase() + " "); 37 | } 38 | 39 | } 40 | 41 | // public static byte[] charArrayToByteArray(char[] cChar){ 42 | // byte[] byteData=Encoding.Default.GetBytes(cChar); 43 | // return byteData; 44 | // } 45 | // 46 | // public static char[] byteArrayToCharArray(byte[] byteData){ 47 | // char[] cChar=Encoding.ASCII.GetChars(byteData); 48 | // return cChar; 49 | // } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/virtue/socketdemo/utils/RequestUtil.java: -------------------------------------------------------------------------------- 1 | package com.virtue.socketdemo.utils; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | import java.util.regex.PatternSyntaxException; 6 | 7 | import android.content.Context; 8 | import android.net.ConnectivityManager; 9 | import android.net.NetworkInfo; 10 | import android.telephony.TelephonyManager; 11 | 12 | public class RequestUtil { 13 | private static int requestID = 100; 14 | 15 | /** 16 | * 获取手机IMEI 17 | * 18 | * @param context 19 | * @return 20 | */ 21 | public static String getPhoneMini(Context context) { 22 | String imei = ""; 23 | TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 24 | imei = telephonyManager.getDeviceId();// 手机IMEI 25 | if (imei == null || imei.length() < 1) { 26 | imei = ""; 27 | } 28 | return imei; 29 | } 30 | 31 | /** 32 | * 检查该字符串是否只包含字母和数字 33 | * @param str 34 | * @return 35 | * @throws PatternSyntaxException 36 | */ 37 | public static boolean StringFilter(String str) throws PatternSyntaxException { 38 | // 只允许字母和数字 39 | String regEx = "[a-zA-Z0-9]{6,16}"; 40 | Pattern p = Pattern.compile(regEx); 41 | Matcher m = p.matcher(str); 42 | return m.matches(); 43 | } 44 | 45 | /** 46 | * 检查当前网络是否可用 47 | * 48 | * @param context 49 | * @return 50 | */ 51 | public static boolean isNetworkAvailable(Context context) 52 | { 53 | ConnectivityManager connectivityManager = (ConnectivityManager) context 54 | .getSystemService(Context.CONNECTIVITY_SERVICE); 55 | 56 | if (connectivityManager == null) 57 | { 58 | return false; 59 | } 60 | else 61 | { 62 | // 获取NetworkInfo对象 63 | NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo(); 64 | if (networkInfo != null && networkInfo.length > 0) 65 | { 66 | for (int i = 0; i < networkInfo.length; i++) 67 | { 68 | // 判断当前网络状态是否为连接状态 69 | if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) 70 | { 71 | return true; 72 | } 73 | } 74 | } 75 | } 76 | // 网络不可以用 77 | return false; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 21 | 22 | 27 | 28 | 33 | 34 |