[英]How to make LED flashlight to blink on call receive?
我正在嘗試讓設備收到來電時使LED閃光燈閃爍。 這就是在服務類中的做法。
public class MyService extends Service {
Camera cam = null;
boolean offhook = false;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String state = intent.getStringExtra("state");
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) && !offhook) {
String myString = "01010101010101010101";
long blinkDelay = 50;
for (int i = 0; i < myString.length(); i++) {
if (myString.charAt(i) == '0') {
// params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
cam = Camera.open();
Camera.Parameters p = cam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
} else {
// params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
offhook = true;
if (cam != null) {
cam.stopPreview();
cam.release();
cam = null;
}
this.stopSelf();
}
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
if (!offhook) {
if (cam != null) {
cam.release();
cam = null;
}
this.stopSelf();
} else {
offhook = false;
}
}
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
offhook = true;
if (cam != null) {
cam.stopPreview();
cam.release();
cam = null;
}
this.stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
if (cam != null) {
cam.release();
cam = null;
}
super.onDestroy();
}
}
但是,即使在我參加或拒絕通話后,眨眼也不會停止,因為它處於for循環中。 我該怎么做?
不要在onStartCommand中編寫代碼,因為此方法將在主線程上執行,因此您可能會獲得ANR。 要停止LED閃爍,請在應答或拒絕呼叫時中斷線程,並放置for循環中斷以退出循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.