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 6863127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:44:40+00:00 2026-05-27T02:44:40+00:00

I am developing a widget for turning on/off camera led of phone. I have

  • 0

I am developing a widget for turning on/off camera led of phone.

I have made a widget that can work like toggle button (on/off).

Behavior is like follows : Sometimes the led light remains on when i enable the widget.
But it doesnot turn on/off the camera led but it changes the icon.

I am not able to figure out whats the actual problem.

The same thing works fine in Activity (Torch Light Application).

Can anyone please explain me how can i solve my problem ?

Where i am going wrong ?

You can look at the code below that i have done so far

onUpdate method

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

         //super.onUpdate(context, appWidgetManager, appWidgetIds);

        remoteViews = new RemoteViews( context.getPackageName(), R.layout.widgetlayout);
        watchWidget = new ComponentName( context, FlashLightWidget.class );

        Intent intentClick = new Intent(context,FlashLightWidget.class);
        intentClick.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, ""+appWidgetIds[0]);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[0],intentClick, 0);
        remoteViews.setOnClickPendingIntent(R.id.myToggleWidget, pendingIntent);
        appWidgetManager.updateAppWidget( watchWidget, remoteViews );
        ctx=context;      
    }

onReceive method is as follows :

@Override

    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        remoteViews = new RemoteViews( context.getPackageName(), R.layout.widgetlayout);
        if (intent.getAction()==null) {
            Bundle extras = intent.getExtras();
            if(extras!=null) {
                 if(status)
                    {
                        status=false;
                        remoteViews.setImageViewResource(R.id.myToggleWidget, R.drawable.shutdown1);
                        processOnClick();
                        Toast.makeText(context,"Status==false-onclick",Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        status = true;
                        remoteViews.setImageViewResource(R.id.myToggleWidget, R.drawable.shutdown2);
                        processOffClick();
                        Toast.makeText(context,"Status==true--Ofclick",Toast.LENGTH_SHORT).show();
                    }
                }

                watchWidget = new ComponentName( context, FlashLightWidget.class );

                (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
           }
        }
  }

processOffClick method

private void processOffClick() {

        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();      
            mCamera = null;
        }
    }

processOnClick method

private void processOnClick() {

    if(mCamera==null)
    {
        try {
            mCamera = Camera.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (mCamera != null) {

        Parameters params = mCamera.getParameters();
        List<String> flashModes = params.getSupportedFlashModes();

        if (flashModes == null) {
            return;
        } else {

                params.setFlashMode(Parameters.FLASH_MODE_OFF);
                mCamera.setParameters(params);
                mCamera.startPreview();

            String flashMode = params.getFlashMode();

            if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {

                if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
                    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    mCamera.setParameters(params);

                } 

            }
        }
    } else if (mCamera == null) {
        //Toast.makeText(ctx, "Camera not found", Toast.LENGTH_LONG).show();
        return;
    }
}
  • 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-05-27T02:44:41+00:00Added an answer on May 27, 2026 at 2:44 am

    After a long time, I got free to solve this problem.

    Here is what I did.

    FlashlightWidgetProvider class :

    public class FlashlightWidgetProvider extends AppWidgetProvider {
    
            @Override
            public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                            int[] appWidgetIds) {
    
                    Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
                    receiver.setAction("COM_FLASHLIGHT");
                    receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);
    
                    RemoteViews views = new RemoteViews(context.getPackageName(),
                                    R.layout.widget_layout);
                    views.setOnClickPendingIntent(R.id.button, pendingIntent);
    
                    appWidgetManager.updateAppWidget(appWidgetIds, views);
    
            }
    }
    

    and BroadcastReceiver for FlashlightWidgetReceiver :

    public class FlashlightWidgetReceiver extends BroadcastReceiver {
                private static boolean isLightOn = false;
                private static Camera camera;
    
                @Override
                public void onReceive(Context context, Intent intent) {
                        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    
                        if(isLightOn) {
                                views.setImageViewResource(R.id.button, R.drawable.off);
                        } else {
                                views.setImageViewResource(R.id.button, R.drawable.on);
                        }
    
                        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                        appWidgetManager.updateAppWidget(new ComponentName(context,     FlashlightWidgetProvider.class),
                                                                                         views);
    
                        if (isLightOn) {
                                if (camera != null) {
                                        camera.stopPreview();
                                        camera.release();
                                        camera = null;
                                        isLightOn = false;
                                }
    
                        } else {
                                // Open the default i.e. the first rear facing camera.
                                camera = Camera.open();
    
                                if(camera == null) {
                                        Toast.makeText(context, R.string.no_camera, Toast.LENGTH_SHORT).show();
                                } else {
                                        // Set the torch flash mode
                                        Parameters param = camera.getParameters();
                                        param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                                        try {
                                                camera.setParameters(param);
                                                camera.startPreview();
                                                isLightOn = true;
                                        } catch (Exception e) {
                                                Toast.makeText(context, R.string.no_flash, Toast.LENGTH_SHORT).show();
                                        }
                                }
                        }
                }
        }
    

    Permission required in Manifest.xml file :

    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    

    Also register receivers in Manifest.xml file :

    <receiver android:name=".FlashlightWidgetProvider" android:icon="@drawable/on" android:label="@string/app_name">
             <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
             </intent-filter>
    
             <meta-data android:name="android.appwidget.provider"
                            android:resource="@xml/flashlight_appwidget_info" />
    </receiver>
    
    <receiver android:name="FlashlightWidgetReceiver">
            <intent-filter>
                   <action android:name="COM_FLASHLIGHT"></action>
            </intent-filter>
     </receiver>
    

    Important Note : This code works perfect if your phone has FLASH_MODE_TORCH supported.

    I have tested in Samsung Galaxy Ace 2.2.1 & 2.3.3. The code is not working because that device has no FLASH_MODE_TORCH.

    Works fine in HTC Salsa, Wildfire..

    If anyone can test and post results here, it would be best.

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

Sidebar

Related Questions

I'm currently developing a widget that can be embedded within a page. However, when
i am developing a widget. For that i have to add images to the
I have been developing a jquery widget that extends ui.mouse. The widget needs to
In short I'm developing a 'widget' in flash that I would like to implement
I am busy developing a Firefox extension and I have a Widget that opens
I have made a widget that is working fine on its part and also
I have a widget in an ASP.NET project that I'm developing for my job.
I have difficulties developing 4x1 widget for Android. This is the appprovider <?xml version=1.0
This is the scenario: I'm developing a dropdown widget in Jquery. I have to
i'm developing a widget that is fetching data from the internet via ajax and

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.