How does Cordova for android handle the exit button event in the App? cordovaandroid

Source: Internet
Author: User

How does Cordova for android handle the exit button event in the App? cordovaandroid

The project needs to add the processing of the Return key to the HTML5 Android App. It is found that adding the return key directly to the Activity does not work. After analyzing the cordova source code, it is found that the return key has been processed by WebView, so we can only process the return key in js!

 @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {if (exit > 1) {finish();} else {Toast.makeText(this, R.string.toast_exit, Toast.LENGTH_SHORT).show();exit++;}return true;} else {return super.onKeyDown(keyCode, event);}}
In the Activity that inherits CordovaActivity, the above Code does not work, because WebView has handled the return key event and exited the Activity.

  /*     * Android 2.x needs to be able to check where the cursor is.  Android 4.x does not     *      * (non-Javadoc)     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)     */        @Override    public boolean onKeyDown(int keyCode, KeyEvent event)    {        //Determine if the focus is on the current view or not        if (appView != null && appView.getFocusedChild() != null && (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU)) {                    return appView.onKeyDown(keyCode, event);        }        else            return super.onKeyDown(keyCode, event);    }        
Return key processing code in CordovaActivity source code


The following code responds to the back button and prompts the user to click again to exit.
If you do not click it three seconds later, the event will be re-registered.

Note: window. plugins. ToastPlugin. show_short () is a plug-in that displays the toast message!

Code:

// Wait for loading PhoneGapdocument. addEventListener ("deviceready", onDeviceReady, false); // The function onDeviceReady () {// button event document after the PhoneGap is loaded. addEventListener ("backbutton", eventBackButton, false); // return key document. addEventListener ("menubutton", eventMenuButton, false); // menu key document. addEventListener ("searchbutton", eventSearchButton, false); // search key} // return key function eventBackButton () {// confirm ("Click again to exit! "); Window. plugins. ToastPlugin. show_short ('click again to exit! '); Document. removeEventListener ("backbutton", eventBackButton, false); // cancel the return key // register var intervalID = window after 3 seconds. setInterval (function () {window. clearInterval (intervalID); document. addEventListener ("backbutton", eventBackButton, false); // return key}, 3000);} // menu key function eventMenuButton () {window. plugins. toastPlugin. show_short ('click the menu button! ');} // Search key function eventSearchButton () {window. plugins. ToastPlugin. show_short (' click the search button! ');}





How does android TabActivity obtain the response button from the mobile phone?

Rewrite the onKeydown method. Paste a piece of code:
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){

If (keyCode = KeyEvent. KEYCODE_MENU) {// intercept the meu key event
// Do something...
}

If (keyCode = KeyEvent. KEYCODE_BACK) {// intercept the return button event
// Do something...
}
Return true;
}

If you use eclipse to develop Android software, click the exit button and you will be redirected to an interface where you want to exit the program with a touch, but only the current page will be ended.

It is troublesome to exit the program in Android, especially in multiple Activity programs. Before Android 2.2, you can exit the program using the following code:
Java code
ActivityManager am = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE );
Am. restartPackage (getPackageName ());

This method is the most convenient and simple way to exit the program, but it cannot be used after 2.2 and 2.2. If we want to exit the program, there are four methods:
Using the content view Stack: if the program has multiple interfaces but does not require an Activity on each interface, you can design each interface as a View. During interface switching, you only need to call the setContentView method of the Activity to set the Contentview of the Activity. To exit the program, you only need to exit the Activity, but you need to design a stack to manage the content view.
You can customize the stack of an Activity and finish all the activities in the stack when the program exits. This method is described in my previous articles.
The essence of the first two methods is that you need to design a stack to manage the interface or Activity, so that the program is more complicated. The method in 3rd is to first let the program go to the Home interface and then kill process: the code is as follows:
Java code
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_HOME );
Intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP );
StartActivity (intent );
Android. OS. Process. killProcess (Process. myPid ());
Another method is to use the Android Broadcast mechanism. Register the message for exiting the program in all the activities and call the finish method when receiving the message. Then, the Activity that exits the program function broadcasts the closed message. The Code is as follows:
Java code
Package com. kingtone. activity;

Import android. app. Activity;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. content. IntentFilter;
/**
* The parent class of all activities, used to register broadcast events that exit the program,
* Handle received exit program events
* @ Author Administrator
*
*/
Public class CommonActivity extends Activity {

// Broadcast internal class. When a close event is received, call the finish method to end the activity.
Private BroadcastReceiver broadcastReceiver = new BroadcastReceiver (){
... The remaining full text>

Related Article

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.