最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ARouter攔截器實(shí)現(xiàn)方法

 更新時(shí)間:2026年03月09日 09:23:13   作者:zh_xuan  
本文介紹了如何使用ARouter在Android應(yīng)用中實(shí)現(xiàn)攔截器功能,通過(guò)攔截器可以實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)前的校驗(yàn),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

修改下之前的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)文章

最新評(píng)論

长顺县| 巴马| 阳东县| 游戏| 绥棱县| 满洲里市| 长沙县| 安阳县| 河北区| 荥经县| 长乐市| 嵊州市| 清水河县| 车险| 营口市| 高台县| 桃江县| 舒兰市| 江陵县| 锡林郭勒盟| 洪江市| 大邑县| 江津市| 绥德县| 隆林| 聂拉木县| 萍乡市| 安图县| 滨海县| 延吉市| 宁城县| 广宗县| 蓝田县| 咸丰县| 白城市| 依安县| 温泉县| 邹平县| 新晃| 岳西县| 清水河县|