使用服务在后台传输呼叫日志

时间:2011-10-24 18:37:08

标签: android service timer telephonymanager

我正在尝试实现后台服务,以便将日志从calllog数据库传输到服务器,我尝试了很多东西......计时器,一个线程无限循环......但似乎没什么用......下面的程序可能是计时器的循环...但是它们都在一些手机上失败了......两个实现都遵循相同的方法,但实现不同(显然)...所以我的问题是我没有检查导致这种无法发送数据的错误的条件 ....我明白数据在睡眠时停止发送,但几乎所有电话都恢复数据传输但有些不... 。

编程结构。

    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.)
                     }
                }
         }
}

逻辑只触发一次。手机启动时或安装/打开应用程序时。如果服务尚未打开。

1 个答案:

答案 0 :(得分:1)

为什么不使用IntentService。这样您就不必处理所有线程。你在onHandleIntent()方法中放入的所有代码都将在一个单独的线程上完成,你可以随意使用。