Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:32:14+00:00 2026-06-15T18:32:14+00:00

I want to make an app without UI, which will toggle the flashlight on/off

  • 0

I want to make an app without UI, which will toggle the flashlight on/off when pressing the app’s icon.

I’ve tried to make a transparent activity which partially works (works when flashlight is off, but when is on and use the app’ s icon to turn it off, it turns off but the app crashes with an error dialog).

I’m thinking that the above is not a good way to achive what I want, and probably need to use a service, but I’m new in developing and I don’t really know how to code a service.

So, I’m asking you to guide me on which is the best approach for making a toggle flashlight on/off using the app’ s icon shortcut, and maybe to give a base example.
Thanks in advance.

Here is the code:

public class Flashlight extends Activity {

private boolean isLighOn = false;
private Camera camera;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flashlight);

    Context context = this;
    PackageManager pm = context.getPackageManager();

    // if device support camera?
    if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Log.e("err", "Device has no camera!");
        return;
    }

    camera = Camera.open();
    final Parameters p = camera.getParameters();

    if (isLighOn) {

        Log.i("info", "torch is turn off!");

        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
        isLighOn = false;

    } else {

        Log.i("info", "torch is turn on!");

        p.setFlashMode(Parameters.FLASH_MODE_TORCH);

        camera.setParameters(p);
        camera.startPreview();
        isLighOn = true;

    }
    finish();

          };

      @Override
      public void onPause() {
      super.onPause();
  bundle.putBoolean("toggleFlashlight", isLighOn);
     }

     public void onResume() {
 super.onResume();
     }

@Override
protected void onStop() {
    super.onStop();

   }
  }  
 }

And here is the logcat error:

E/QualcommCamera(  116): Qint android::get_camera_info(int, camera_info*): E
I/QualcommCameraHardware(  116): Found a matching camera info for ID 0
I/QualcommCameraHardware(  116): HAL_getCameraInfo: orientation = 90
I/QualcommCameraHardware(  116): HAL_getCameraInfo: modes supported = 5
W/CameraService(  116): CameraService::connect X (pid 6147) rejected (existing c
lient).
W/dalvikvm( 6147): threadid=1: thread exiting with uncaught exception (group=0x4
0c911f8)
E/AndroidRuntime( 6147): FATAL EXCEPTION: main
E/AndroidRuntime( 6147): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{com.myprojects.lightsonoff/com.myprojects.lightsonoff.Flashlight}: j
ava.lang.RuntimeException: Fail to connect to camera service
E/AndroidRuntime( 6147):        at android.app.ActivityThread.performLaunchActiv.....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:32:16+00:00Added an answer on June 15, 2026 at 6:32 pm

    I couldn’t make it work with this approach, so I created a service in which I started flashlight as a service. Then from the transparent activity I created an if statement which will start the sercvice if not running by checking a blobal boolean flag(which is included in service class), or stop it if already run.
    The only problem now was that the system kill the flashlight service very easy, so I inclued the startForeground method like this:

    Notification fakenote = new Notification( 0, null, System.currentTimeMillis() );
        fakenote.flags |= Notification.FLAG_NO_CLEAR;
        startForeground( 2, fakenote );
    

    Also I put stopForeground(true) in onDestroy method.
    Now seems that works fine.
    Is there any way to prevent system completely from killing the service, so it can be killed only by the app, or this is the best can be done?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to make an app which can add the file properties of a
I want to make a simple app, where a UIWebView with custom content will
I want to make an app which takes a video from the camera, adds
I am new android app developer i want make app for tablets and phone
I want to make an app that has a view that moves randomly or
I want to make an app that can receive broadcast when other apps on
I want to make an App Engine connected Android app, and I'm beginning with
HI all I want to make one app for iPhone 2.2.* and for version
I want to make an Android app by using ZXing . Can anyone tell
i want to make a php app that let people submit photos/videos/sounds Now, everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.