Android ARouter攔截器實(shí)現(xiàn)方法
修改下之前的demo,測(cè)試下ARouter攔截器。頁(yè)面如下:

未登錄狀態(tài),點(diǎn)擊下面紅色按鈕,跳轉(zhuǎn)登錄頁(yè)面:

輸入一個(gè)名字點(diǎn)擊登錄按鈕跳轉(zhuǎn)到下面用戶中心頁(yè)面:

回到首頁(yè),點(diǎn)擊退出登錄,重新點(diǎn)擊最下面按鈕,直接跳轉(zhuǎn)到用戶中心頁(yè)面:

ok. 代碼如下:
在業(yè)務(wù)模塊新增LoginActivity:
package com.example.module_2;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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.common.LoginService;
import com.example.module_2.R;
/**
* 登錄頁(yè)面
*/
@Route(path = "/login/login")
public class LoginActivity extends AppCompatActivity {
@Autowired(name = "goto_path")
String gotoPath; // 登錄成功后要跳轉(zhuǎn)的路徑
private EditText etUsername;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 注入?yún)?shù)
ARouter.getInstance().inject(this);
// 創(chuàng)建簡(jiǎn)單的 UI
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(50, 100, 50, 100);
etUsername = new EditText(this);
etUsername.setHint("請(qǐng)輸入用戶名");
etUsername.setTextSize(16);
Button btnLogin = new Button(this);
btnLogin.setText("登錄");
btnLogin.setTextSize(16);
btnLogin.setPadding(20, 30, 20, 30);
layout.addView(etUsername);
layout.addView(btnLogin);
setContentView(layout);
// 設(shè)置登錄按鈕點(diǎn)擊事件
btnLogin.setOnClickListener(v -> {
String username = etUsername.getText().toString().trim();
if (username.isEmpty()) {
Toast.makeText(this, "請(qǐng)輸入用戶名", Toast.LENGTH_SHORT).show();
return;
}
// 執(zhí)行登錄
LoginService.login(username);
Toast.makeText(this, "登錄成功!歡迎:" + username, Toast.LENGTH_LONG).show();
if (gotoPath != null) {
ARouter.getInstance().build(gotoPath).navigation(); // 跳轉(zhuǎn)到目標(biāo)頁(yè)面
finish();
} else {
finish();
}
});
}
}個(gè)人中心頁(yè)面:
package com.example.module_2;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.LoginService;
/**
* 需要登錄才能訪問(wèn)的頁(yè)面
*
* 路徑以 /needlogin/ 開(kāi)頭,會(huì)被 LoginInterceptor 攔截檢查
*/
@Route(path = "/needlogin/personal")
public class PersonalCenterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText(String.format("個(gè)人中心頁(yè)面\n\n %s", LoginService.getUsername()));
textView.setTextSize(18);
textView.setPadding(50, 100, 50, 100);
setContentView(textView);
}
}攔截器也寫(xiě)在該業(yè)務(wù)模塊,如下:
package com.example.module_2.interceptor;
import android.content.Context;
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.launcher.ARouter;
/**
* 日志記錄攔截器
*
* 作用:記錄所有路由跳轉(zhuǎn)的詳細(xì)信息,調(diào)試用
*
* 優(yōu)先級(jí):數(shù)字小的優(yōu)先級(jí)高,先執(zhí)行
*/
@Interceptor(priority = -1, name = "日志記錄攔截器")
public class LoggerInterceptor implements com.alibaba.android.arouter.facade.template.IInterceptor {
private static final String TAG = "ARouter-Logger";
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
// 記錄路由信息
Log.d(TAG, "========================================");
Log.d(TAG, ">>> 路由跳轉(zhuǎn)開(kāi)始");
Log.d(TAG, ">>> 目標(biāo)路徑:" + postcard.getPath());
// 打印所有參數(shù)
if (postcard.getExtras() != null && !postcard.getExtras().isEmpty()) {
Log.d(TAG, ">>> 攜帶參數(shù):");
for (String key : postcard.getExtras().keySet()) {
Object value = postcard.getExtras().get(key);
Log.d(TAG, " " + key + " = " + value);
}
} else {
Log.d(TAG, ">>> 無(wú)參數(shù)");
}
// 繼續(xù)處理,傳遞自定義回調(diào)
callback.onContinue(postcard); // 交給下一個(gè)攔截器處理
}
@Override
public void init(Context context) {
Log.d(TAG, ">>> LoggerInterceptor 初始化完成");
}
}package com.example.module_2.interceptor;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
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.launcher.ARouter;
import com.example.common.LoginService;
/**
* 登錄檢查攔截器
*
* 作用:攔截所有需要登錄的頁(yè)面,如果未登錄則跳轉(zhuǎn)到登錄頁(yè)
*
*/
@Interceptor(priority = 1, name = "登錄檢查攔截器")
public class LoginInterceptor implements com.alibaba.android.arouter.facade.template.IInterceptor {
private static final String TAG = "LoginInterceptor";
private Context context;
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
Log.d(TAG, ">>> LoginInterceptor 開(kāi)始處理。 \n 嘿嘿");
// 獲取目標(biāo)路徑
String path = postcard.getPath();
// 只攔截特定路徑(以 /needlogin/ 開(kāi)頭的路徑都需要登錄)
if (path != null && path.startsWith("/needlogin/")) {
Log.d(TAG, "檢測(cè)到需要登錄的路徑:" + path);
// 檢查是否已登錄
if (!LoginService.isLogin()) {
Log.d(TAG, "用戶未登錄,準(zhǔn)備跳轉(zhuǎn)到登錄頁(yè)");
// 保存原始請(qǐng)求信息
// postcard.withString("goto_path", path); // 和47行路由傳參一樣,調(diào)用一處就行。
// 重定向到登錄頁(yè)
ARouter.getInstance()
.build("/login/login")
.withString("goto_path", path)
.navigation();
// 中斷路由流程
callback.onInterrupt(new RuntimeException("請(qǐng)先登錄哈"));
// 顯示提示信息
if (context != null) {
Toast.makeText(context, "請(qǐng)先登錄哦", Toast.LENGTH_SHORT).show();
}
return;
} else {
Log.d(TAG, "用戶已登錄,允許通過(guò)");
}
} else {
Log.d(TAG, "該路徑不需要登錄驗(yàn)證:" + path);
}
// 繼續(xù)處理下一個(gè)攔截器或目標(biāo)頁(yè)面
callback.onContinue(postcard);
}
@Override
public void init(Context context) {
this.context = context;
Log.d(TAG, ">>> LoginInterceptor 初始化完成");
}
}主模塊注冊(cè)業(yè)務(wù)模塊的activity:

主頁(yè)MainActivity關(guān)鍵代碼,測(cè)試需要校驗(yàn)登錄的頁(yè)面(即個(gè)人中心頁(yè)面):
// 測(cè)試攔截器 - 訪問(wèn)需要登錄的頁(yè)面
View btnNeedLogin = findViewById(R.id.btnNeedLogin);
if (btnNeedLogin != null) {
btnNeedLogin.setOnClickListener(v -> {
Log.d("MainActivity", "準(zhǔn)備訪問(wèn)需要登錄的頁(yè)面");
ARouter.getInstance()
.build("/needlogin/personal")
.navigation();
});
}ok. 看下未登錄時(shí),點(diǎn)擊跳轉(zhuǎn)個(gè)人中心,攔截器日志:

ok. 日志攔截器先打印了路由信息。然后登錄攔截器校驗(yàn)了未登錄,重定向到登錄頁(yè)面。
然后登錄頁(yè)面登錄成功,跳轉(zhuǎn)個(gè)人中心的時(shí)候,攔截器日志如下:

ok. 這次允許通過(guò),成功跳轉(zhuǎn)到個(gè)人中心頁(yè)面。
ok. 攔截器挺有用。能用于實(shí)現(xiàn)一些公共功能。
到此這篇關(guān)于Android使用ARouter實(shí)現(xiàn)攔截器功能的文章就介紹到這了,更多相關(guān)android arouter攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android BitmapUtils工具類(lèi)使用詳解
這篇文章主要為大家詳細(xì)介紹了Android BitmapUtils工具類(lèi)的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
android studio 3.6.1升級(jí)后如何處理 flutter問(wèn)題
這篇文章主要介紹了android-studio-3.6.1升級(jí)后 flutter問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android Kotlin的使用及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android Kotlin的使用及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android入門(mén)之實(shí)現(xiàn)手工發(fā)送一個(gè)BroadCast
這篇文章主要通過(guò)手工來(lái)發(fā)送一條BroadCast進(jìn)一步來(lái)帶大家深入了解BroadCast,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定幫助,感興趣的可以收藏一下2022-12-12
Android 仿今日頭條簡(jiǎn)單的刷新效果實(shí)例代碼
這篇文章主要介紹了Android 仿今日頭條簡(jiǎn)單的刷新效果實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android無(wú)需權(quán)限調(diào)起系統(tǒng)相機(jī)
在進(jìn)行一些小型APP的開(kāi)發(fā),或者是對(duì)拍照界面沒(méi)有自定義要求時(shí),我們可以用調(diào)起系統(tǒng)相機(jī)的方式快速完成拍照需求2023-03-03
Android判斷設(shè)備網(wǎng)絡(luò)連接狀態(tài)及判斷連接方式的方法
這篇文章主要介紹了Android判斷設(shè)備網(wǎng)絡(luò)連接狀態(tài)及判斷連接方式的方法,涉及Android針對(duì)網(wǎng)絡(luò)連接的相關(guān)判定技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android 實(shí)現(xiàn)抖音小游戲潛艇大挑戰(zhàn)的思路詳解
《潛水艇大挑戰(zhàn)》是抖音上的一款小游戲,最近特別火爆,很多小伙伴都玩過(guò)。接下來(lái)通過(guò)本文給大家分享Android 手?jǐn)]抖音小游戲潛艇大挑戰(zhàn)的思路,需要的朋友可以參考下2020-04-04

