Skip to content

Commit ac3ed71

Browse files
author
kongqw
committed
Initial commit
0 parents  commit ac3ed71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1193
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AndroidNetworkMonitor
2+
3+
> Android 全局网络变化监听

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 29
9+
buildToolsVersion "29.0.2"
10+
defaultConfig {
11+
applicationId "com.kongqw.networkmonitor"
12+
minSdkVersion 21
13+
targetSdkVersion 29
14+
versionCode 1
15+
versionName "1.0"
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
buildTypes {
19+
debug {
20+
// minifyEnabled false
21+
minifyEnabled true
22+
shrinkResources true
23+
zipAlignEnabled true
24+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25+
}
26+
release {
27+
minifyEnabled true
28+
shrinkResources true
29+
zipAlignEnabled true
30+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
31+
}
32+
}
33+
}
34+
35+
dependencies {
36+
implementation fileTree(dir: 'libs', include: ['*.jar'])
37+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
38+
implementation 'androidx.appcompat:appcompat:1.1.0'
39+
implementation 'androidx.core:core-ktx:1.2.0'
40+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
41+
testImplementation 'junit:junit:4.12'
42+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
43+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
44+
45+
implementation project(":monitor")
46+
47+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
48+
}

app/proguard-rules.pro

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
22+
23+
-keepattributes *Annotation*
24+
-keepclassmembers class * {
25+
@com.kongqw.network.monitor.interfaces.NetworkMonitor <methods>;
26+
}
27+
-keep class com.kongqw.network.monitor.** { *; }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.kongqw.networkmonitor
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.kongqw.networkmonitor", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.kongqw.networkmonitor">
4+
5+
<application
6+
android:name=".MyApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name=".MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
<action android:name="android.intent.action.VIEW" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.kongqw.networkmonitor
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.util.Log
6+
import android.widget.Toast
7+
import com.kongqw.network.monitor.NetworkMonitorManager
8+
import com.kongqw.network.monitor.enums.NetworkState
9+
import com.kongqw.network.monitor.interfaces.NetworkMonitor
10+
import com.kongqw.network.monitor.util.NetworkStateUtils
11+
import kotlinx.android.synthetic.main.activity_main.*
12+
13+
class MainActivity : AppCompatActivity() {
14+
15+
companion object {
16+
private val TAG = MainActivity::class.java.simpleName
17+
}
18+
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState)
21+
setContentView(R.layout.activity_main)
22+
23+
NetworkMonitorManager.getInstance().register(this)
24+
25+
btn_action_1?.setOnClickListener {
26+
val hasNetworkCapability = NetworkStateUtils.hasNetworkCapability(applicationContext)
27+
Toast.makeText(applicationContext, if (hasNetworkCapability) "当前有网络" else "当前无网络", Toast.LENGTH_SHORT).show()
28+
}
29+
btn_action_2?.setOnClickListener {
30+
val networkState = NetworkStateUtils.getNetworkState(applicationContext)
31+
Toast.makeText(applicationContext, "当前网络类型:$networkState", Toast.LENGTH_SHORT).show()
32+
}
33+
}
34+
35+
override fun onDestroy() {
36+
NetworkMonitorManager.getInstance().unregister(this)
37+
super.onDestroy()
38+
}
39+
40+
@NetworkMonitor
41+
fun onNetWorkStateChange(networkState: NetworkState) {
42+
Log.i(TAG, "onNetWorkStateChange networkState = $networkState")
43+
when (networkState) {
44+
NetworkState.NONE -> {
45+
Toast.makeText(applicationContext, "暂无网络", Toast.LENGTH_SHORT).show()
46+
tv_network_state?.text = "当前网络类型:暂无网络"
47+
}
48+
NetworkState.WIFI -> {
49+
Toast.makeText(applicationContext, "WIFI网络", Toast.LENGTH_SHORT).show()
50+
tv_network_state?.text = "当前网络类型:WIFI网络"
51+
}
52+
NetworkState.CELLULAR -> {
53+
Toast.makeText(applicationContext, "蜂窝网络", Toast.LENGTH_SHORT).show()
54+
tv_network_state?.text = "当前网络类型:蜂窝网络"
55+
}
56+
}
57+
}
58+
59+
@NetworkMonitor(monitorFilter = [NetworkState.WIFI])
60+
fun onNetWorkStateChangeWIFI(networkState: NetworkState) {
61+
Log.i(TAG, "onNetWorkStateChangeWIFI networkState = $networkState")
62+
}
63+
64+
@NetworkMonitor(monitorFilter = [NetworkState.CELLULAR])
65+
fun onNetWorkStateChangeCellular(networkState: NetworkState) {
66+
Log.i(TAG, "onNetWorkStateChangeCellular networkState = $networkState")
67+
}
68+
69+
@NetworkMonitor(monitorFilter = [NetworkState.NONE])
70+
fun onNetWorkStateChangeNONE(networkState: NetworkState) {
71+
Log.i(TAG, "onNetWorkStateChangeNONE networkState = $networkState")
72+
}
73+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.kongqw.networkmonitor
2+
3+
import android.app.Application
4+
import com.kongqw.network.monitor.NetworkMonitorManager
5+
6+
class MyApplication : Application() {
7+
8+
override fun onCreate() {
9+
super.onCreate()
10+
11+
NetworkMonitorManager.getInstance().init(this)
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeWidth="1"
11+
android:strokeColor="#00000000">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeWidth="1"
33+
android:strokeColor="#00000000" />
34+
</vector>

0 commit comments

Comments
 (0)