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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:23:31+00:00 2026-05-25T11:23:31+00:00

I have an android app that will record data every x mins. The time

  • 0

I have an android app that will record data every x mins. The time the alarm goes off is based on the settings page (either 10 or 30 mins). The alarm is first activated when it goes into the splash screen. My problem is, it doesnt seem to be called when I’m using it on my phone and so I was hoping someone could tell me if I’m using the function properly.

    if (prefs.getBoolean("backgroundupdates", true)) {
        Intent setAlarm = new Intent(Splash.this,
                UpdateLocation.class);
        PendingIntent pendingIntent = PendingIntent.getService(
                Splash.this, 0, setAlarm, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy-MM-dd hh:mm:ssaa");
        History.addHistory(sdf.format(calendar.getTime()), "App Started", "");
        calendar.setTimeInMillis(System.currentTimeMillis());

        int UPDATE_TIME = prefs.getInt("Update_time", 30);
        calendar.add(Calendar.MINUTE, UPDATE_TIME);
        alarmManager.set(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), pendingIntent);


    }

This checks if background updates are on and sets an alarm for UPDATE_TIME mins (default 30)

in my UpdateLocation.class I have this in the bottom

    Intent setAlarm = new Intent(UpdateLocation.this,
                    UpdateLocation.class);
            PendingIntent pendingIntent = PendingIntent.getService(
                    UpdateLocation.this, 0, setAlarm, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            int UPDATE_TIME = prefs.getInt("Update_time", 30);
            calendar.add(Calendar.MINUTE, UPDATE_TIME);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), pendingIntent);

This is so that the alarm will call itself again in UPDATE_TIME mins. The reason I’m doing it this way is because UPDATE_TIME may change (depending on user preferences)

Thanks!

EDIT here is my UpdateLocation.class

public class UpdateLocation extends IntentService {
    public static final String id = "";

    public UpdateLocation() {
        super("UpdateLocation");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        SharedPreferences prefs = getSharedPreferences("Settings", 0);
        final String id = prefs.getString("ID", "");
        if (prefs.getBoolean("backgroundupdates", true)) {
            HttpParams httpParams = new BasicHttpParams();
            // 30seconds and it stops
            HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
            HttpConnectionParams.setSoTimeout(httpParams, 30000);
            DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
            HttpPost httpost = new HttpPost(
                    "http://iphone-radar.com/gps/gps_locations");

            JSONObject holder = new JSONObject();

            try {
                holder.put("id", id);
                LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                String bestProvider = locationManager.getBestProvider(criteria,
                        false);
                LocationListener loc_listener = new LocationListener() {

                    @Override
                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onLocationChanged(Location location) {
                        // TODO Auto-generated method stub

                    }
                };
                try {
                    Looper.prepare();
                    locationManager.requestLocationUpdates(bestProvider, 0, 0,
                            loc_listener);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Location location = locationManager
                        .getLastKnownLocation(bestProvider);
                Calendar c = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "hh:mmaa MM/dd/yyyy");
                holder.put("time", sdf.format(c.getTime()));
                holder.put("time_since_epoch", System.currentTimeMillis()/1000);
                try {
                    holder.put("lat", location.getLatitude());
                    holder.put("lon", location.getLongitude());
                } catch (NullPointerException e) {
                    try {
                        holder.put("lat", -1.0);
                        holder.put("lon", -1.0);
                    } catch (JSONException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

                StringEntity se = new StringEntity(holder.toString());
                httpost.setEntity(se);
                httpost.setHeader("Accept", "application/json");
                httpost.setHeader("Content-type", "application/json");

                ResponseHandler responseHandler = new BasicResponseHandler();
                String response = httpclient.execute(httpost, responseHandler);
                org.json.JSONObject obj;
                SimpleDateFormat sdf2 = new SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ssaa");
                try{
                History.addHistory(sdf2.format(c.getTime()), "GPS",
                        "Latitude: " + location.getLatitude() + "\n"
                                + "Longitude: " + location.getLongitude());}
                catch(NullPointerException e){
                    History.addHistory(sdf2.format(c.getTime()), "GPS",
                            "Latitude: " + "No Signal" + "\n"
                                    + "Longitude: " + "No Signal");
                }
                obj = new org.json.JSONObject(response);
                Intent setAlarm = new Intent(UpdateLocation.this,
                        UpdateLocation.class);
                PendingIntent pendingIntent = PendingIntent.getService(
                        UpdateLocation.this, 0, setAlarm, 0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                int UPDATE_TIME = prefs.getInt("Update_time", 30);
                calendar.add(Calendar.MINUTE, UPDATE_TIME);
                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(), pendingIntent);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

}

Note, I do not have a broadcast receiver, I’m not quite sure how to use it so any examples would be very helpful.

  • 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-25T11:23:32+00:00Added an answer on May 25, 2026 at 11:23 am

    Could you possibly post all of your UpdateLocation.class? It needs to extend BroadcastReceiver so that may be where your problem lies.

    As for your alarm, if it’s being set to go off every xx minutes without end, you’re better off using alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), UPDATE_TIME * 60000, pendingIntent); to have your alarm reset on a regular basis.


    Edit 1

    Regarding putting the PendingIntent into a static function for easy access, it could go something like this:

    public static PendingIntent getPendingIntent(int update_time) {
        Intent setAlarm = new Intent(this.getBaseContext(), UpdateLocation.class);
        PendingIntent pendingIntent = PendingIntent.getService(this.getBaseContext(),
                                                               0, setAlarm, 0)
        return pendingIntent;
    }
    

    And then you can set your alarm with alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), UPDATE_TIME * 60000, MyClass.getPendingIntent());

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

Sidebar

Related Questions

I am writing a simple Android app and have a database that will send
I'm creating an android app that will have multiple pages with the same layout,
I'm helping a friend create an android app that will have screens with lists
We have an Android app that periodically polls for data and updates the display.
I have a class that extends android.app.Dialog, the layout is done in an xml
I have an Android project that branched into three different applications, app-1 , app-2
We're designing an Android app that has a lot of data (customers, products, orders...),
i'm working on a android app that will display Strings to the user, and
I have an app that is available from the Android Market. Some users have
I'm trying to write an Android 2.2 app that will find installed apps that

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.