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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:57:59+00:00 2026-06-16T20:57:59+00:00

I have setup a widget like this ToggleWidget.java public class ToggleWidget extends AppWidgetProvider {

  • 0

I have setup a widget like this

ToggleWidget.java

    public class ToggleWidget extends AppWidgetProvider {

    static RemoteViews remoteViews;
    boolean status = false;
    static int appWidgetId;

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

        final int N = appWidgetIds.length;



        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            appWidgetId = appWidgetIds[i];

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.toggle_widget);
            Intent intent = new Intent(context.getApplicationContext(), WakeService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
            PendingIntent pendingIntent = PendingIntent.getService(
                    context.getApplicationContext(), 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.bulb_widget, pendingIntent);




            if (WakeService.isAwake) {
                remoteViews.setImageViewResource(R.id.bulb_widget, R.drawable.bulb_on);

            }
            else {
                remoteViews.setImageViewResource(R.id.bulb_widget, R.drawable.bulb_off);

            }

         // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);


        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);

    }




}

How can i save the ID of every widget i create and delete those that i’ve deleted with sharedPreferences?

Also from a service i’d like to update the widget like this

private void updateWidget(){

    Intent intent = new Intent(this,ToggleWidget.class);
    intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    int[] ids = {ToggleWidget.appWidgetId};
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
    sendBroadcast(intent);
}

How can i retrieve the saved IDs from the service to make the update?

  • 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-16T20:58:01+00:00Added an answer on June 16, 2026 at 8:58 pm

    Take a look at android’s Data Storage options. Here is how you could use SharedPreferences…

    public class MyService extends Service{
    
                //Class used to store data
                private SharedPreferences preferences;
                private int[] ids;
                private int numIds;
    
                public void retreiveIds(){
                    preferences = PreferenceManager.getDefaultSharedPreferences(this);
                    numIds = preferences.getInt("num",0);
                    ids = new int[numIds];
                    for(int i=0;i<numIds;i++){
                        ids[i] = preferences.getInt("ID"+i,0);
                    }
                }
    
                public void saveIds(){
                    SharedPreferences.Editor ed = preferences.edit();
                    numIds = ids.length;
    
                    //iterate through array and save each id
                    for(int i=0;i<numIds;i++){
                        ed.putInt("ID"+i,ids[i]);
                    }
    
                    ed.putInt("num",numIds);
                    ed.commit();
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a div whose css looks like this: .class { overflow:auto; resize:
I have a public SHOUTcast on my site and I've setup a widget so
I have setup an app project and a static project in a workspace in
This is weird. I have a virtual directory setup for an MVC3 application called
I'm following this guide to add an actionbar SearchView widget to my activity: http://developer.android.com/training/search/setup.html
I would like to have an input 'submit' calling a method from the Java
I have a type that is derived from an entity: public class WidgetB :
First off I'd like to say this is the only java experience I've had.
In my API, I'd like to have routes like GET /api/v1/widgets/1,2,3 and GET /api/v1/widgets/best-widget,major-widget,bob-the-widget
I have setup airbrake in my app but it doesn't seem to log any

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.