android几个常用按键的获取

package com.wzy.cn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.Toast;

public class AABActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }   
    @Override
 public void onAttachedToWindow() {
  // TODO Auto-generated method stub
  super.onAttachedToWindow();
  this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
 }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //返回键的回去
        if (keyCode == KeyEvent.KEYCODE_BACK ) {
         dialog();
           return false;
        } else if(keyCode == KeyEvent.KEYCODE_HOME){

    //Home按键的获取
                out();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
        }       
        return super.onKeyDown(keyCode, event);
    }
 private void out() {
  // TODO Auto-generated method stub

  Toast.makeText(AABActivity.this,"按HOME键退出程序", Toast.LENGTH_SHORT).show();
 }

 private void dialog() {
  // TODO Auto-generated method stub
  Intent it=new Intent();
  it.setClass(AABActivity.this,ABC.class);
  startActivity(it);
  Toast.makeText(this,"返回", Toast.LENGTH_SHORT).show();

 }
   
}

posted @ 2012-11-28 17:09  crazywenza  阅读(507)  评论(0编辑  收藏  举报