Android login Blocker implementation method (II)

Source: Internet
Author: User

The second way is the main idea is to start a dedicated waiting for the results of the thread, in the process of landing in a suspended state, after the landing of the results by other threads to wake up the waiting thread, continue to work before .

Take the time to write a simple class to achieve the above way Logonrequiretask, later to expand and repair.

Use the code like the following, directly using the Execute method to do what you want to do, you can jump to the personal information page, can be a collection function, so the processing is very simple and clear. But don't forget to call logonrequiretask.loginended () when the login logic is complete.

New Logonrequiretask () {@Overridepublic Boolean logon () {return logon;//utils.islogin ()} @Overridepublic void Onlogout ( {//TODO auto-generated method Stub}}.execute (new Runnable () {@Overridepublic void run () {//when login Ok,go On//startac Tivity or do other things}});

It is recommended to change the runnable to static and weak references to the context to prevent memory problems.



The following is the entire code of Logonrequiretask, landing running status hangs thread, wake up after landing.

package com.idroid.requirement;import java.util.concurrent.callable;import  java.util.concurrent.executionexception;import java.util.concurrent.executor;import  java.util.concurrent.executors;import java.util.concurrent.futuretask;import android.os.handler; Public abstract class logonrequiretask {private static volatile executor  sdefaultexecutor = executors.newsinglethreadexecutor ();p Rivate final static int  sDefaultWaitLong = 5 * 60 * 1000;private static final  Handler shandler = new handler ();p rivate volatile status mstatus =  status.idle;public enum status {running, idle,}public abstract boolean  logon ();/** *  skip to Page  */public abstract void onlogout ();p ublic  when not logged in Static void loginended ()  {synchronized  (Shandler) &NBsp {Shandler.notifyall ();}} Public void setstatus (Status status)  {mstatus = status;} Public final void execute (Runnable r)  {         if  (logon ())  {shandler.post (R); return;} if  (mstatus != status.running) onlogout ();mstatus = status.running;final  Workercallable callable = new workercallable ()  {@Overridepublic  Boolean  Call ()  throws Exception {if  (logon ()) return true;if  (mstatus ==  Status.idle) return false; Process.setthreadpriority (Process.thread_priority_background);synchronized  (SHandler)  {sHandler.wait (Sdefaultwaitlong); Mstatus = status.idle;return logon ();}}; Futuretask<boolean> future = new futuretask<boolean> (callable)  {@ Overrideprotected void done () &NBSP;{TRY&NBSP;{FINAL&NBSP;BOOLEAN&NBSP;RESULT&NBSP;=&NBsp;get ();if  (Result)  {shandler.post (callable.runnable);}  else {shandler.removecallbacksandmessages (null);}}  catch  (interruptedexception e)  {e.printstacktrace ();}  catch  (executionexception e)  {e.printstacktrace ();}  catch  (nullpointerexception e)  {e.printstacktrace ();}}; Callable.runnable = r;sdefaultexecutor.execute (future);} Private static abstract class workercallable implements callable<boolean>  {runnable runnable;}}



This article is from the "Android Pool" blog, so be sure to keep this source http://yeerik.blog.51cto.com/10547727/1682053

Android login Blocker implementation method (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.