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

  • Home
  • SEARCH
  • 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 6835631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:15:19+00:00 2026-05-26T23:15:19+00:00

I’m trying to create my first AppWidget using the AlarmManager class so that I

  • 0

I’m trying to create my first AppWidget using the AlarmManager class so that I can update more frequently than every 30 minutes. I followed this tutorial as a basis for setting up my widget, but for some reason I cannot get the updates to begin properly. It appears as I am never receiving any APPWIDGET_ENABLED intents, which would fire off the onEnabled event callback in my AppWidgetProvider.

Here is the manifest definition for my AppWidgetProvider:

    <receiver 
       android:name="com.myapp.android.appwidget.MarketTimingAppWidgetProvider"
       android:label="@string/appwidget_markettiming_label">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
            <action android:name="@string/appwidget_markettiming_updateintent" />           
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/appwidget_markettiming_info" />
    </receiver>

Here is the code for my AppWidgetProvider:

public class MarketTimingAppWidgetProvider extends AppWidgetProvider {

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

    final int N = appWidgetIds.length;

    Log.d("myLogger", "onUpdate");
    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {

        int appWidgetId = appWidgetIds[i];
        Log.d("myLogger", "Updating Widget: " + appWidgetId);
        updateWidget(context, appWidgetManager, appWidgetId);

    }

}

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);

    Log.d("myLogger", "onEnabled running");
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 1);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 
                              1000, createClockIntent(context));   
}

public void onDisabled(Context context) {
    super.onDisabled(context);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(createClockIntent(context));
}

public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    Log.d("myLogger", "Intent Received " + intent.getAction());
    String widgetIntent = context.getResources().getString(R.string.appwidget_markettiming_updateintent);

    // This code fires when my custom intent is received
    if(widgetIntent.equals(intent.getAction())) {
        ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
        for(int appWidgetId: ids) {
            updateWidget(context, appWidgetManager, appWidgetId);
        }
    }

}

private void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_markettiming);
    views.setTextViewText(R.id.widget_text, "Update: " +  
            DateFormat.getDateTimeInstance(
                    DateFormat.LONG, DateFormat.LONG).format(new Date()));

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

}

private PendingIntent createClockIntent(Context context) {
    String updateIntent = context.getResources().getString(R.string.appwidget_markettiming_updateintent);
    Log.d("myLogger", "my intent: " + updateIntent);
    Intent intent = new Intent(updateIntent);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingIntent;
}

}

When I look in LogCat the only intent that is ever recieved by my onReceive method is the initial APPWIDGET_UPDATE intent, and the only callback ever executed is the onUpdate callback. I’ve tried including the APPWIDGET_ENABLED intent in my appwidget intent-filter (although the docs tell me that this should be automatically received by my widget). It didn’t work. Is there just something I’m missing here?

  • 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-26T23:15:20+00:00Added an answer on May 26, 2026 at 11:15 pm

    There is an error in your manifest. Action name in this element:

    <action android:name="@string/appwidget_markettiming_updateintent" />
    

    should be replaced by actual string, not the reference. So it should be something like this:

    <action android:name="com.myapp.android.appwidget.action.MARKETTIMING_UPDATE" />
    

    or whatever you have in your values/something.xml inside the <string name="appwidget_markettiming_updateintent"> element.

    The problem is that the BroadcastReceiver does not receives the broadcasts from AlarmManager. I’ve created a project with your code, replaced only this string in manifest (and added the appropriate value to values/strings.xml of course) and all works fine.

    In addition, you may want to replace the second parameter of alarmManager.setRepeating() by just System.currentTimeMillis() + 1000 and remove all those extra Calendar-related stuff.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't

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.