一個輕量級、簡單、智能而且強大的安卓路由庫java
Github地址android
在build.gradle文件中添加如下依賴:git
dependencies {
implementation 'zlc.season:rxrouter:x.y.z'
annotationProcessor 'zlc.season:rxrouter-compiler:x.y.z'
}
複製代碼
若是使用 Kotlin
,用 kapt
替換 annotationProcessor
github
首先在咱們須要路由的Activity上添加 @Url
註解:express
@Url("this is a url")
class UrlActivity : AppCompatActivity() {
...
}
複製代碼
而後建立一個被 @Router
註解的類,用來告訴RxRouter這裏有一個路由器:apache
@Router
class MainRouter{
}
複製代碼
這個類不須要有任何其他的代碼,RxRouter會根據這個類的類名自動生成一個 RouterProvider
,好比這裏的 MainRouter
將會生成 MainRouterProvider
.bash
接着咱們須要把這些路由器添加到 RxRouterProviders
中:app
class CustomApplication : Application() {
override fun onCreate() {
super.onCreate()
RxRouterProviders.add(MainRouterProvider())
}
}
複製代碼
最後,就能夠開始咱們的表演了:less
RxRouter.of(context)
.route("this is a uri")
.subscribe()
複製代碼
攜帶參數跳轉:ide
經過with方法,你能夠給本次路由添加一系列參數.
RxRouter.of(context)
.with(10) //int value
.with(true) //boolean value
.with(20.12) //double value
.with("this is a string value") //string value
.with(Bundle()) //Bundle value
.route("this is a uri")
.subscribe()
複製代碼
onActivityResult
方法了想要獲取跳轉返回的值?不再用寫一大堆 onActivityResult
方法了!鏈式調用,一步到位!
RxRouter.of(context)
.with(false)
.with(2000)
.with(9999999999999999)
.route("this is a uri")
.subscribe {
if (it.resultCode == Activity.RESULT_OK) {
val intent = it.data
val stringResult = intent.getStringExtra("result")
result_text.text = stringResult
stringResult.toast()
}
}
複製代碼
若是有結果返回,在subscribe中處理就好了.
不想用Url註解?沒問題,RxRouter一樣支持原始的指定類名的跳轉方式,和url跳轉的方式相同:
RxRouter.of(context)
.routeClass(ClassForResultActivity::class.java)
.subscribe{
if (it.resultCode == Activity.RESULT_OK) {
val intent = it.data
val stringResult = intent.getStringExtra("result")
result_text.text = stringResult
stringResult.toast()
}
}
複製代碼
一樣的,RxRouter也支持系統的Action和自定義的Action跳轉.
自定義Action跳轉:
<activity android:name=".ActionActivity">
<intent-filter>
<action android:name="zlc.season.sample.action" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
複製代碼
RxRouter.of(context)
.routeAction("zlc.season.sample.action")
.subscribe({
"no result".toast()
}, {
it.message?.toast()
})
複製代碼
系統Action跳轉:
//撥打電話
RxRouter.of(this)
.addUri(Uri.parse("tel:123456"))
.routeSystemAction(Intent.ACTION_DIAL)
.subscribe()
//發送短信
val bundle = Bundle()
bundle.putString("sms_body", "這是信息內容")
RxRouter.of(this)
.addUri(Uri.parse("smsto:10086"))
.with(bundle)
.routeSystemAction(Intent.ACTION_SENDTO)
.subscribe()
複製代碼
RxRouter擁有一個小巧而強大的防火牆,可以在路由以前根據防火牆的規則進行攔截,您能夠添加一個或者多個防火牆.
//建立一個LoginFirewall
class LoginFirewall : Firewall {
override fun allow(datagram: Datagram): Boolean {
if (notLogin) {
"您尚未登陸,請先登陸".toast()
return false
}
return true
}
}
//將Firewall添加到路由中
RxRouter.of(this)
.addFirewall(LoginFirewall())
.route("this is a url")
.subscribe()
複製代碼
Copyright 2018 Season.Zlc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
複製代碼