Android登入攔截器實現方式(一)

來源:互聯網
上載者:User

標籤:android登入 攔截器 interceptor

  對於App端來說,如果能保證使用者在登入後能自動延續登入前的操作,將是非常不錯的使用者體驗。所以專門花了點時間,想了兩種方式來實現該種需求。

   這篇文章講先第一種方式,這種方式實現的思路大致是這樣:

   在執行需要登陸狀態的操作時,將該操作交由攔截器處理,該攔截器會把該操作延後到登陸成功後處理。

   舉個例子,使用者想查看設定檔,通常的做法是先判斷登入與否,如果沒有登入,得先去登入,然後從登入頁返回後在onActivityResult中再執行相應的跳轉。但如果使用攔截器後,你只需將設定檔頁必要的資訊(比如標記activity的action)傳給攔截器,攔截器經過處理後,再跳到設定檔頁,這樣你只需要關心你原有的需求,查看設定檔即可。

   接下來看代碼:

public void jumpToActivity(Context ctx, String target, Bundle params) {jumpToActivity(ctx, target, params, new Intent(ctx, getLoginClass()));}/** *  * @param ctx * @param target *            目標activity的action * @param params *            需要傳給目標activity的參數 * @param loginIntent *            帶參數的指向登入頁的intent */public void jumpToActivity(Context ctx, String target, Bundle params,Intent loginIntent) {if (TextUtils.isEmpty(target) || loginIntent == null)throw new RuntimeException("No target activity.");JumpInvoker invoker = new JumpInvoker(target, params);if (logon()) {invoker.invoke(ctx);} else {loginIntent.putExtra(INVOKER, invoker);loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);ctx.startActivity(loginIntent);}}

    代碼JumpInvoker對象是登入成功後的邏輯執行者,它會被傳輸到登入模組中,在登入成功後執行invoke方法跳到指定頁面,見代碼:

    

        @Overridepublic void invoke(Context ctx) {Intent it = new Intent(mTargetAction);it.putExtras(mData);it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);ctx.startActivity(it);}


使用方式:

    1.在登入成功的地方獲利Invoker對象,調用Invoker.invoke方法。

    Invoker invoker = getIntent().getParcelableExtra(Interceptor.INVOKER);invoker.invoke(getApplicationContext());

   2.需要跳轉到登入後顯示的Activity時,可使用代碼中的jumpToActivity方法,將目標頁的action及參數(非必須)傳進即可。


    源碼與jar包見附件,其中只實現在登入跳轉的邏輯。對於其它需求,比如收藏點擊,在登入後自動收藏,也可以參照實現Interceptor與Invoker介面即可。

本文出自 “Android Pool” 部落格,請務必保留此出處http://yeerik.blog.51cto.com/10547727/1681729

Android登入攔截器實現方式(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.