android源代碼:鎖屏滅屏來電接通後立即息屏、通話過程中息屏對方掛斷不亮屏

在Android源碼實際開發中遇到這樣兩個問題:鎖屏滅屏來電接通後立即息屏、通話過程中息屏對方掛斷不亮屏;由於喚醒亮屏的問題很多地方都用到,不想輕意修改其源碼代碼,怕引出其它更多的問題。
所以再解決這兩個問題時,采用一個直接的辦法,就是:鎖屏滅屏來電接通後立即息屏,那麼就點亮屏,幾秒之後再釋放;如果:通話過程中息屏對方掛斷不亮屏,那麼就點屏,幾秒之後再釋放;
   這種亮屏的方法,可以放在電話監聽的三個狀態的 Call_STATE_IDLE與Call_State_OFFHooK中來做處理。
   在這兩個狀態中會遇到這樣一個問題:明明做一鎖的釋放,但屏還是暗亮點。一點不會黑屏下去不會待機。其實問題還時沒有釋放所有的亮屏鎖,如果在狀態Call_STATE_IDLE與Call_State_OFFHooK 中將鎖new 多次,則每次產生的鎖也是不一樣的,即使時同一個鎖名,但下面不會認同是同一個鎖,故鎖沒有釋放,不會息屏。所以在Call_STATE_IDLE與Call_State_OFFHooK中一定要限定new出來的鎖隻進行一次,並釋放對應的鎖。這樣就可達到亮屏後,幾秒鐘(通過message來進行控制時間)再息屏。
   代碼初記如下:
[java]
TelephonyManager mTelephonyMgr = (TelephonyManager) this 
                .getSystemService(Context.TELEPHONY_SERVICE); 
     
        mTelephonyMgr.listen(new TeleListener(), 
                PhoneStateListener.LISTEN_CALL_STATE); 
//============================= 
 
    //Add by kylin 2011.11.22 begin 
     
    Handler myHandler = new Handler(){ 
        @Override 
        public void handleMessage(Message msg) { 
            // TODO Auto-generated method stub 
            super.handleMessage(msg); 
            if (null != wl) { 
                wl.release(); 
            } 
        } 
         
    }; 
   
    Handler myHandler2 = new Handler(){ 
        @Override 
        public void handleMessage(Message msg2) { 
            // TODO Auto-generated method stub 
            super.handleMessage(msg2); 
            if (null != wl2) { 
                wl2.release(); 
            } 
        } 
         
    }; 
     
    //end 
 
//================================================ 
 
class TeleListener extends PhoneStateListener { 
 
        @Override 
        public void onCallStateChanged(int state, String incomingNumber) { 
            super.onCallStateChanged(state, incomingNumber); 
            switch (state) { 
            case TelephonyManager.CALL_STATE_IDLE: { 
                //Add by kylin 2011.10.21 
                    
                String getElapsedTimeTemp=mCallCard.getElapsedTime().toString(); 
                 
                 
            if(getElapsedTimeTemp!=null && !getElapsedTimeTemp.trim().equals("") && PhoneApp.getInstance().isVTCall()==false){ 
//              if(PhoneApp.getInstance().isVTCall()==false){ 
                     getToast(); 
                    }    
  
                pm.setBtnBacklight(false); 
     
                mFlag=false;  
                 
                mFlag2 = false; 
                   
 
                //end 
                break; 
            } 
 
            case TelephonyManager.CALL_STATE_OFFHOOK: { 
                mFlag=true; 
                pm.setBtnBacklight(true); 
                 
                if(!mFlag2){ 
                    pm2=(PowerManager) getSystemService(Context.POWER_SERVICE); 
                    wl2 = pm2.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "InCallScreen"); 
//                  wl2.setReferenceCounted(false); 
                    wl2.acquire(); 
 
                    Message msg2 = new Message(); 
                    msg2.what = 200; 
                    myHandler2.sendMessageDelayed(msg2, 5000);   
                    mFlag2 = true; 
                } 
                 
                break; 
            } 
 
            case TelephonyManager.CALL_STATE_RINGING: { 
                pm.setBtnBacklight(true); 
                break; 
            } 
            default: 
                break; 
            } 
        } 
 
    } 
//================================== 
 
private void getToast(){ 
         
         if(mFlag==true){    
             pm=(PowerManager) getSystemService(Context.POWER_SERVICE); 
             wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "InCallScreen"); 
             wl.acquire(); 
             
             mFlag=false;    
              
             Message msg = new Message(); 
             msg.what = 100; 
             myHandler.sendMessageDelayed(msg, 2000); 
             
            // wl.release();  
         } 
    } 

摘自 Kylin麒麟

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。