警报对话框需要两次返回才能完成活动

时间:2014-03-14 07:39:31

标签: android

在我的活动中,当用户按下POWER按钮微调器显示成功时,我正在显示微调器如果用户选择任何选项工作正常但是当用户按下它需要的两次后按以显示原始活动我该怎么办?如何关闭和完成活动只需单击后按???

public class ActivityHome extends Activity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.mainactivity);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if(KeyEvent.KEYCODE_POWER == event.getKeyCode() && isRooted()) {     
        startActivity(new Intent(ActivityHome.this, Spinnermenu.class));        
        return true;      
    }
    return super.onKeyDown(keyCode, event);
}

public class Spinnermenu  extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);     
    final String items[] = {"Power off","Restart"};
    AlertDialog.Builder ab=new AlertDialog.Builder(Spinnermenu.this);
    ab.setTitle("Power Options");
    ab.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface d, int choice) {
        if(choice == 0) {       
                    Toast.makeText(Spinnermenu.this,"PowerOff Perform",Toast.LENGTH_SHORT).show();                      
        }
        else if(choice == 1) {

        }
    });
    ab.show();
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
       if(keyCode==KeyEvent.KEYCODE_BACK){
            this.finish ();
       }
       return true;
     }
}

2 个答案:

答案 0 :(得分:0)

您只需将此代码放入您的程序中,它就可以满足您的需求....

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

一切顺利......

答案 1 :(得分:0)

调用onBackPressed()
       public boolean onKeyDown(int keyCode, KeyEvent event) 
{
   if(keyCode==KeyEvent.KEYCODE_BACK){
   // call here
      onBackPressed();
   }
   return true;
 }

             @Override
public void onBackPressed() {


// just write alertbox option here

   AlertDialog.Builder ab=new AlertDialog.Builder(Spinnermenu.this);
ab.setTitle("Power Options");
ab.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int choice) {
    if(choice == 0) {       
                Toast.makeText(Spinnermenu.this,"PowerOff Perform",Toast.LENGTH_SHORT).show();  
//add,     
    finish();                    
    }
    else if(choice == 1) {
//add,

    finish();   

    }
});
ab.show();


}