Android -- 距離感應器控制螢幕滅屏白屏

來源:互聯網
上載者:User

標籤:android   des   style   blog   http   color   strong   io   

許可權                                                                                            

<uses-permission android:name="android.permission.DEVICE_POWER"/><uses-permission android:name="android.permission.WAKE_LOCK"/>

第一個許可權加到xml檔案中時會報錯,但是我會很利索的去clean掉,實現步驟:找到eclipse中的菜單選項project>clean,選擇當前項目就ok。

代碼                                                                                            

public class MainActivity extends Activity implements SensorEventListener {    public static final String TAG = "SensorTest";    //調用距離感應器,控制螢幕    private SensorManager mManager;//感應器管理對象    //螢幕開關    private PowerManager localPowerManager = null;//電源管理對象    private PowerManager.WakeLock localWakeLock = null;//電源鎖    //TextView    private TextView tv;//基本上沒啥用    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                tv = (TextView) findViewById(R.id.tv);        mManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);        //擷取系統服務POWER_SERVICE,返回一個PowerManager對象        localPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);        //擷取PowerManager.WakeLock對象,後面的參數|表示同時傳入兩個值,最後的是LogCat裡用的Tag         localWakeLock = this.localPowerManager.newWakeLock(32, "MyPower");//第一個參數為電源鎖層級,第二個是日誌tag    }    public void onResume(){        super.onResume();        mManager.registerListener(this, mManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),// 距離感應器                SensorManager.SENSOR_DELAY_NORMAL);//註冊感應器,第一個參數為距離監聽器,第二個是感應器類型,第三個是延遲類型    }    public void onStop(){        super.onStop();        Log.d(TAG,"on stop");    }    public void onDestroy(){        super.onDestroy();        Log.d(TAG,"on destroy");        if(mManager != null){            localWakeLock.release();//釋放電源鎖,如果不釋放finish這個acitivity後仍然會有自動鎖屏的效果,不信可以試一試            mManager.unregisterListener(this);//登出感應器監聽        }    }    @Override    public void onSensorChanged(SensorEvent event) {        float[] its = event.values;        //Log.d(TAG,"its array:"+its+"sensor type :"+event.sensor.getType()+" proximity type:"+Sensor.TYPE_PROXIMITY);        if (its != null && event.sensor.getType() == Sensor.TYPE_PROXIMITY) {            System.out.println("its[0]:" + its[0]);            tv.setText(its[0]+"");            //經過測試,當手貼近距離感應器的時候its[0]傳回值為0.0,當手離開時返回1.0            if (its[0] == 0.0) {// 貼近手機                System.out.println("hands up");                Log.d(TAG,"hands up in calling activity");                if (localWakeLock.isHeld()) {                    return;                } else{                    localWakeLock.acquire();// 申請裝置電源鎖                }            } else {// 遠離手機                System.out.println("hands moved");                Log.d(TAG,"hands moved in calling activity");                if (localWakeLock.isHeld()) {                    return;                } else{                    localWakeLock.setReferenceCounted(false);                    localWakeLock.release(); // 釋放裝置電源鎖                }            }        }    }    @Override    public void onAccuracyChanged(Sensor sensor, int accuracy) {        // TODO 自動產生的方法存根    }}

需要特別注意的是,在onDestroy()函數裡需要把對象鎖釋放掉(localWakeLock.release()),否則這個功能會一直存在知道你清楚應用資料或者刪除應用,我就為這個問題困擾了好久。為此我特意在原始碼例子裡做了一個跳轉頁面,加了log。

這裡我們執行個體化了兩個管理對象,一個是感應器管理對象:SensorManager;另一個是 電源管理對象:localPowerManager。

感應器管理對象負責收集收集與物體之間的距離資料,電源管理對象通過判斷感應器收集到資料對螢幕的滅屏和白屏做處理。 

我是天王蓋地虎的分割線                                                                 

原始碼:http://pan.baidu.com/s/1dD1Qx01

距離感應器.zip

 

 

參考:http://blog.csdn.net/luozhi3527/article/details/9999901

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.