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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:06:03+00:00 2026-05-29T07:06:03+00:00

I have a Service that uses a custom Connection class (extends thread) to a

  • 0

I have a Service that uses a custom Connection class (extends thread) to a hardware controller. When the User prefers, I wish to maintain this connection on a permanent basis. I already have the code to handle when the Android device loses its internet connection, switches between wi-fi, etc.

In order to stay connected, the controller requires that you speak to it within every 5 minutes. I currently, within the Connection class start a thread that runs in a while(), and checks the system time and the last time it communicated, and when > 4 minutes it requests a status. For some reason, at different times the communication doesn’t occur in time. i.e., occurs after 5 minutes. The Service doesn’t die, as far as I can tell but the “Ping” to the controller is late. This doesn’t happen when I have the phone plugged into the charger (or debugger). Additionally, the behavior is the same when I move the Service to the foreground.

Does the phone slow down it’s processor when it goes to sleep?

Is there a better way?

I’m thinking it’s the AlarmManger, but I’m having trouble getting it to work with an inner-class, within the Service. I tried using the API demos as a starting point, but I can’t seem to figure out how to get the Broadcast receiver registered. I am trying to register the receiver programmatically, with no changes to the manifest.

public class DeviceConnectionService extends Service {

    @Override
    public void onCreate() {
        Intent intent = new Intent(this, PingConnection.class);
        intent.setAction("KEEP_CONNECTION_ALIVE");
        PendingIntent sender = PendingIntent.getBroadcast(this,
            0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // We want the alarm to go off 30 seconds from now.
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 15*1000;
        // Schedule the alarm!
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                firstTime, 15*1000, sender);
        // register to listen to the Alarm Manager
        if (mPingConnectionReceiver == null) {
            mPingConnectionReceiver = new PingConnection();
            getApplicationContext().registerReceiver(mPingConnectionReceiver,
             new IntentFilter("KEEP_CONNECTION_ALIVE"));
        }
    }

    // ...

    public class PingConnection extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (dBug) Log.i("PingConnection", "Pinging Controller");
            // do real work 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-29T07:06:04+00:00Added an answer on May 29, 2026 at 7:06 am

    Does the phone slow down it’s processor when it goes to sleep?

    The phone shuts down its processor when it goes to sleep. That is the definition of “sleep”.

    I’m thinking it’s the AlarmManger, but I’m having trouble getting it to work with an inner-class, within the Service. I tried using the API demos as a starting point, but I can’t seem to figure out how to get the Broadcast receiver registered. I am trying to register the receiver programatically, with no changes to the manifest.

    That is an unusual approach for AlarmManager. That being said, since you declined to describe “having trouble” in any detail, it is difficult to help you.

    Get rid of getApplicationContext() (you don’t need it and really don’t want it in this case). I would register the receiver before touching AlarmManager. Before you go to production, please choose an action name that has your package name in it (e.g., com.something.myapp.KEEP_CONNECTION_ALIVE).

    Beyond that, check LogCat for warnings.


    UPDATE

    In your LogCat, you should have a warning from AlarmManager complaining about not being able to talk to your BroadcastReceiver.

    Replace:

    Intent intent = new Intent(this, PingConnection.class);
    intent.setAction("KEEP_CONNECTION_ALIVE");
    

    with:

    Intent intent = new Intent("KEEP_CONNECTION_ALIVE");
    

    and you may have better luck.

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

Sidebar

Related Questions

I have a WCF service and a client, that uses a custom UserNamePasswordValidator for
I have a service that uses a custom UserNamePasswordValidator and need to save the
I have an application that uses a service to create an ArrayList of custom
I have a web service that uses Python's SimpleJSON to serialize JSON, and a
I have a WCF service that uses basicHttpbinding in development. Now in product we
I have a WCF service that uses a System.ServiceModel.Syndication.SyndicationFeed to create an RSS feed.
I have a WCF service that uses X.509 certificates for authentication. What's the best
I have a wcf service that uses the .net System.AddIns framework to load assemblies
I have a web service that uses nhibernate, calls to the web service go
I have a simple web service that uses an oracle database. When I test

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.