Android中来电时闪烁的闪光灯问题

时间:2019-03-14 06:35:48

标签: android broadcastreceiver

我有以下代码,我想在电话铃响时使闪光灯闪烁,并在收到或终止呼叫时关闭闪光灯。但它没有用。循环结束时,它将关闭闪光灯。我不知道问题出在哪里。

我已经检查了循环中的振铃状态。收到呼叫后,此循环仍然运行。请帮助我。

public class CallReceiver extends BroadcastReceiver {

Camera mCamera;
Camera.Parameters mParams;
private static final String TAG = "123";


@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getStringExtra(TelephonyManager.EXTRA_STATE) != null){
        String myString = "01010101010101010101010101010101010101010101010101010101010101010101010101010101";
        long blinkDelay = 150;
        for (int i = 0; i < myString.length(); i++) {
            if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                if (myString.charAt(i) == '0') {
                    turnOn();
                } else {
                    turnOff();
                }
                try {
                    Thread.sleep(blinkDelay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){
        Toast.makeText(context,"Call ended...",Toast.LENGTH_LONG).show();

    }else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){

    }
    }
}

public void turnOn() {

    mCamera = Camera.open();
    if (mCamera != null) {
        mParams = mCamera.getParameters();
        mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(mParams);
    }
}

public void turnOff() {
    if (mCamera != null) {
        mParams = mCamera.getParameters();
        if (mParams.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH)) {
            mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            mCamera.setParameters(mParams);
        }
    }
}

}

0 个答案:

没有答案