diff --git a/.gitignore b/.gitignore index 87b0f57..831a3cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,11 @@ *.iml .gradle /local.properties -/.idea/caches -/.idea/libraries -/.idea/modules.xml -/.idea/workspace.xml -/.idea/navEditor.xml -/.idea/assetWizardSettings.xml +.idea .DS_Store /build /captures .externalNativeBuild - target/ pom.xml.tag pom.xml.releaseBackup diff --git a/AMap_Android_API_Location_Demo.zip b/AMap_Android_API_Location_Demo.zip new file mode 100644 index 0000000..376a17a Binary files /dev/null and b/AMap_Android_API_Location_Demo.zip differ diff --git a/ARouterDemo.zip b/ARouterDemo.zip new file mode 100644 index 0000000..f00cd5f Binary files /dev/null and b/ARouterDemo.zip differ diff --git a/ARouterDemo/.gitignore b/ARouterDemo/.gitignore new file mode 100644 index 0000000..603b140 --- /dev/null +++ b/ARouterDemo/.gitignore @@ -0,0 +1,14 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx diff --git a/ARouterDemo/app/.gitignore b/ARouterDemo/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/ARouterDemo/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/ARouterDemo/app/build.gradle b/ARouterDemo/app/build.gradle new file mode 100644 index 0000000..1dfae67 --- /dev/null +++ b/ARouterDemo/app/build.gradle @@ -0,0 +1,40 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.2" + defaultConfig { + applicationId "com.example.arouterdemo" + minSdkVersion 23 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + javaCompileOptions { + annotationProcessorOptions { + // 生成的文档路径 : build/generated/source/apt/(debug or release)/com/alibaba/android/arouter/docs/arouter-map-of-${moduleName}.json + arguments = [AROUTER_MODULE_NAME: project.getName(), AROUTER_GENERATE_DOC: "enable"] + } + } + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + + implementation 'com.alibaba:arouter-api:1.5.0' + annotationProcessor 'com.alibaba:arouter-compiler:1.2.2' + implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.62' + +} diff --git a/ARouterDemo/app/proguard-rules.pro b/ARouterDemo/app/proguard-rules.pro new file mode 100644 index 0000000..0c95a02 --- /dev/null +++ b/ARouterDemo/app/proguard-rules.pro @@ -0,0 +1,30 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile +-keep public class com.alibaba.android.arouter.routes.**{*;} +-keep public class com.alibaba.android.arouter.facade.**{*;} +-keep class * implements com.alibaba.android.arouter.facade.template.ISyringe{*;} + +# 如果使用了 byType 的方式获取 Service,需添加下面规则,保护接口 +-keep interface * implements com.alibaba.android.arouter.facade.template.IProvider + +# 如果使用了 单类注入,即不定义接口实现 IProvider,需添加下面规则,保护实现 +# -keep class * implements com.alibaba.android.arouter.facade.template.IProvider \ No newline at end of file diff --git a/ARouterDemo/app/src/androidTest/java/com/example/arouterdemo/ExampleInstrumentedTest.java b/ARouterDemo/app/src/androidTest/java/com/example/arouterdemo/ExampleInstrumentedTest.java new file mode 100644 index 0000000..7b0d061 --- /dev/null +++ b/ARouterDemo/app/src/androidTest/java/com/example/arouterdemo/ExampleInstrumentedTest.java @@ -0,0 +1,27 @@ +package com.example.arouterdemo; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + assertEquals("com.example.arouterdemo", appContext.getPackageName()); + } +} diff --git a/ARouterDemo/app/src/main/AndroidManifest.xml b/ARouterDemo/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..46a8d85 --- /dev/null +++ b/ARouterDemo/app/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/BaseActivity.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/BaseActivity.java new file mode 100644 index 0000000..ac29372 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/BaseActivity.java @@ -0,0 +1,20 @@ +package com.example.arouterdemo.activity; + +import android.annotation.SuppressLint; +import android.os.Bundle; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import com.alibaba.android.arouter.launcher.ARouter; + +@SuppressLint("Registered") +public class BaseActivity extends AppCompatActivity { + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // inject()方法配合 @Autowired 一起使用 + ARouter.getInstance().inject(this); + } +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/MainActivity.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/MainActivity.java new file mode 100644 index 0000000..a6b7342 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/MainActivity.java @@ -0,0 +1,242 @@ +package com.example.arouterdemo.activity; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; + +import androidx.core.app.ActivityOptionsCompat; +import androidx.fragment.app.Fragment; + +import com.alibaba.android.arouter.facade.Postcard; +import com.alibaba.android.arouter.facade.annotation.Autowired; +import com.alibaba.android.arouter.facade.annotation.Route; +import com.alibaba.android.arouter.facade.callback.NavCallback; +import com.alibaba.android.arouter.launcher.ARouter; +import com.example.arouterdemo.R; +import com.example.arouterdemo.bean.TestObj; +import com.example.arouterdemo.router.MyRouter; +import com.example.arouterdemo.service.SingleService; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +// 在支持路由的页面上添加注解(必选) +@Route(path = MyRouter.ACTIVITY_URL_MAIN) +public class MainActivity extends BaseActivity { + + private static Activity activity; + + public static Activity getThis() { + return activity; + } + + /**************** + * (推荐)使用依赖注入的方式发现服务, + * 通过注解标注字段,即可使用,无需主动获取 + * Autowired注解中标注name之后,将会使用byName的方式注入对应的字段, + * 不设置name属性,会默认使用byType的方式发现服务 + * (当同一接口有多个实现的时候,必须使用byName的方式发现服务) + ****************/ + @Autowired(name = "/yourservicegroupname/single") + SingleService singleService; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + activity = this; + + /**************** 带参数跳转 ****************/ + /**************** 传递自定义类要有JSONServiceImpl实现类 ****************/ + Button bt_with_arguments = findViewById(R.id.bt_with_arguments); + bt_with_arguments.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + List list = new ArrayList<>(); + list.add(new TestObj(8, "Jack")); + + Map map = new HashMap<>(); + map.put("9", new TestObj(9, "Nine")); + + // 跳转并携带参数 + ARouter.getInstance().build(MyRouter.ACTIVITY_URL_TARGET) + .withByte("key1", Byte.valueOf("1")) + .withShort("key2", Short.valueOf("2")) + .withInt("key3", 3) + .withLong("key4", 4L) + .withFloat("key5", 5.0F) + .withDouble("key6", 6.0D) + .withChar("key7", '7') + .withBoolean("key8", true) + .withString("key9", "9") + // 以下三个要想传递过去,必须存在com.example.arouterdemo.service.JsonServiceImpl实现类 + .withObject("key10", new TestObj(10, "Rose")) + .withObject("key11", list) + .withObject("key12", map) + .navigation(); + finish(); + } + }); + + /**************** 带动画跳转 ****************/ + /**************** 新旧跳转方式不同,不过,基本上已经没有 API < 16 的手机了 ****************/ + Button bt_with_animation = findViewById(R.id.bt_with_animation); + bt_with_animation.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // 新版动画 + if (Build.VERSION.SDK_INT >= 16) { + ActivityOptionsCompat compat = ActivityOptionsCompat. + makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0); + + ARouter.getInstance() + .build(MyRouter.ACTIVITY_URL_TARGET) + .withOptionsCompat(compat) + .navigation(); + finish(); + } + // 旧版动画 + else { + Toast.makeText(MainActivity.this, "API < 16,不支持新版本动画,", Toast.LENGTH_SHORT).show(); + ARouter.getInstance() + .build(MyRouter.ACTIVITY_URL_TARGET) + .withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom) + .navigation(MainActivity.this); + finish(); + } + } + }); + + /**************** 带拦截器跳转 ****************/ + /**************** 要有对应的拦截器拦截此次跳转 ****************/ + Button bt_with_interceptor = findViewById(R.id.bt_with_interceptor); + bt_with_interceptor.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ARouter.getInstance() + .build(MyRouter.ACTIVITY_URL_TARGET_INTERCEPTOR) + .navigation(MainActivity.this, new NavCallback() { + @Override + public void onArrival(Postcard postcard) { + + } + + @Override + public void onInterrupt(Postcard postcard) { + // 拦截器写在了 com.example.arouterdemo.interceptor.Test1Interceptor + Log.d("ARouter", "被拦截了"); + } + }); + } + }); + + /**************** 处理跳转结果 ****************/ + /**************** 目前没有 "/xxx/xxx" 这个路由,所以会触发跳转失败 ****************/ + Button bt_with_result = findViewById(R.id.bt_with_result); + bt_with_result.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ARouter.getInstance().build("/xxx/xxx").navigation(MainActivity.this, new NavCallback() { + @Override + public void onFound(Postcard postcard) { + Log.d("ARouter", "找到了"); + } + + @Override + public void onLost(Postcard postcard) { + Log.d("ARouter", "找不到了"); + // 这里的弹窗仅做举例,代码写法不具有可参考价值 + final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.getThis()); + alertDialog.setCancelable(true); + alertDialog.setTitle("温馨提醒"); + alertDialog.setMessage("页面没有找到,跳转失败!"); + alertDialog.setNeutralButton("跳不了,不跳了", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + return; + } + }); + alertDialog.create().show(); + } + + @Override + public void onArrival(Postcard postcard) { + Log.d("ARouter", "跳转完了"); + } + + @Override + public void onInterrupt(Postcard postcard) { + Log.d("ARouter", "被拦截了"); + } + }); + } + }); + + /**************** + * (推荐)使用依赖注入的方式发现服务, + * 通过注解标注字段,即可使用,无需主动获取 + * Autowired注解中标注name之后,将会使用byName的方式注入对应的字段, + * 不设置name属性,会默认使用byType的方式发现服务 + * (当同一接口有多个实现的时候,必须使用byName的方式发现服务) + ****************/ + Button bt_with_inject = findViewById(R.id.bt_with_inject); + bt_with_inject.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + singleService.sayHello(", World!"); + } + }); + + /**************** + * 获取Fragment + * 这里仅仅是单独的获取Fragment,并没有使用ViewPager或者其他进行展示 + * ****************/ + Button bt_with_fragment = findViewById(R.id.bt_with_fragment); + bt_with_fragment.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Fragment fragment = (Fragment) ARouter.getInstance().build("/test/fragment").navigation(); + Toast.makeText(MainActivity.this, "找到Fragment:" + fragment.toString(), Toast.LENGTH_SHORT).show(); + } + }); + + /**************** + * 带请求码进行跳转 + * ****************/ + Button bt_with_request_code = findViewById(R.id.bt_with_request_code); + bt_with_request_code.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ARouter.getInstance() + .build(MyRouter.ACTIVITY_URL_TARGET) + .navigation(MainActivity.this, 666); + } + }); + } + + /** + * 只会调用一次,除非卸载重装App + */ + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + switch (requestCode) { + case 666: + Toast.makeText(MainActivity.this, "activityResult"+String.valueOf(resultCode), Toast.LENGTH_SHORT).show(); + break; + default: + break; + } + } +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/TargetActivity.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/TargetActivity.java new file mode 100644 index 0000000..88c7527 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/TargetActivity.java @@ -0,0 +1,80 @@ +package com.example.arouterdemo.activity; + +import android.annotation.SuppressLint; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; + +import com.alibaba.android.arouter.facade.annotation.Autowired; +import com.alibaba.android.arouter.facade.annotation.Route; +import com.alibaba.android.arouter.launcher.ARouter; +import com.example.arouterdemo.R; +import com.example.arouterdemo.bean.TestObj; +import com.example.arouterdemo.router.MyRouter; + +import java.util.List; +import java.util.Map; + +// 在支持路由的页面上添加注解(必选) +// 这里的路径需要注意的是至少需要有两级,/xx/xx +@Route(path = MyRouter.ACTIVITY_URL_TARGET) +public class TargetActivity extends BaseActivity { + /** + * 1、需要写上对应的name,否则会出现null + * 2、这里的参数不需要private之类的修饰符,否则会报错 + * 3、List、Map只能写成List、Map;不能用ArrayList、HashMap之类替代 + */ + @Autowired(name = "key1") + byte key1; + @Autowired(name = "key2") + short key2; + @Autowired(name = "key3") + int key3; + @Autowired(name = "key4") + long key4; + @Autowired(name = "key5") + float key5; + @Autowired(name = "key6") + double key6; + @Autowired(name = "key7") + char key7; + @Autowired(name = "key8") + boolean key8; + @Autowired(name = "key9") + String key9; + @Autowired(name = "key10") + TestObj key10; + @Autowired(name = "key11") + List key11; + @Autowired(name = "key12") + Map key12; + + @SuppressLint("SetTextI18n") + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_target); + + TextView textView = findViewById(R.id.textView); + if (key8) { + textView.setText(key1 + "\n" + key2 + "\n" + key3 + "\n" + key4 + + "\n" + key5 + "\n" + key6 + "\n" + key7 + "\n" + key8 + + "\n" + key9 + "\n" + key10.toString() + "\n" + key11.toString() + "\n" + key12.toString()); + } + + Button bt_target = findViewById(R.id.bt_target); + bt_target.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // 应用内简单的跳转 + ARouter.getInstance().build(MyRouter.ACTIVITY_URL_MAIN).navigation(); + finish(); + } + }); + + setResult(666); + } +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/TargetInterceptorActivity.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/TargetInterceptorActivity.java new file mode 100644 index 0000000..0fec705 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/activity/TargetInterceptorActivity.java @@ -0,0 +1,38 @@ +package com.example.arouterdemo.activity; + +import android.annotation.SuppressLint; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.alibaba.android.arouter.facade.annotation.Autowired; +import com.alibaba.android.arouter.facade.annotation.Route; +import com.alibaba.android.arouter.launcher.ARouter; +import com.example.arouterdemo.R; +import com.example.arouterdemo.bean.TestObj; +import com.example.arouterdemo.router.MyRouter; + +import java.util.List; +import java.util.Map; + +// 在支持路由的页面上添加注解(必选) +// 这里的路径需要注意的是至少需要有两级,/xx/xx +@Route(path = MyRouter.ACTIVITY_URL_TARGET_INTERCEPTOR) +public class TargetInterceptorActivity extends BaseActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_target); + + Button bt_target = findViewById(R.id.bt_target); + bt_target.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // 应用内简单的跳转 + ARouter.getInstance().build(MyRouter.ACTIVITY_URL_MAIN).navigation(); + finish(); + } + }); + } +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/application/App.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/application/App.java new file mode 100644 index 0000000..092a9b1 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/application/App.java @@ -0,0 +1,27 @@ +package com.example.arouterdemo.application; + +import android.app.Application; + +import com.alibaba.android.arouter.launcher.ARouter; + +public class App extends Application { + + boolean isDebug = true; + + @Override + public void onCreate() { + super.onCreate(); + + if (isDebug) { // 这两行必须写在init之前,否则这些配置在init过程中将无效 + ARouter.openLog(); // 打印日志 + ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险) + } + ARouter.init(this); // 尽可能早,推荐在Application中初始化 + } + + @Override + public void onTerminate() { + super.onTerminate(); + ARouter.getInstance().destroy(); + } +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/bean/TestObj.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/bean/TestObj.java new file mode 100644 index 0000000..ea55472 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/bean/TestObj.java @@ -0,0 +1,22 @@ +package com.example.arouterdemo.bean; + +public class TestObj { + public String name; + public int id; + + public TestObj() { + } + + public TestObj(int id,String name) { + this.id = id; + this.name = name; + } + + @Override + public String toString() { + return "TestObj{" + + "name='" + name + '\'' + + ", id=" + id + + '}'; + } +} \ No newline at end of file diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/fragment/BlankFragment.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/fragment/BlankFragment.java new file mode 100644 index 0000000..c7b5760 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/fragment/BlankFragment.java @@ -0,0 +1,39 @@ +package com.example.arouterdemo.fragment; + + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.fragment.app.Fragment; + +import com.alibaba.android.arouter.facade.annotation.Autowired; +import com.alibaba.android.arouter.facade.annotation.Route; +import com.example.arouterdemo.R; +import com.example.arouterdemo.bean.TestObj; + +/** + * A simple {@link Fragment} subclass. + */ +@Route(path = "/test/fragment") +public class BlankFragment extends Fragment { + + @Autowired + String name; + + @Autowired(required = true) + TestObj obj; + + public BlankFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_blank,container,false); + return view; + } + +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/interceptor/Test1Interceptor.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/interceptor/Test1Interceptor.java new file mode 100644 index 0000000..b497194 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/interceptor/Test1Interceptor.java @@ -0,0 +1,83 @@ +package com.example.arouterdemo.interceptor; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.util.Log; + +import com.alibaba.android.arouter.facade.Postcard; +import com.alibaba.android.arouter.facade.annotation.Interceptor; +import com.alibaba.android.arouter.facade.callback.InterceptorCallback; +import com.alibaba.android.arouter.facade.template.IInterceptor; +import com.example.arouterdemo.util.MainLooper; +import com.example.arouterdemo.activity.MainActivity; +import com.example.arouterdemo.router.MyRouter; + +/** + * 一个拦截器的例子 + * + * @author Alex Contact me. + * @version 1.0 + * @since 2017/1/3 11:20 + */ +@Interceptor(priority = 7) +public class Test1Interceptor implements IInterceptor { + Context mContext; + + /** + * The operation of this interceptor. + * + * @param postcard meta + * @param callback cb + */ + @Override + public void process(final Postcard postcard, final InterceptorCallback callback) { + if (MyRouter.ACTIVITY_URL_TARGET_INTERCEPTOR.equals(postcard.getPath())) { + + // 这里的弹窗仅做举例,代码写法不具有可参考价值 + final AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.getThis()); + ab.setCancelable(false); + ab.setTitle("温馨提醒"); + ab.setMessage("想要跳转到Test4Activity么?(触发了\"/inter/test1\"拦截器,拦截了本次跳转)"); + ab.setNegativeButton("继续", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + callback.onContinue(postcard); + } + }); + ab.setNeutralButton("算了", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + callback.onInterrupt(null); + } + }); + ab.setPositiveButton("加点料", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + postcard.withString("extra", "我是在拦截器中附加的参数"); + callback.onContinue(postcard); + } + }); + + MainLooper.runOnUiThread(new Runnable() { + @Override + public void run() { + ab.create().show(); + } + }); + } else { + callback.onContinue(postcard); + } + } + + /** + * Do your init work in this method, it well be call when processor has been load. + * + * @param context ctx + */ + @Override + public void init(Context context) { + mContext = context; + Log.e("testService", Test1Interceptor.class.getName() + " has init."); + } +} \ No newline at end of file diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/router/MyRouter.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/router/MyRouter.java new file mode 100644 index 0000000..04c6647 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/router/MyRouter.java @@ -0,0 +1,8 @@ +package com.example.arouterdemo.router; + +// 这里的路径需要注意的是至少需要有两级,/xx/xx +public class MyRouter { + public static final String ACTIVITY_URL_MAIN = "/activity/main"; + public static final String ACTIVITY_URL_TARGET = "/activity/target"; + public static final String ACTIVITY_URL_TARGET_INTERCEPTOR = "/activity/target/interceptor"; +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/service/JsonServiceImpl.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/service/JsonServiceImpl.java new file mode 100644 index 0000000..62de169 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/service/JsonServiceImpl.java @@ -0,0 +1,36 @@ +package com.example.arouterdemo.service; + +import android.content.Context; + +import com.alibaba.android.arouter.facade.annotation.Route; +import com.alibaba.android.arouter.facade.service.SerializationService; +import com.alibaba.fastjson.JSON; + +import java.lang.reflect.Type; + +// 如果需要使用withObject()像:传递自定义对象、List<自定义对象>、Map +// 需要写上这个实现类 +// 就下面的代码一字不动的写上就行 +// 不知道为啥ARouter不把这写代码整成内置的 +@Route(path = "/yourservicegroupname/json") +public class JsonServiceImpl implements SerializationService { + @Override + public void init(Context context) { + + } + + @Override + public T json2Object(String text, Class clazz) { + return JSON.parseObject(text, clazz); + } + + @Override + public String object2Json(Object instance) { + return JSON.toJSONString(instance); + } + + @Override + public T parseObject(String input, Type clazz) { + return JSON.parseObject(input, clazz); + } +} diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/service/SingleService.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/service/SingleService.java new file mode 100644 index 0000000..6053ea7 --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/service/SingleService.java @@ -0,0 +1,29 @@ +package com.example.arouterdemo.service; + +import android.content.Context; +import android.widget.Toast; + +import com.alibaba.android.arouter.facade.annotation.Route; +import com.alibaba.android.arouter.facade.template.IProvider; + +/** + * 测试单类注入 + * + * @author zhilong Contact me. + * @version 1.0 + * @since 2017/4/24 下午9:04 + */ +@Route(path = "/yourservicegroupname/single") +public class SingleService implements IProvider { + + Context mContext; + + public void sayHello(String name) { + Toast.makeText(mContext, "Hello " + name, Toast.LENGTH_SHORT).show(); + } + + @Override + public void init(Context context) { + mContext = context; + } +} \ No newline at end of file diff --git a/ARouterDemo/app/src/main/java/com/example/arouterdemo/util/MainLooper.java b/ARouterDemo/app/src/main/java/com/example/arouterdemo/util/MainLooper.java new file mode 100644 index 0000000..83bd09e --- /dev/null +++ b/ARouterDemo/app/src/main/java/com/example/arouterdemo/util/MainLooper.java @@ -0,0 +1,25 @@ +package com.example.arouterdemo.util; + +import android.os.Handler; +import android.os.Looper; + +public class MainLooper extends Handler { + private static MainLooper instance = new MainLooper(Looper.getMainLooper()); + + protected MainLooper(Looper looper) { + super(looper); + } + + public static MainLooper getInstance() { + return instance; + } + + public static void runOnUiThread(Runnable runnable) { + if(Looper.getMainLooper().equals(Looper.myLooper())) { + runnable.run(); + } else { + instance.post(runnable); + } + + } +} diff --git a/ARouterDemo/app/src/main/res/anim/slide_in_bottom.xml b/ARouterDemo/app/src/main/res/anim/slide_in_bottom.xml new file mode 100644 index 0000000..30fafc6 --- /dev/null +++ b/ARouterDemo/app/src/main/res/anim/slide_in_bottom.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/ARouterDemo/app/src/main/res/anim/slide_out_bottom.xml b/ARouterDemo/app/src/main/res/anim/slide_out_bottom.xml new file mode 100644 index 0000000..f55737e --- /dev/null +++ b/ARouterDemo/app/src/main/res/anim/slide_out_bottom.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/ARouterDemo/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/ARouterDemo/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/ARouterDemo/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/ARouterDemo/app/src/main/res/drawable/ic_launcher_background.xml b/ARouterDemo/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/ARouterDemo/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ARouterDemo/app/src/main/res/layout/activity_main.xml b/ARouterDemo/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..4ce316e --- /dev/null +++ b/ARouterDemo/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,125 @@ + + + +