按键监听

home键:

public static BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {  //监听home键,如果按了home键则退出长截屏模式
	    String SYSTEM_REASON = "reason";  
	    String SYSTEM_HOME_KEY = "homekey";  
	    String RECENT_APPS = "recentapps";  
	       
	    @Override  
	    public void onReceive(Context context, Intent intent) {  
	    	Tools.log(TAG, "mHomeKeyEventReceiver   onReceive ");
	    	if (mScreenService.isSavingPicture) {
	    		Tools.log(TAG, "mHomeKeyEventReceiver   onReceive   mScreenService.isSavingPicture==true  now return");
	    		return;
			}
	        String action = intent.getAction();  
	        Tools.log(TAG, "  intent.getAction(); =====" + intent.getAction());
	        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {  
	            String reason = intent.getStringExtra(SYSTEM_REASON);  
	            Tools.log(TAG, "   intent.getStringExtra(SYSTEM_REASON) == " + intent.getStringExtra(SYSTEM_REASON));
	            if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {  
	                 //表示按了home键,程序到了后台  
	            	Tools.log(TAG, "mHomeKeyEventReceiver onReceive Home clicked  stopSelfBySelf");
	                mScreenService.stopSelfBySelf();
	            }else if (TextUtils.equals(reason, RECENT_APPS)) {
	            	Tools.log(TAG, "mHomeKeyEventReceiver onReceive recent apps  clicked   stopSelfBySelf");
	                mScreenService.stopSelfBySelf();
				}
	        }   
	    }  
	}; 
	public static BroadcastReceiver PhoneStatReceiver = new BroadcastReceiver() {  
	    
	    @Override  
	    public void onReceive(Context context, Intent intent) {  
	            //如果是拨打电话  
	            if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                          
//	                    incomingFlag = false;  
	                    String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);                                  
	            }else{                          
	                    //如果是来电  
	                    TelephonyManager tm =   
	                        (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                          
	                      
	                    switch (tm.getCallState()) {
	                    case TelephonyManager.CALL_STATE_RINGING:  
	                    	if (mScreenService.isSavingPicture) {
	                    		Tools.log(TAG, "phoneStatReceiver call is comming  mScreenService.isSavingPicture=="+mScreenService.isSavingPicture);
	                    		return;
	                    	}
	                    	Tools.log(TAG, "phoneStatReceiver call is comming stopSelfBySelf");
	                    	mScreenService.stopSelfBySelf();
	                    }   
	            }  
	    }  
	} ;

熄屏监听:这个只能动态注册才能接收到

/**
     * Broadcast Action: Sent when the device goes to sleep and becomes non-interactive.
     * <p>
     * For historical reasons, the name of this broadcast action refers to the power
     * state of the screen but it is actually sent in response to changes in the
     * overall interactive state of the device.
     * </p><p>
     * This broadcast is sent when the device becomes non-interactive which may have
     * nothing to do with the screen turning off.  To determine the
     * actual state of the screen, use {@link android.view.Display#getState}.
     * </p><p>
     * See {@link android.os.PowerManager#isInteractive} for details.
     * </p>
     * You <em>cannot</em> receive this through components declared in
     * manifests, only by explicitly registering for it with
     * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
     * Context.registerReceiver()}.
     *
     * <p class="note">This is a protected intent that can only be sent
     * by the system.
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_SCREEN_OFF = "android.intent.action.SCREEN_OFF";


public static BroadcastReceiver ScreenOffReceiver = new BroadcastReceiver() {  
	    
	    @Override  
	    public void onReceive(Context context, Intent intent) {  
	            //如果虚拟来电
	            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

	                    if (mScreenService.isSavingPicture) {
	                    		Tools.log(TAG, "FakeCall is comming  mScreenService.isSavingPicture=="+mScreenService.isSavingPicture);
	                    		return;
	                    }
	                    Tools.log(TAG, "Screen off  stopSelfBySelf");
	                    mScreenService.stopSelfBySelf(); 
	            }  
	    }  
	} ;












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值