使用服务在后台传输通话记录

发布于 2024-12-11 12:50:10 字数 3045 浏览 0 评论 0原文

我正在尝试实现一个后台服务来将日志从呼叫日志数据库传输到服务器,并且我尝试了很多东西...计时器,线程无限循环...但似乎没有任何作用...以下程序可能是计时器循环...但它们在某些手机上都失败了...两种实现都遵循相同的方法,但实现不同(显然)...所以我的问题是我不检查导致无法发送数据的某些情况 .... i了解数据在睡眠时停止发送,但几乎所有手机都会恢复数据传输,但有些则不会....

编程结构。

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-12-18 12:50:10

为什么不直接使用 IntentService 呢?这样您就不必处理所有线程。您放入 onHandleIntent() 方法中的所有代码都将在单独的线程上完成,您可以根据需要花多长时间。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文