---
title: "锁定触控按键"
author: "ping8080"
date: "2011-10-18T15:01:40Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2011-10-18T15:01:40Z"
id: 1199284
url: "https://ping8080.iteye.com/blog/1199284"
---

1 在KeyEvent文件 中自定义键值

 

KeyEvent.KEYCODE\_KEYLOCK

 

2 PhoneWindowManager 在方法public int interceptKeyTq（）中加入

 

 if(event.keycode == KeyEvent.KEYCODE\_KEYLOCK && event.value != 0 ){             
            long when = System.currentTimeMillis();  
            if(!mTouchKeyLock){  
                mTouchKeyLock = true;  
                Context context = mContext;  
                String ns = Context.NOTIFICATION\_SERVICE;  
                NotificationManager mNotificationManager =(NotificationManager)mContext.getSystemService(ns);

                IntentFilter filter = new IntentFilter();  
                filter.addAction(LOCK\_ACTION);  
                mContext.registerReceiver(mBroadcastReceiver,filter);

                int icon = com.android.internal.R.drawable.stat\_key\_locked;  
                CharSequence tickerText = mContext.getResources().getString(com.android.internal.R.string.label\_key\_locked);  
                Notification notification = new Notification(icon, tickerText, when);  
                notification.defaults |= Notification.DEFAULT\_SOUND;  
                notification.flags = Notification.FLAG\_AUTO\_CANCEL | Notification.FLAG\_NO\_CLEAR;

                CharSequence contentTitle = mContext.getResources().getString(com.android.internal.R.string.label\_key\_locked\_name);  
                CharSequence contentText = mContext.getResources().getString(com.android.internal.R.string.label\_key\_unlocked);  
                Intent intent = new Intent();  
                //intent.setFlags(Intent.FLAG\_ACTIVITY\_CLEAR\_TOP | Intent.FLAG\_ACTIVITY\_NEW\_TASK);  
                intent.setAction(LOCK\_ACTION);  
                PendingIntent contentIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);  
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  
                mNotificationManager.notify(KEY\_LOCKED\_ID, notification);

                result &= ~ACTION\_PASS\_TO\_USER;  
                return result;  
            } else {  
                mTouchKeyLock = false;  
                String ns = Context.NOTIFICATION\_SERVICE;  
                NotificationManager mNotificationManager =(NotificationManager)mContext.getSystemService(ns);  
                mNotificationManager.cancel(KEY\_LOCKED\_ID);

                result &= ~ACTION\_PASS\_TO\_USER;  
                return result;  
            }  
        }  
        if(mTouchKeyLock){             
            if(event.keycode == KeyEvent.KEYCODE\_MENU || event.keycode ==KeyEvent.KEYCODE\_BACK  
                || event.keycode ==KeyEvent.KEYCODE\_SEARCH || event.keycode ==KeyEvent.KEYCODE\_HOME){  
             
                result &= ~ACTION\_PASS\_TO\_USER;  
                return result;  
            }  
        }