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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:42:05+00:00 2026-06-07T21:42:05+00:00

My app has a widget that lights the LED Flash when we click on

  • 0

My app has a widget that lights the LED Flash when we click on it and swhitch it off when we click again on it.

Here is the code (thanks to Kartik from this post):

WidgetProvider.java

public class WidgetProvider extends AppWidgetProvider {

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

        Intent receiver = new Intent(context, WidgetReceiver.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);

        views.setOnClickPendingIntent(R.id.imageButtonWidget, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, views);

    }
}

WidgetReceiver.java

public class WidgetReceiver extends BroadcastReceiver {
    public static boolean isLightOn = false;
    public static Camera camera;

    @Override
    public void onReceive(Context context, Intent intent) {
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);

        if (isLightOn) {
            views.setImageViewResource(R.id.imageButtonWidget,
                    R.drawable.widget_lamp_button_default);
        } else {
            views.setImageViewResource(R.id.imageButtonWidget,
                    R.drawable.widget_lamp_button_checked);
        }


        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
        appWidgetManager.updateAppWidget(new ComponentName(context,
                WidgetProvider.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();
                }
            }
        }
    }
}

Now I would like to switch the widget off from inside my app . With this code in the main activity of my app, I can release the camera taken by the widget :

MainActivity.java

//Stop widget camera
        if (WidgetReceiver.isLightOn){
        Camera a = WidgetReceiver.camera;
        a.stopPreview();
        a.release();
        a = null;
        WidgetReceiver.isLightOn=false;}

But the problem is that the widget is still set to the checked drawable (R.drawable.widget_lamp_button_checked). So the FlashLight is well turned off but I still need to force the widget to set its drawable to the unchecked one (R.drawable.widget_lamp_button_default).

How can I do this ?

Edit : Problem Solved

WidgetProvider.java

   public class WidgetProvider extends AppWidgetProvider {

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

             // Set widget's drawable to unchecked

            RemoteViews views2 = new RemoteViews(context.getPackageName(),
                    R.layout.widget);
            AppWidgetManager mManager = AppWidgetManager.getInstance(MainActivity
                    .getContext());
            ComponentName cn = new ComponentName(MainActivity.getContext(),
                    WidgetProvider.class);
            views2.setImageViewResource(R.id.imageButtonWidget,
                    R.drawable.widget_lamp_button_default);
            mManager.updateAppWidget(cn, views2);


            // Widget OnClick Behavior

            Intent receiver = new Intent(context, WidgetReceiver.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);

            views.setOnClickPendingIntent(R.id.imageButtonWidget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetIds, views);

        }
    }

WidgetReceiver.java -> kept the same

MainActivity.java

    public class MainActivity extends Activity {
        private static Context mContext;

        public static Context getContext() {
            return mContext;
        }


       @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mContext = this;


                   //Stop widget camera

                    if (WidgetReceiver.isLightOn){
                    Camera a = WidgetReceiver.camera;
                    a.stopPreview();
                    a.release();
                    a = null;
                    WidgetReceiver.isLightOn=false;}


        // Fire Widget's update with Intent 

        Intent intent = new Intent(this, WidgetProvider.class);
                intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
                // Use an array and EXTRA_APPWIDGET_IDS instead of
                // AppWidgetManager.EXTRA_APPWIDGET_ID,
                // since it seems the onUpdate() is only fired on that:
                int[] ids = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
                sendBroadcast(intent);

}
  • 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-07T21:42:07+00:00Added an answer on June 7, 2026 at 9:42 pm

    Send a broadcast to your AppWidgetProvider and update your widget by using updateAppWidget method of AppWidgetManager class,in onReceive() method of AppWidgetProvider:

    @Override
    public void onReceive(Context context, Intent intent) {
    
    
        ...
        views = new RemoteViews(context.getPackageName(),
        R.layout.yourwidgetlayout);
        AppWidgetManager mManager = AppWidgetManager.getInstance(App
        .getContext());
        ComponentName cn = new ComponentName(App.getContext(),
        YourAppWidgetProvider.class);
        //change your views,here I change text of text view witch it's id is "widgettextview" 
        views.setTextViewText(R.id.widgettextview, "lastWord");
        mManager.updateAppWidget(cn, views);
        ...
    }     
    

    Here is App definition:

    public class App extends Application implements OnInitListener {
    
        private static Context mContext;
    
        public void onCreate() {
            super.onCreate();
            mContext = this;
            }
        public static Context getContext() {
            return mContext;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app has a GtkFileChooserButton that you can use to open a chooser widget
I am trying to reproduce the account chooser widget that the GMail app has
My App widget has a TextView that it's text changes in interaction with user.Now
I want to design simple app widget which has two textview and two button
My app has an options menu that is available in almost all Activities, which
I've developed an Application (called Instant Buttons) and the app has a widget feature.
i want to create a widget app that when added will start ConfigurationActivity which
I created an Android app widget that displays some text. By pressing on the
I'm trying to develop an app that has many videos embedded in it, and
I have an Android widget that has a configure activity. I also have an

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.