按两次后退按钮退出AIR android应用程序

时间:2017-02-16 06:11:29

标签: android air exit toast back

我最近在很多Android应用和游戏中注意到了这种模式:当点击后退按钮时,退出"在应用程序中,Toast会出现类似于&#34的消息;再次按BACK退出"。经过很长一段时间的搜索,我发现一个ANE扩展程序在我的应用程序中显示了一个很好的吐司,我已经测试了它并且单击后退按钮后工作正常,现在我想在单击后退按钮两次后关闭我的应用程序延迟3秒,使用强制代码退出 NativeApplication.nativeApplication.exit(0);

ANE扩展: https://github.com/mateuszmackowiak/NativeDialogs

我的代码:

import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import pl.mateuszmackowiak.nativeANE.notifications.Toast;

stop();

NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, CheckKeypress);

function CheckKeypress(event: KeyboardEvent): void {
    switch (event.keyCode) {
        case Keyboard.BACK:
            trace("Back key is pressed.");
            event.preventDefault();
            event.stopImmediatePropagation();
            Toast.show("Press Back again to exit", Toast.LENGTH_LONG);
            break;

        case Keyboard.MENU:
            trace("Menu key is pressed.");
            break;

        case Keyboard.SEARCH:
            trace("Search key is pressed.");
            break;
    }
}

3 个答案:

答案 0 :(得分:0)

在MainActivity中尝试此操作。可能有帮助。

import java.io.*;  
import java.awt.event.*;  
import javax.swing.*;    
import java.io.IOException;
import java.nio.file.StandardOpenOption;

public class Editor implements ActionListener
{    

    boolean updateStatus;
    Editor Jedtr;
    boolean saved;  
    boolean newFileFlag;  
    String fileName;    
    static File fileRef;  


    JFrame f;    
    JMenuBar mb;    
    JMenu file,edit,help;    
    JMenuItem cut,copy,paste,selectAll,NewFile,Open,Save,SaveAs;    
    JTextArea ta;  


    Editor(){ 


        saved=true;  
        newFileFlag=true;  
        fileName=new String("Untitled");  
        fileRef=new File(fileName);  

        Open=new JMenuItem("Open File");    
        Open.addActionListener(this);            
        file=new JMenu("File");    
        file.add(Open);             
        mb=new JMenuBar();    
        f=new JFrame();    
        cut=new JMenuItem("Cut");    
        copy=new JMenuItem("Copy");    
        paste=new JMenuItem("Paste");    
        selectAll=new JMenuItem("Select All");    

        /////////////////////////////////////////////////////////////////////////////////

        NewFile =new JMenuItem("New");
        Open=new JMenuItem("Open");
        Save=new JMenuItem("Save");
        SaveAs=new JMenuItem("Save As");

        ///////////////////////////////////////////////////////////////////////////////////

        cut.addActionListener(this);    
        copy.addActionListener(this);    
        paste.addActionListener(this);    
        selectAll.addActionListener(this);

        /////////////////////////////////////////////////////////////////////////////////////

        NewFile.addActionListener(this);
        Open.addActionListener(this);
        Save.addActionListener(this);
        SaveAs.addActionListener(this);

        //////////////////////////////////////////////////////////////////////////////////

        mb=new JMenuBar();    
        file=new JMenu("File");    
        edit=new JMenu("Edit");    
        help=new JMenu("Help");     
        file.add(NewFile);file.add(Open);file.add(Save);file.add(SaveAs);    
        edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);    
        mb.add(file);mb.add(edit);mb.add(help);    

        ///////////////////////////////////////////////////////////////////////////////////////

        ta=new JTextArea();    
        ta.setBounds(5,5,1200,1200);    
        f.add(mb);f.add(ta);    
        f.setJMenuBar(mb);  
        f.setLayout(null);    
        f.setSize(800,400);    
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }     




    /////////////////////////////Action performed/////////////////////////////////////////////////


    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==cut)                 
            ta.cut();                                // to cut text.
        else if(e.getSource()==paste)
            ta.paste();                             // to paste text. 
        else if(e.getSource()==copy)
            ta.copy();                              // to copy text.
        else if(e.getSource()==selectAll)
            ta.selectAll();                        // to select text. 
        else if(e.getSource()==Open)
        {
            JFileChooser fc=new JFileChooser();
            int i=fc.showOpenDialog(f);
            if(i==JFileChooser.APPROVE_OPTION)
            {
                File f=fc.getSelectedFile();
                String filepath=f.getPath();
                try
                {
                    BufferedReader br=new BufferedReader(new FileReader(filepath));
                    String s1="",s2="";
                    while((s1=br.readLine())!=null)
                    {
                        s2+=s1+"\n";
                    }
                    ta.setText(s2);
                    br.close();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }                                       //to open an existing file
        else if(e.getSource() == NewFile)
        {
            JFileChooser fc=new JFileChooser();
            int j=fc.showOpenDialog(f);
            if (j==JFileChooser.CUSTOM_DIALOG);

            System.out.println("New File");

            File file = new File("C:\\Android\\.txt");

            boolean result;
            try {
                // create a new file
                result = file.createNewFile();
                // test if successfully created a new file
                if(result){
                    System.out.println("Successfully created "+file.getAbsolutePath());

                }
                else{
                    System.out.println("Filed creating "+file.getAbsolutePath());
                }
            }
                catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    }                                            // to create a new file.

            else if(e.getSource()== SaveAs)
            {                                  // what to specify here?
                }



            else if(e.getSource()==Save)
            {                                 // what to specify here?
            }
}




                /*private Object SaveAs(File temp)
{
    // TODO Auto-generated method stub
    return null;
}



///////////////////////////////////////////////////////////////////////////////////////////////


private void updateStatus(File temp, boolean b)
{
    // TODO Auto-generated method stub

}*/
                boolean isSave()
                {
                    return saved;}  

                void setSave(boolean saved)
                {
                    this.saved=saved;
                }

                String getFileName()
                {
                    return new String(fileName);
                }

                void setFileName(String fileName)
                {
                    this.fileName=new String(fileName);
                }  

                ////-------File-------////////

                //public class File implements ActionListener{}

                ////-------File-------////////

                /*public void actionPerformedf(ActionEvent O) {    
if(O.getSource()==Open){    
    JFileChooser fc=new JFileChooser();    
    int i=fc.showOpenDialog(f);    
    if(i==JFileChooser.APPROVE_OPTION){    
        File f=fc.getSelectedFile();    
        String filepath=f.getPath();    
        try{  
        BufferedReader br=new BufferedReader(new FileReader(filepath));    
        String s1="",s2="";                         
        while((s1=br.readLine())!=null){    
        s2+=s1+"\n";    
        }    
        ta.setText(s2);    
        br.close();    
        }catch (Exception ex) 
        {
            ex.printStackTrace();          
        }
    }
    }
    }*/    

                public static void main(String[] args) {    

                    new Editor(); 

                }    
            }

答案 1 :(得分:0)

您可以在活动中使用onBackPressed()。当用户按下后退按钮时。将调用此方法。我认为以下代码可以帮助您。

@Override
public void onBackPressed() {
    if(initialClickTime == 0){
        initialClickTime = System.currentTimeMillis();
        //Toast with msg press again to exit
    }else{
       if ((System.currentTimeMillis() - initialClickTime)/1000 == 3){
           finish();
       }else{
           initialClickTime = 0;
           //Toast with msg press again to exit
       }
    }

答案 2 :(得分:0)

@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
    super.onBackPressed();
    return;
}

this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {

@Override
public void run() {
    doubleBackToExitPressedOnce=false;                       
}
}, 2000);
}