I am working on Flashlight app with Widget. When I turn on Flashlight with Widget flashlight is on, and when I start some app, the flashlight turns off. Why is this happening? Why can't my Flashlight run in background? How can I prevent this? I want Flashlight to be turned off only by user not the system.
This is my code for widget:
@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;
}
}
}
Here is the whole code to run Falsh in background. all you need to put your code in a service. then start your service from your main activity.
Here is the service class:
public class ServiceFlash extends Service {
private boolean isFlashOn = false;
private Camera camera;
Context context ;
PackageManager pm;
@Override
public void onCreate() {
// TODO Auto-generated method stub
context = getApplicationContext();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
Toast.makeText(getApplicationContext(),
"Your device doesn't have camera!", Toast.LENGTH_SHORT)
.show();
return 0;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
// turn flash on
if (isFlashOn) {
Log.i("info", "torch is turned off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isFlashOn = false;
} else {
Log.i("info", "torch is turned on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
Don't forget add this to your manifest:
<service
android:name=".ServiceFlash"
android:exported="false"/>
Your activity maybe like this: public class AppActivity extends Activity { private boolean isFlashOn = false; private Camera camera; private Button button;
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent front_translucent = new Intent(this, ServiceFlash.class);
startService(front_translucent);
}
}
You can start your service from widget class like this (try to put this code inside onReceive method of widget class):
// Create intent
Intent serviceIntent = new Intent(context, mService.class);
// start service
context.startService(serviceIntent);
Enjoy..!
来源:https://stackoverflow.com/questions/25709391/flashlight-turns-off-when-other-apps-are-started-android