我正在开发一个Android应用程序,该应用程序会在来电和短信上闪烁,但是问题是,第一次闪光灯在来电或短信上开始闪烁,但是第二次它引发了“无法连接到相机服务”
这是我的源代码
public class CallReceiver extends PhonecallReceiver {
private static Camera camera =null;
private boolean isFlashOn;
Camera.Parameters params;
private boolean isBlinking = false;
String myString = "010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101";
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onIncomingCallStarted(Context ctx, String number, Date start) {
isBlinking = true;
setFlashBlinking(ctx);
}
@Override
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
Log.d("arsalan", "Out going call started");
turnOffFlash();
/* myString = "1";
onIncomingCallStarted(ctx, number, start);*/
}
@Override
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
Log.d("arsalan", "incoming call is ended");
myString = "1";
turnOffFlash();
//onIncomingCallStarted(ctx, number, start);
}
@Override
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
Log.d("arsalan", "on out going call is ended");
}
@Override
protected void onMissedCall(Context ctx, String number, Date start) {
Log.d("arsalan", "On Missed call");
turnOffFlash();
// onIncomingCallStarted(ctx, number, start);
}
private void turnOnFlash() {
if (!isFlashOn) {
camera = Camera.open();
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
}
private void turnOffFlash() {
if (isFlashOn) {
// params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
camera.release();
camera = null;
isFlashOn = false;
}
}
public void setFlashBlinking(Context context) {
MyPreference preference = MyPreference.getInstance(context);
//checking that flash light is available or not
if (FlashLight.hasFlashLight(context)) {
long blinkDelay = preference.getCallBlinkingDelay();
for (int i = 0; i < myString.length(); i++) {
if (isBlinking == false) {
break;
}
if (myString.charAt(i) == '0') {
turnOnFlash();
} else {
turnOffFlash();
}
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void setFlashBlinking(boolean blinking) {
isBlinking = blinking;
}
}
PhonecallReceiver是扩展广播接收器的抽象类,用于检测传入呼叫
setFlashBlinking()方法用于打开/关闭闪光灯,而 setFlashBlinking(boolean isBlinking)方法(用于停止闪烁)用于在通话时停止循环。接听或未接电话。
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.creativelabs.flashoncallandsms">
]
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
/>
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BroadCastReceiver.CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver android:name=".BroadCastReceiver.SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
如何解决此问题“无法连接到相机服务”
感谢您的帮助:)