Question

I am trying to implement a background service to transmit logs from a calllog database to a server and I have tried out a lot of things ... timer, a thread Looping infinitely... but it seems nothing works... the following program could be loop of timer... but both of them fail on some of the phones... Both of the implementations follow the same methodology but a different implementation(Obviously)... so my question is am i NOT checking out some condition that is causing this fault of not being able to send data .... i understand that the data stops sending when in sleep but almost all the phones resume data transmission but some dont ....

The programming structure .

    public class BootableService extends Service{
           @Override
           public IBinder onBind(final Intent intent) {

                  return null;
           }
           @Override
           public void onCreate() {

                  super.onCreate();
                  // use this to start my infinite thread here which is initialised inside this service class       
           }                    
           @Override
           public void onStart(final Intent intent, final int startId) {

                 //Use this to start a telophony listener that respond to call state and add the call from the call log to a blocking queue .... 

                 telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

                 // Create a new PhoneStateListener
                 listener = new PhoneStateListener() {

                        @Override
                        public void onCallStateChanged(int state, String incomingNumber) {

                            switch (state) {

                                case TelephonyManager.CALL_STATE_IDLE:
                                     System.out.println("phone IDLE");
                                     // calls a function to add the events to a queue. that have not been sent already. 
                                     addtoQueue();                                    
                                     break;
                                case TelephonyManager.CALL_STATE_OFFHOOK:
                                   System.out.println("Phone in use");
                                   break;
                                case TelephonyManager.CALL_STATE_RINGING:
                                   System.out.println("Phone Ringing");
                                       break;
                            }

                        }
                 };
                 // Register the listener wit the telephony manager
                 telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
          }
          addToQueue()
          {
             //add to the queue add the call log not already transmitted to the server. 
          }

          class handler HandleStuff{

               this handles the data coming from the thread .,,,, the ID of the record sent. straight from the Android call log database.  and store it. 
          }

          Class ThreadRunning extends thread{

                run(){
                     while(true){
                         // This implements take the elements from the queue if present 
                         transmit 1 record/// 
                         sleep(20 seconds.)
                     }
                }
         }
}

the logic is triggered only once. when the phone gets started or when the app is installed/opened. if the service is not already opened.

Was it helpful?

Solution

Why not just use an IntentService. That way you don't have to deal with all of the threading. All of your code you put in your onHandleIntent() method will be done on a seperate thread and you can take as long as you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top