Android Double click Back key to Exit program (Alarm mechanism implementation)

Source: Internet
Author: User

==================== Realization Principle ========================

1. Overriding the Back key method
2. Setting the Exit ID
3. Delay Reset Logo

==================== delay Reset ========================
1. Inherit Broadcastreceiver (be sure to register in Manifest)
2. Use Alarmmanager to set the delay start task (in milliseconds)
3. Timed Auto-start broadcastreceiver (Reset Exit ID)

==================== Activity Code ========================

 Public classAlarmactivityextendsActivity {Private Static BooleanQuit =false; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }    /*** Override back Key method*/     Public voidonbackpressed () {if(Quit = =false) {Toast.maketext ( This, "Press again to exit the program", Toast.length_short). Show ();                          Alarmattime (); //Call TimerQuit =true;//Set Exit ID}Else {            Super. onbackpressed ();  This. Finish ();//Exit Current Activity        }    }    /*** Alarm mechanism timer*/    Private voidAlarmattime () {Intent Intent=NewIntent ( This, Alarmreceiver.class);//Start the receiverpendingintent pi = pendingintent.getbroadcast ( This, 0, intent, 0);//setting up a delayed startAlarmmanager Manager =(Alarmmanager) Getsystemservice (Context.alarm_service); LongDelay = System.currenttimemillis () + 2000;//get the current time and delay 2 secondsManager.set (alarmmanager.rtc_wakeup, delay, pi);//delayed start of broadcast receivers    }    /*** Broadcastreceiver broadcast receiver*/     Public Static classAlarmreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {quit=false;//Reset Exit ID        }    }}

Be sure to register in the Manifest

    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <ActivityAndroid:name=". Alarmactivity ">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <receiverAndroid:name=". Activity.alarmactivity$alarmreceiver "></receiver>    </Application>

Using the Alarm mechanism to achieve double-click return is actually more cumbersome, Alarm suitable for a long time or need to wake up the CPU when used, generally use the Java API provided in the timer class can be very good to complete the scheduled task.

Android Double click Back key to Exit program (Alarm mechanism implementation)

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.