我正在开发一个带小部件的手电筒应用程序。
当我通过小部件打开手电筒时,手电筒会亮起,但当我启动某些应用程序时,手电筒就会关闭。
这是为什么?为什么我的手电筒不能在后台运行?
如何防止这种情况发生?我希望手电筒只能由用户关闭,而不是系统自动关闭。
以下是我的小部件代码:
以下是我的小部件代码:
@Override
public void onReceive(Context context, Intent intent) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
myPref = PreferenceManager.getDefaultSharedPreferences(context);
if (AppGlobals.getIsFlashOn()) {
views.setImageViewResource(R.id.flashlight_widget_imageview,
R.drawable.light_on);
} else {
views.setImageViewResource(R.id.flashlight_widget_imageview,
R.drawable.light_off);
}
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context,
FlashlightWidgetProvider.class), views);
if (AppGlobals.getIsFlashOn()) {
if (getmCameraWidget() != null) {
flashOffWidget();
}
if (Flashlight.getmCameraActivity() != null) {
flashOffApp();
Flashlight.flashlight_button
.setBackgroundResource(R.drawable.light_on);
}
Flashlight.turnMotorolaOff();
isLightOn = false;
NotifyFlashlight(context, isLightOn);
} else {
try {
setmCameraWidget(Camera.open());
} catch (Exception e) {
e.printStackTrace();
}
if (getmCameraWidget() == null) {
} else {
setParamsWidget(getmCameraWidget().getParameters());
List<String> flashModes = getParamsWidget()
.getSupportedFlashModes();
if (flashModes == null) {
return;
} else {
if (count == 0) {
getParamsWidget().setFlashMode(
Parameters.FLASH_MODE_OFF);
getmCameraWidget().setParameters(getParamsWidget());
getmCameraWidget().startPreview();
AppGlobals.setIsFlashOn(true);
}
String flashMode = getParamsWidget().getFlashMode();
if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
getParamsWidget().setFlashMode(
Parameters.FLASH_MODE_TORCH);
getmCameraWidget().setParameters(getParamsWidget());
} else {
getParamsWidget().setFlashMode(
Parameters.FLASH_MODE_ON);
getmCameraWidget().setParameters(getParamsWidget());
try {
getmCameraWidget().autoFocus(
new AutoFocusCallback() {
public void onAutoFocus(
boolean success,
Camera camera) {
count = 1;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
AppGlobals.setIsFlashOn(true);
isLightOn = true;
NotifyFlashlight(context, isLightOn);
}
}
}
Flashlight.turnMotorolaOn();
}
}
private void flashOffApp() {
Flashlight.getmCameraActivity().stopPreview();
Flashlight.getmCameraActivity().release();
Flashlight.setmCameraActivity(null);
AppGlobals.setIsFlashOn(true);
count = 0;
}
private void flashOffWidget() {
FlashlightWidgetReceiver.getmCameraWidget().stopPreview();
FlashlightWidgetReceiver.getmCameraWidget().release();
FlashlightWidgetReceiver.setmCameraWidget(null);
AppGlobals.setIsFlashOn(false);
count = 0;
}
public static Camera getmCameraWidget() {
return mCameraWidget;
}
public static void setmCameraWidget(Camera mCameraWidget) {
FlashlightWidgetReceiver.mCameraWidget = mCameraWidget;
}
public static Parameters getParamsWidget() {
return paramsWidget;
}
public static void setParamsWidget(Parameters paramsWidgetSet) {
paramsWidget = paramsWidgetSet;
}
}
}