The flashlight turns off when you start other applications. Android

I am working on a Flashlight application with Widget. When I turn on the flashlight with the Widget flashlight, and when I launch some application, the flashlight turns off. Why is this happening? Why is my flashlight not working in the background? How can I prevent this? I want the flashlight to be turned off only by the user, not the system.

This is my widget code:

@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; } } } 
+2
source share
1 answer

Here is all the code to run Falsh in the background. all you need to put your code in the service. then start your service with your core business.

Here is the class of service:

 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; } 

Remember to add this to your manifest:

 <service android:name=".ServiceFlash" android:exported="false"/> 

Your activity may be this: the public class AppActivity extends the action {private boolean isFlashOn = false; private camera; private button buttons;

 @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 a widget class like this (try putting this code inside the onReceive method of the widget class):

  // Create intent Intent serviceIntent = new Intent(context, mService.class); // start service context.startService(serviceIntent); 

Enjoy! ..

+1
source

Source: https://habr.com/ru/post/1204044/


All Articles