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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:41:53+00:00 2026-05-30T23:41:53+00:00

I’ve an application that has a widget already and have a service which updates

  • 0

I’ve an application that has a widget already and have a service which updates that widget in every 5 seconds.

Here is my AppWidgetProvider:

public class MyWidget extends AppWidgetProvider {

    PendingIntent service = null;
    AlarmManager alarmManager;

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

        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final Intent i = new Intent(context, MyService.class);

        if(service == null) {
            service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
        }

        alarmManager.setRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime(), 1000*5, service);
    }

    @Override
    public void onDisabled(Context context) {
        Log.d("LogTag", "onDisabled");
        alarmManager.cancel(service);
        service.cancel();
    }
}

And my Service:

public class MyService extends Service {

    int b = 5;

    @Override
    public void onCreate() { super.onCreate(); }

    public int onStartCommand(Intent intent, int flags, int startId) {
        this.buildUpdate();
        return 0;
    }   

    private void buildUpdate() {
        Log.d("AAA", "Update is coming");

        RemoteViews rmViews = new RemoteViews(getPackageName(), R.layout.widget_layout);

        rmViews.setTextViewText(R.id.hello, "Iterator: " + Integer.toString(b));

        ComponentName thisWidget = new ComponentName(this, MyWidget.class);

        AppWidgetManager manager = AppWidgetManager.getInstance(this);

        manager.updateAppWidget(thisWidget, rmViews);

        b++;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

Service and other things are fine. But still I’ve couple questions about all these stuff:

  1. What’s for SystemClock.elapsedRealtime() which is a parameter in setRepeating method? Does it point when to start?

  2. When I try to delete widget, I got some errors (on below) and unfortunately AlarmManager still works not cancel. What am I missing?

  3. I need to do some network operations before widget has been initialized (or called). Widget should has some data from server. Should I use AsyncTask or what?

When I try to delete widget, getting:

03-04 23:39:51.428: E/AndroidRuntime(5646): Uncaught handler: thread main exiting due to uncaught exception
03-04 23:39:51.438: E/AndroidRuntime(5646): java.lang.RuntimeException: Unable to start receiver com.example.widget.MyWidget: java.lang.NullPointerException
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.app.ActivityThread.access$3100(ActivityThread.java:119)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.os.Looper.loop(Looper.java:123)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at java.lang.reflect.Method.invoke(Method.java:521)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at dalvik.system.NativeStart.main(Native Method)
03-04 23:39:51.438: E/AndroidRuntime(5646): Caused by: java.lang.NullPointerException
03-04 23:39:51.438: E/AndroidRuntime(5646):     at com.example.widget.MyWidget(MyWidget.java:33)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:76)
03-04 23:39:51.438: E/AndroidRuntime(5646):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2637)
03-04 23:39:51.438: E/AndroidRuntime(5646):     ... 10 more
  • 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-30T23:41:55+00:00Added an answer on May 30, 2026 at 11:41 pm
    1. SystemClock.elapsedRealTime() as you have it is currently in the triggerAtTime parameter, meaning that any number you put there is the time that you want the alarm to go off. elapsedRealtime() is counted in milliseconds since the system was booted, including deep sleep. This clock should be used when measuring time intervals that may span periods of system sleep.

    2. In you onDisabled method, you need to recreate the entire PendingIntent. My guess is it is null there because it doesn’t actually exist when you call onDisabled, but only exists in the onUpdate. So recreate the service variable PendingIntent exactly the same way as you do before and then call cancel on it as you already are.

    3. AsyncTasks are good for things like that. An AsyncTask spawns a managed thread in the background and does work while the UI thread keeps going, then can update the UI thread when done. So if you have lots of heavy networking to do, an AsyncTask will handle that beautifully. The docs on AsyncTask are very useful. Find them here. There are many options for this depending on what you are doing, so you’d have to explain your issue on this third point in a little more detail to get a more tailored answer.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

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.