├── .idea
├── .name
├── vcs.xml
├── AndroidProjectSystem.xml
├── migrations.xml
├── deploymentTargetSelector.xml
├── gradle.xml
├── misc.xml
├── jarRepositories.xml
├── runConfigurations.xml
├── compiler.xml
└── codeStyles
│ └── Project.xml
├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── dimens.xml
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── menu
│ │ │ └── menu_main.xml
│ │ ├── layout
│ │ │ ├── content_main.xml
│ │ │ ├── fragment_second.xml
│ │ │ ├── fragment_first.xml
│ │ │ └── activity_main.xml
│ │ ├── navigation
│ │ │ └── nav_graph.xml
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ └── drawable
│ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── richzjc
│ │ └── net
│ │ ├── FirstFragment.java
│ │ ├── SecondFragment.java
│ │ └── MainActivity.java
├── proguard-rules.pro
└── build.gradle
├── network
├── consumer-rules.pro
├── .gitignore
├── src
│ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── richzjc
│ │ │ └── network
│ │ │ ├── SubscribeInfoIndex.java
│ │ │ ├── ConstKt.java
│ │ │ ├── SubscribeInfo.java
│ │ │ ├── SubscribeMethod.java
│ │ │ ├── NetBroadCastReceiver.java
│ │ │ ├── SimpleSubscribeInfo.java
│ │ │ ├── NetworkUtils.java
│ │ │ └── NetManager.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── netCompiler
├── .gitignore
├── src
│ └── main
│ │ └── java
│ │ └── com
│ │ └── richzjc
│ │ └── netcompiler
│ │ ├── Const.java
│ │ ├── util
│ │ └── EmptyUtils.java
│ │ └── NetCompiler.java
└── build.gradle
├── netannotation
├── .gitignore
├── src
│ └── main
│ │ └── java
│ │ └── com
│ │ └── richzjc
│ │ └── netannotation
│ │ ├── nettype
│ │ └── NetType.java
│ │ ├── NetLose.java
│ │ ├── NetAvailable.java
│ │ └── NetChange.java
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── settings.gradle
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/.idea/.name:
--------------------------------------------------------------------------------
1 | net
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/network/consumer-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/netCompiler/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/network/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/netannotation/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name='net'
2 | include ':app'
3 | include ':netannotation'
4 | include ':netCompiler'
5 | include ':network'
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/richzjc/NetworkListener/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/netannotation/src/main/java/com/richzjc/netannotation/nettype/NetType.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.netannotation.nettype;
2 |
3 | public enum NetType {
4 | AUTO, WIFI, MOBILE, NONE;
5 | }
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/SubscribeInfoIndex.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | import java.util.Map;
4 |
5 | public interface SubscribeInfoIndex {
6 | Map getSubscriberInfo();
7 | }
8 |
--------------------------------------------------------------------------------
/.idea/AndroidProjectSystem.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
7 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/ConstKt.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | public class ConstKt {
4 | public static final String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
5 | public static final String LOG_TAG = "net >>>";
6 | }
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
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
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Jan 02 15:23:37 CST 2025
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/.idea/migrations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/SubscribeInfo.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | import java.util.List;
4 |
5 | public interface SubscribeInfo {
6 | List availabeMethods();
7 | List loseMethods();
8 | List changeMethods();
9 | }
10 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/deploymentTargetSelector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/netannotation/src/main/java/com/richzjc/netannotation/NetLose.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.netannotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.CLASS)
9 | @Target(ElementType.METHOD)
10 | public @interface NetLose {
11 | }
12 |
--------------------------------------------------------------------------------
/netannotation/src/main/java/com/richzjc/netannotation/NetAvailable.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.netannotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.CLASS)
9 | @Target(ElementType.METHOD)
10 | public @interface NetAvailable {
11 | }
12 |
--------------------------------------------------------------------------------
/network/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/SubscribeMethod.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | import com.richzjc.netannotation.nettype.NetType;
4 |
5 | public class SubscribeMethod {
6 | public String getMethodName() {
7 | return methodName;
8 | }
9 |
10 | public NetType getNetType() {
11 | return netType;
12 | }
13 |
14 | private String methodName;
15 | private NetType netType;
16 |
17 | public SubscribeMethod(String methodName, NetType type){
18 | this.methodName = methodName;
19 | this.netType = type;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/netCompiler/src/main/java/com/richzjc/netcompiler/Const.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.netcompiler;
2 |
3 | public class Const {
4 | public static final String SUBSCRIBE_INFO_INDEX = "com.richzjc.network.SubscribeInfoIndex";
5 | public static final String OVERRIDE_ANNOTATION = "java.lang.Override";
6 | public static final String NETTYPE_PATH = "com.richzjc.netannotation.nettype.NetType";
7 | public static final String SIMPLE_SUBSCRIBE_INFO = "com.richzjc.network.SimpleSubscribeInfo";
8 | public static final String SUBSCRIBE_METHOD_PATH = "com.richzjc.network.SubscribeMethod";
9 | }
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | net
3 | Settings
4 |
5 | First Fragment
6 | Second Fragment
7 | Next
8 | Previous
9 |
10 | Hello first fragment
11 | Hello second fragment. Arg: %1$s
12 |
13 |
--------------------------------------------------------------------------------
/netCompiler/src/main/java/com/richzjc/netcompiler/util/EmptyUtils.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.netcompiler.util;
2 |
3 | import java.util.Collection;
4 | import java.util.Map;
5 |
6 | /**
7 | * 字符串、集合判空工具
8 | */
9 | public final class EmptyUtils {
10 |
11 |
12 | public static boolean isEmpty(CharSequence cs) {
13 | return cs == null || cs.length() == 0;
14 | }
15 |
16 | public static boolean isEmpty(Collection> coll) {
17 | return coll == null || coll.isEmpty();
18 | }
19 |
20 | public static boolean isEmpty(final Map, ?> map) {
21 | return map == null || map.isEmpty();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/netannotation/src/main/java/com/richzjc/netannotation/NetChange.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.netannotation;
2 |
3 | import com.richzjc.netannotation.nettype.NetType;
4 |
5 | import java.lang.annotation.Documented;
6 | import java.lang.annotation.ElementType;
7 | import java.lang.annotation.Retention;
8 | import java.lang.annotation.RetentionPolicy;
9 | import java.lang.annotation.Target;
10 |
11 |
12 |
13 | @Retention(RetentionPolicy.CLASS)
14 | @Target(ElementType.METHOD)
15 | public @interface NetChange {
16 | /**
17 | * 描述 netType 属性的作用
18 | *
19 | * @return 返回网络类型枚举值(如 4G/5G/WiFi)
20 | */
21 | NetType netType();
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/network/proguard-rules.pro:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
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 | -keep @androidx.annotation.Keep class * { *; }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
19 |
20 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/NetBroadCastReceiver.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.util.Log;
7 |
8 | public class NetBroadCastReceiver extends BroadcastReceiver {
9 |
10 | @Override
11 | public void onReceive(Context context, Intent intent) {
12 | if (intent == null || intent.getAction() == null) {
13 | Log.e(ConstKt.LOG_TAG, "异常了");
14 | return;
15 | }
16 |
17 | // 处理广播事件
18 | if (intent.getAction().equalsIgnoreCase(ConstKt.ANDROID_NET_CHANGE_ACTION)) {
19 | Log.e(ConstKt.LOG_TAG, "网络发生改变");
20 | if (NetworkUtils.isNetworkAvailable(context)) {
21 | Log.e(ConstKt.LOG_TAG, "网络连接成功");
22 | } else {
23 | Log.e(ConstKt.LOG_TAG, "没有网络连接");
24 | }
25 | NetManager.notifyAllChange();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/SimpleSubscribeInfo.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | import java.util.List;
4 |
5 | public class SimpleSubscribeInfo implements SubscribeInfo{
6 |
7 | private List availableMethods;
8 | private List loseMethods;
9 | private List changeMethods;
10 |
11 | public SimpleSubscribeInfo(List availableMethods, List loseMethods, List changeMethods){
12 | this.availableMethods = availableMethods;
13 | this.loseMethods = loseMethods;
14 | this.changeMethods = changeMethods;
15 | }
16 |
17 | @Override
18 | public List availabeMethods() {
19 | return availableMethods;
20 | }
21 |
22 | @Override
23 | public List loseMethods() {
24 | return loseMethods;
25 | }
26 |
27 | @Override
28 | public List changeMethods() {
29 | return changeMethods;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | 1.8
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/navigation/nav_graph.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
17 |
18 |
23 |
24 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/richzjc/net/FirstFragment.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.net;
2 |
3 | import android.os.Bundle;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 |
8 | import androidx.annotation.NonNull;
9 | import androidx.fragment.app.Fragment;
10 | import androidx.navigation.fragment.NavHostFragment;
11 |
12 | public class FirstFragment extends Fragment {
13 |
14 | @Override
15 | public View onCreateView(
16 | LayoutInflater inflater, ViewGroup container,
17 | Bundle savedInstanceState
18 | ) {
19 | // Inflate the layout for this fragment
20 | return inflater.inflate(R.layout.fragment_first, container, false);
21 | }
22 |
23 | public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
24 | super.onViewCreated(view, savedInstanceState);
25 |
26 | view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
27 | @Override
28 | public void onClick(View view) {
29 | NavHostFragment.findNavController(FirstFragment.this)
30 | .navigate(R.id.action_FirstFragment_to_SecondFragment);
31 | }
32 | });
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/richzjc/net/SecondFragment.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.net;
2 |
3 | import android.os.Bundle;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 |
8 | import androidx.annotation.NonNull;
9 | import androidx.fragment.app.Fragment;
10 | import androidx.navigation.fragment.NavHostFragment;
11 |
12 | public class SecondFragment extends Fragment {
13 |
14 | @Override
15 | public View onCreateView(
16 | LayoutInflater inflater, ViewGroup container,
17 | Bundle savedInstanceState
18 | ) {
19 | // Inflate the layout for this fragment
20 | return inflater.inflate(R.layout.fragment_second, container, false);
21 | }
22 |
23 | public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
24 | super.onViewCreated(view, savedInstanceState);
25 |
26 | view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
27 | @Override
28 | public void onClick(View view) {
29 | NavHostFragment.findNavController(SecondFragment.this)
30 | .navigate(R.id.action_SecondFragment_to_FirstFragment);
31 | }
32 | });
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_second.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_first.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
18 |
19 |
28 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | kotlin.code.style=official
21 |
22 | android.nonTransitiveRClass=true
23 |
24 | MEDUSA_KEYSTORE_PASS=130109
25 | MEDUSA_ALIAS_PASS=130109
26 | MEDUSA_ALIAS_NAME=net
27 |
28 |
29 | ANDROID_MIN_VERSION=26
30 | ANDROID_TARGET_VERSION=35
31 |
32 | COMPILE_SDK_VERSION=35
33 | BUILD_TOOL_VERSION=35.0.0
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
20 |
21 |
22 |
23 |
24 |
25 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/NetworkUtils.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | import android.app.Activity;
4 | import android.content.ComponentName;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.net.ConnectivityManager;
8 | import android.net.NetworkInfo;
9 |
10 | import com.richzjc.netannotation.NetChange;
11 | import com.richzjc.netannotation.nettype.NetType;
12 |
13 | public class NetworkUtils {
14 |
15 | public static boolean isNetworkAvailable(Context context) {
16 | ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
17 | if (connMgr == null) return false;
18 | // 返回所有网络信息
19 | NetworkInfo[] info = connMgr.getAllNetworkInfo();
20 | if (info != null) {
21 | for (NetworkInfo anInfo : info) {
22 | if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
23 | return true;
24 | }
25 | }
26 | }
27 | return false;
28 | }
29 |
30 | public static NetType getNetWorkType(Context context) {
31 | ConnectivityManager connMgr = (ConnectivityManager) context
32 | .getSystemService(Context.CONNECTIVITY_SERVICE);
33 | if (connMgr == null) return NetType.NONE;
34 | // 获取当前激活的网络连接信息
35 | NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
36 | if (networkInfo == null) {
37 | return NetType.NONE;
38 | }
39 | int nType = networkInfo.getType();
40 |
41 | if (nType == ConnectivityManager.TYPE_MOBILE) {
42 | return NetType.MOBILE;
43 | } else if (nType == ConnectivityManager.TYPE_WIFI) {
44 | return NetType.WIFI;
45 | }
46 | return NetType.NONE;
47 | }
48 |
49 | public static void openSetting(Context context, int requestCode) {
50 | Intent intent = new Intent("/");
51 | ComponentName cm = new ComponentName("com.android.settings",
52 | "com.android.settings.WirelessSettings");
53 | intent.setComponent(cm);
54 | intent.setAction("android.intent.action.VIEW");
55 | ((Activity) context).startActivityForResult(intent, requestCode);
56 | }
57 |
58 | }
59 |
60 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/.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 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/netCompiler/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java-library'
2 |
3 |
4 | dependencies {
5 | implementation fileTree(dir: 'libs', include: ['*.jar'])
6 | compileOnly 'com.google.auto.service:auto-service:1.0-rc6'
7 | annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6'
8 | implementation 'com.squareup:javapoet:1.12.1'
9 | implementation 'io.github.richzjc:netannotation:1.0.0'
10 | }
11 |
12 | // 生成源码 JAR
13 | task sourcesJar(type: Jar) {
14 | from sourceSets.main.allJava
15 | archiveClassifier = "sources" // ✅ 替换为 archiveClassifier
16 | }
17 |
18 |
19 | // 生成 JavaDoc JAR
20 | task javadocJar(type: Jar, dependsOn: javadoc) {
21 | from javadoc.destinationDir
22 | archiveClassifier = "javadoc" // ✅ 替换为 archiveClassifier
23 | }
24 |
25 | apply plugin: 'maven-publish'
26 |
27 | publishing {
28 | publications {
29 | // 创建一个 Maven 发布任务
30 | release(MavenPublication) {
31 | // 设置 groupId, artifactId, 版本等
32 | groupId = 'io.github.richzjc'
33 | artifactId = 'netCompiler'
34 | version = '1.0.0'
35 | // 指定发布 AAR 文件
36 | afterEvaluate {
37 | // 发布 AAR 文件
38 | artifact("$buildDir/libs/${project.name}.jar")
39 | }
40 | artifact sourcesJar // 包含源码
41 | artifact javadocJar // 包含文
42 |
43 | // 配置缺失的元数据
44 | pom {
45 | name = "netCompiler" // 项目名称
46 | description = "基于apt的注解处理器" // 项目描述
47 | url = "git@github.com:richzjc/NetworkListener.git" // 项目URL
48 |
49 | // 开发者信息
50 | developers {
51 | developer {
52 | id = "richzjc"
53 | name = "richzjc"
54 | email = "richzjc130109@gmail.com"
55 | }
56 | }
57 |
58 | // 开源协议
59 | licenses {
60 | license {
61 | name = "The Apache License, Version 2.0"
62 | url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
63 | }
64 | }
65 |
66 | // SCM(版本控制地址)
67 | scm {
68 | connection = "scm:git:git@github.com:richzjc/NetworkListener.git"
69 | developerConnection = "scm:git:ssh://github.com:richzjc/NetworkListener.git"
70 | url = "https://github.com/richzjc/NetworkListener.git"
71 | }
72 | }
73 | }
74 | }
75 |
76 | repositories {
77 | maven { url "file://${rootDir}/repo" }
78 | }
79 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 |
4 | android {
5 | compileSdk ANDROID_TARGET_VERSION as int
6 | namespace 'com.richzjc.net'
7 | defaultConfig {
8 | applicationId "com.richzjc.net"
9 | minSdk ANDROID_MIN_VERSION as int
10 | targetSdk ANDROID_TARGET_VERSION as int
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 |
16 | javaCompileOptions {
17 | annotationProcessorOptions {
18 | arguments = [ moduleName : project.getName() ]
19 | }
20 | }
21 | }
22 |
23 | signingConfigs {
24 | debug {
25 | storeFile file("../net.jks")
26 | storePassword MEDUSA_KEYSTORE_PASS
27 | keyAlias MEDUSA_ALIAS_NAME
28 | keyPassword MEDUSA_ALIAS_PASS
29 | v2SigningEnabled true
30 | }
31 |
32 | release {
33 | storeFile file("../net.jks")
34 | storePassword MEDUSA_KEYSTORE_PASS
35 | keyAlias MEDUSA_ALIAS_NAME
36 | keyPassword MEDUSA_ALIAS_PASS
37 | v2SigningEnabled true
38 | }
39 | }
40 |
41 | buildTypes {
42 | release {
43 | debuggable false
44 | signingConfig signingConfigs.release
45 | //是否混淆
46 | minifyEnabled true
47 | //disZipalign优化
48 | zipAlignEnabled true
49 | // 移除无用的resource文件
50 | shrinkResources true
51 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
52 | }
53 |
54 | debug {
55 | jniDebuggable false
56 | versionNameSuffix "-debug"
57 | signingConfig signingConfigs.debug
58 | //是否混淆
59 | minifyEnabled false
60 | //Zipalign优化
61 | zipAlignEnabled false
62 | // 移除无用的resource文件
63 | shrinkResources false
64 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
65 | }
66 | }
67 |
68 | compileOptions {
69 | sourceCompatibility JavaVersion.VERSION_17
70 | targetCompatibility JavaVersion.VERSION_17
71 | }
72 | }
73 |
74 | dependencies {
75 | implementation fileTree(dir: 'libs', include: ['*.jar'])
76 |
77 | implementation 'androidx.appcompat:appcompat:1.1.0'
78 | implementation 'com.google.android.material:material:1.1.0'
79 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
80 | implementation 'androidx.navigation:navigation-fragment:2.2.1'
81 | implementation 'androidx.navigation:navigation-ui:2.2.1'
82 | // annotationProcessor 'com.richzjc:netChange_compiler:1.0.8'
83 | // implementation 'com.richzjc:network_change:1.0.5'
84 | }
85 |
--------------------------------------------------------------------------------
/netannotation/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java-library'
2 |
3 |
4 |
5 | dependencies {
6 | implementation fileTree(dir: 'libs', include: ['*.jar'])
7 | }
8 | tasks.withType(JavaCompile) {
9 | options.compilerArgs += ["-Xlint:none", "-nowarn"]
10 | }
11 | // 生成源码 JAR
12 | task sourcesJar(type: Jar) {
13 | from sourceSets.main.allJava
14 | archiveClassifier = "sources" // ✅ 替换为 archiveClassifier
15 | }
16 |
17 |
18 | // 生成 JavaDoc JAR
19 | task javadocJar(type: Jar, dependsOn: javadoc) {
20 | from javadoc.destinationDir
21 | archiveClassifier = "javadoc" // ✅ 替换为 archiveClassifier
22 | }
23 |
24 | apply plugin: 'maven-publish'
25 |
26 | publishing {
27 | publications {
28 | // 创建一个 Maven 发布任务
29 | release(MavenPublication) {
30 | // 设置 groupId, artifactId, 版本等
31 | groupId = 'io.github.richzjc'
32 | artifactId = 'netannotation'
33 | version = '1.0.0'
34 | // 指定发布 AAR 文件
35 | afterEvaluate {
36 | // 发布 AAR 文件
37 | artifact("$buildDir/libs/${project.name}.jar")
38 | }
39 | artifact sourcesJar // 包含源码
40 | artifact javadocJar // 包含文
41 |
42 | // 配置缺失的元数据
43 | pom {
44 | name = "netannotation" // 项目名称
45 | description = "运用编译时 注解生成代码,所用到的注解" // 项目描述
46 | url = "git@github.com:richzjc/NetworkListener.git" // 项目URL
47 |
48 | // 开发者信息
49 | developers {
50 | developer {
51 | id = "richzjc"
52 | name = "richzjc"
53 | email = "richzjc130109@gmail.com"
54 | }
55 | }
56 |
57 | // 开源协议
58 | licenses {
59 | license {
60 | name = "The Apache License, Version 2.0"
61 | url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
62 | }
63 | }
64 |
65 | // SCM(版本控制地址)
66 | scm {
67 | connection = "scm:git:git@github.com:richzjc/NetworkListener.git"
68 | developerConnection = "scm:git:ssh://github.com:richzjc/NetworkListener.git"
69 | url = "https://github.com/richzjc/NetworkListener.git"
70 | }
71 | }
72 | }
73 | }
74 |
75 | repositories {
76 |
77 | maven { url "file://${rootDir}/repo" }
78 |
79 | // // 配置私有 Maven 仓库的 URL 和身份验证信息
80 | // maven {
81 | // url = uri('https://maven.wscn.net/repository/mobile-third/')
82 | // credentials {
83 | // username = properties.getProperty("userName")
84 | // password = properties.getProperty("password")
85 | // }
86 | // }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/app/src/main/java/com/richzjc/net/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.net;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.google.android.material.floatingactionbutton.FloatingActionButton;
6 | import com.google.android.material.snackbar.Snackbar;
7 | import com.richzjc.app.NetChanger;
8 | import com.richzjc.netannotation.NetAvailable;
9 | import com.richzjc.netannotation.NetChange;
10 | import com.richzjc.netannotation.NetLose;
11 | import com.richzjc.netannotation.nettype.NetType;
12 | import com.richzjc.network.NetManager;
13 |
14 | import androidx.annotation.Keep;
15 | import androidx.appcompat.app.AppCompatActivity;
16 | import androidx.appcompat.widget.Toolbar;
17 |
18 | import android.view.View;
19 | import android.view.Menu;
20 | import android.view.MenuItem;
21 | import android.widget.Toast;
22 |
23 | @Keep
24 | public class MainActivity extends AppCompatActivity {
25 |
26 | @NetLose
27 | public void test(){
28 | Toast.makeText(this, "没有网络了", Toast.LENGTH_LONG).show();
29 | }
30 |
31 | @NetAvailable
32 | public void test1(){
33 | Toast.makeText(this, "连接上网络了", Toast.LENGTH_LONG).show();
34 | }
35 |
36 | @NetChange(netType = NetType.AUTO)
37 | public void test2(){
38 | Toast.makeText(this, "有网络了Auto", Toast.LENGTH_LONG).show();
39 | }
40 |
41 | @NetChange(netType = NetType.WIFI)
42 | public void test3(){
43 | Toast.makeText(this, "有网络了Wife", Toast.LENGTH_LONG).show();
44 | }
45 |
46 | @Override
47 | protected void onCreate(Bundle savedInstanceState) {
48 | super.onCreate(savedInstanceState);
49 | NetManager.addIndex(new NetChanger());
50 | NetManager.init(this);
51 | NetManager.bind(this);
52 | setContentView(R.layout.activity_main);
53 | Toolbar toolbar = findViewById(R.id.toolbar);
54 | setSupportActionBar(toolbar);
55 |
56 | FloatingActionButton fab = findViewById(R.id.fab);
57 | fab.setOnClickListener(new View.OnClickListener() {
58 | @Override
59 | public void onClick(View view) {
60 | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
61 | .setAction("Action", null).show();
62 | }
63 | });
64 | }
65 |
66 | @Override
67 | public boolean onCreateOptionsMenu(Menu menu) {
68 | // Inflate the menu; this adds items to the action bar if it is present.
69 | getMenuInflater().inflate(R.menu.menu_main, menu);
70 | return true;
71 | }
72 |
73 | @Override
74 | public boolean onOptionsItemSelected(MenuItem item) {
75 | // Handle action bar item clicks here. The action bar will
76 | // automatically handle clicks on the Home/Up button, so long
77 | // as you specify a parent activity in AndroidManifest.xml.
78 | int id = item.getItemId();
79 |
80 | //noinspection SimplifiableIfStatement
81 | if (id == R.id.action_settings) {
82 | return true;
83 | }
84 |
85 | return super.onOptionsItemSelected(item);
86 | }
87 |
88 | @Override
89 | protected void onDestroy() {
90 | super.onDestroy();
91 | NetManager.unBind(this);
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/network/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdk ANDROID_TARGET_VERSION as int
5 | namespace 'com.richzjc.network'
6 | defaultConfig {
7 | minSdk ANDROID_MIN_VERSION as int
8 | targetSdk ANDROID_TARGET_VERSION as int
9 | versionCode 1
10 | versionName "1.0"
11 | }
12 |
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | compileOptions {
20 | sourceCompatibility JavaVersion.VERSION_17
21 | targetCompatibility JavaVersion.VERSION_17
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 | implementation 'androidx.appcompat:appcompat:1.1.0'
28 | api 'io.github.richzjc:netannotation:1.0.0'
29 | }
30 |
31 |
32 |
33 | // 生成源码 JAR
34 | task sourcesJar(type: Jar) {
35 | from android.sourceSets.main.java.srcDirs
36 | archiveClassifier = "sources" // ✅ 替换为 archiveClassifier
37 | }
38 |
39 |
40 | // 生成源码 JAR
41 | task javadocJar(type: Jar) {
42 | from android.sourceSets.main.java.srcDirs
43 | archiveClassifier = "javadoc" // ✅ 替换为 archiveClassifier
44 | }
45 |
46 |
47 | apply plugin: 'maven-publish'
48 |
49 | publishing {
50 | publications {
51 | // 创建一个 Maven 发布任务
52 | release(MavenPublication) {
53 | // 设置 groupId, artifactId, 版本等
54 | groupId = 'io.github.richzjc'
55 | artifactId = 'network'
56 | version = '1.0.0'
57 | // 指定发布 AAR 文件
58 | afterEvaluate {
59 | // 发布 AAR 文件
60 | artifact("$buildDir/outputs/aar/${project.name}-release.aar")
61 | }
62 | artifact sourcesJar // 包含源码
63 | artifact javadocJar // 包含文
64 |
65 | // 配置缺失的元数据
66 | pom {
67 | name = "network" // 项目名称
68 | description = "基于apt的注解处理器" // 项目描述
69 | url = "git@github.com:richzjc/NetworkListener.git" // 项目URL
70 |
71 | // 开发者信息
72 | developers {
73 | developer {
74 | id = "richzjc"
75 | name = "richzjc"
76 | email = "richzjc130109@gmail.com"
77 | }
78 | }
79 |
80 | // 开源协议
81 | licenses {
82 | license {
83 | name = "The Apache License, Version 2.0"
84 | url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
85 | }
86 | }
87 |
88 | // SCM(版本控制地址)
89 | scm {
90 | connection = "scm:git:git@github.com:richzjc/NetworkListener.git"
91 | developerConnection = "scm:git:ssh://github.com:richzjc/NetworkListener.git"
92 | url = "https://github.com/richzjc/NetworkListener.git"
93 | }
94 | }
95 | }
96 | }
97 |
98 | repositories {
99 | maven { url "file://${rootDir}/repo" }
100 | }
101 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### maven central的地址
2 | https://central.sonatype.com/search?q=io.github.richzjc
3 | ---
4 | ### 仓库
5 | mavenCentral()
6 | ---
7 | ### 依赖方式
8 | ```
9 | implementation 'io.github.richzjc:netannotation:1.0.0'
10 | implementation 'io.github.richzjc:network:1.0.0'
11 | kapt 'io.github.richzjc:netCompiler:1.0.0'
12 | ```
13 | ---
14 | ### 博客地址
15 | https://zhuanlan.zhihu.com/p/1894702829630895262
16 |
17 | ---
18 | ### 优点
19 | 该库简化了写网络监听的相关代码。真正的实现了变向切面的编程!!
20 | 只需要通过一个注解就能实现相应的网络变化
21 |
22 | ---
23 | ### 注解说明
24 |
25 | ```
26 | @NetAvailable
27 | 该注解表示,监听到有网络了,如果想在监听到有网络时执行相应操作,则只需要给方法添加此注解
28 |
29 | @NetLose
30 | 该注解表示,监听到无网络了,如果想在监听到无网络时执行相应操作,则只需要给方法添加此注解
31 |
32 | @NetChange(netType = NetType.AUTO)
33 | 该注解表示,监听到网络切换了,如果想在监听到网络切换时执行相应操作,则只需要给方法添加此注解
34 |
35 | ```
36 | ---
37 | ### NetType有四种取值
38 |
39 | ```
40 | public enum NetType {
41 | AUTO, WIFI, MOBILE, NONE;
42 | }
43 | ```
44 |
45 | **AUTO** : 如果传入的参数是AUTO; 就相当于NetAvailable注解,监听到有网络
46 | **NONE** : 如果传入的参数是NONE; 就相当于NetLOSE注解,监听到无网络
47 |
48 | ---
49 | ### 示例代码
50 | ```
51 | @NetLose
52 | public void test(){
53 | //执行无网络的操作
54 | }
55 |
56 | @NetAvailable
57 | public void testl(){
58 | //执行有网络的操作
59 | }
60 |
61 | @NetChange(netType =NetType.WIFI){
62 | //网络变成WIFI后,执行相应的操作
63 | }
64 | ```
65 |
66 | ---
67 | ### 实现原理
68 |
69 | 基于APT注解处理器, 结合JavaPoet, 在编译时,动态生成类文件,可以下载工程自己跑一遍。
70 |
71 | ---
72 | ### 展示一下apt生成的类的源码
73 | ```
74 | public class NetChanger implements SubscribeInfoIndex {
75 | private static final Map SUBSCRIBER_INDEX;
76 |
77 | static {
78 | SUBSCRIBER_INDEX = new HashMap();
79 | List availableList;
80 | List loseList;
81 | List changeList;
82 | availableList = new ArrayList();
83 | loseList = new ArrayList();
84 | changeList = new ArrayList();
85 | availableList.add(new SubscribeMethod("test1", null));
86 | loseList.add(new SubscribeMethod("test", null));
87 | changeList.add(new SubscribeMethod("test2", NetType.AUTO));
88 | changeList.add(new SubscribeMethod("test3", NetType.WIFI));
89 | SUBSCRIBER_INDEX.put(MainActivity.class, new SimpleSubscribeInfo(availableList, loseList, changeList));
90 | }
91 |
92 | @Override
93 | public Map getSubscriberInfo() {
94 | return SUBSCRIBER_INDEX;
95 | }
96 | }
97 | ```
98 |
99 | ---
100 | ### 配置
101 |
102 | 需要在使用到注解的module下面的build.gradle文件中,添加如下配置:
103 | ```
104 | javaCompileOptions {
105 | annotationProcessorOptions {
106 | arguments = [ moduleName : project.getName() ]
107 | }
108 | }
109 | ```
110 | ---
111 | ### 初始化
112 |
113 | 将各个模块下面生成的NetChanner类添加到NetManager中:
114 | ```
115 | NetManager.addIndex(new NetChanger());
116 | NetManager.init(this);
117 | ```
118 |
119 | ---
120 | ### 注册与取消注册
121 |
122 | 在使用到注解的对应类里面,调用注册方法与取消注册方法,比如说在Activity里面:
123 | ```
124 | @Override
125 | protected void onCreate(Bundle savedInstanceState) {
126 | super.onCreate(savedInstanceState);
127 | NetManager.bind(this);
128 | }
129 |
130 |
131 | @Override
132 | protected void onDestroy() {
133 | super.onDestroy();
134 | NetManager.unBind(this);
135 | }
136 | ```
137 |
138 | ---
139 | ### 添加混淆规则
140 |
141 | ```
142 | -keepclasseswithmembernames class * {
143 | @com.richzjc.netannotation.* ;
144 | }
145 | ```
146 |
147 | 若写得不对, 欢迎大家共同讨论!!!!
148 |
149 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/network/src/main/java/com/richzjc/network/NetManager.java:
--------------------------------------------------------------------------------
1 | package com.richzjc.network;
2 |
3 | import android.content.Context;
4 | import android.content.IntentFilter;
5 |
6 | import com.richzjc.netannotation.nettype.NetType;
7 |
8 | import java.lang.reflect.Method;
9 | import java.util.HashMap;
10 | import java.util.Map;
11 |
12 | import static com.richzjc.network.ConstKt.ANDROID_NET_CHANGE_ACTION;
13 |
14 | public class NetManager {
15 |
16 | private static HashMap