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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:47:48+00:00 2026-05-29T15:47:48+00:00

I have activity which needs to be active all the time. I have thread

  • 0

I have activity which needs to be active all the time. I have thread which sleep 10 sec, and monitors values taken from database, compare them and start method. I’m wondering if user go back to other applications and activities, does my activity and thread still work, or they are handled by activity manager and go to pause-stop-destroy?? How to stay them a live??
Thank you.
here is code for that thread:

 new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            while(true){
                try {
                    Thread.sleep(10000);
                    myHendler.post(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub

                            final Calendar cal = Calendar.getInstance();
                            int godina2 = cal.get(Calendar.YEAR);
                            int mesec2 = cal.get(Calendar.MONTH);
                            int dan2 = cal.get(Calendar.DAY_OF_MONTH);
                            int sati2 = cal.get(Calendar.HOUR_OF_DAY);
                            int minuti2 = cal.get(Calendar.MINUTE);

                            trenutniDatum = new StringBuilder().append(dan2).append("-").append(mesec2 +1).append("-").append(godina2);
                            trenutnoVreme = prepraviVreme(sati2) + ":" + prepraviVreme(minuti2);
                            for(int i = 0; i < primljenoIzBazeDatum.length; i++){
                                String bazaBroj = "";
                                String bazaText = "";
                                if(primljenoIzBazeDatum[i].toString().equals(trenutniDatum.toString()) && primljenoIzBazeVreme[i].toString().equals(trenutnoVreme)){

                                        int bazaId = Integer.parseInt(primljenoIzBazeId[i]);
                                        bazaBroj = primljenoIzBazeBroj[i].toString();
                                        bazaText = primljenoIzBazeText[i].toString();
                                        String datumPromena = "*" + primljenoIzBazeDatum[i].toString() + "* SENT *";

                                        datumVreme.open();
                                        datumVreme.updateData(bazaId, datumPromena);
                                        datumVreme.close();

                                        sendPoruka(bazaBroj, bazaText);

                                }

                            } // end for


                        } // end run
                    });
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }).start();
  • 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-29T15:47:49+00:00Added an answer on May 29, 2026 at 3:47 pm

    Based on my understanding of what you want to do, here is what I would do :

    First, create a BroadcastReceiver

    public class Poller extends BroadcastReceiver {
        private final String TAG = "Poller";
        @Override
        public void onReceive( Context context, Intent intent ) {
        Log.i(TAG, "Poller broadcastintent received");
        Intent myIntent = new Intent( context, PollerService.class );
        context.startService( myIntent );
    }
    

    then , create the service that is called and then shuts itself down

    public class PollerService extends Service {
        final String TAG = "PollerService";
        @Override
        public void onStart(Intent intent, int startId) {
        Log.i(TAG, "Service onStart()");
        pollingTask.execute();
    }
    
    AsyncTask<Void, Void, Void> pollingTask = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... param) {
            // Do what you want in the background
        }
    
        @Override
        protected void onPostExecute(Void result) {
            stopSelf();
        }
    };
    }
    

    then, set an AlarmManager to wake the service every minute

    AlarmManager am = ( AlarmManager ) getSystemService( Context.ALARM_SERVICE );
    Intent alarmIntent = new Intent( "CHECK_DATABASE" );
    PendingIntent pi = PendingIntent.getBroadcast(context, 0 , alarmIntent, 0 );
    int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
    long interval = POLLING_INTERVAL_IN_MILLISECONDS;
    long triggerTime = SystemClock.elapsedRealtime() + interval;
    // For short intervals setInexact repeating is same as exactRepeating, use at least fifteen minutes to make it more efficient
    am.setInexactRepeating( type, triggerTime, interval, pi );
    Log.i(TAG, "Set inexact alarm through AlarmManager");
    }
    

    setup the receiver in Android manifest

    <receiver android:name="Poller">
        <intent-filter>
            <action android:name="CHECK_DATABASE"/>
        </intent-filter>
    </receiver>
    

    finally, unset the AlarmManager to stop polling once your required SMS is received

    AlarmManager am = ( AlarmManager ) getSystemService( Context.ALARM_SERVICE );
    Intent intent = new Intent( "CHECK_DATABASE" );
    PendingIntent pi = PendingIntent.getBroadcast( context, 0 , intent, 0 );
    am.cancel(pi);
    

    I do think that Peter is right though and this will kill you battery unless you’ll only be checking until you get the required info and then don’t poll and that’s a short time.

    Also, if you can get the exact time when you want to send the SMS with a single call from the database you can just set up the AlarmManger to wake up the service at that time, perform the action and be done with it. That would be the best approach (I can’t quite make out if that is the case from you code but it does seems to be from you comments).

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

Sidebar

Related Questions

I have an Android activity which in turn starts a thread. In the thread
So I have an Activity (say TestActivity ) which needs to act as a
I have an activity (act2), which can be launched from act1 or act3. If
I have an activity, which when starting (onCreate or onStart) needs to manipulate some
I have an Activity, which needs its orientation to be locked with setRequestedOrientation(screenOrientation); But
I have an activity which shows some List entries. When I click on a
I have created an activity which sends a number of notifications to status bar.
I have a couple of tables which are used to log user activity for
Say if I have a locationManager(LM) object in activity A, which is my main
I have a android application which needs username and password to login. I need

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.