1

コールログ データベースからサーバーにログを送信するバックグラウンド サービスを実装しようとしています。多くのことを試しました... タイマー、スレッド 無限にループしています...しかし、何も機能していないようです...次のプログラムタイマーのループである可能性があります...しかし、一部の電話では両方とも失敗します...両方の実装は同じ方法論に従いますが、実装は異なります(明らかに)...だから私の質問は、私はいくつかの条件をチェックアウトしていないということですそれがデータを送信できないというこの障害を引き起こしています....スリープ中にデータの送信が停止することは理解していますが、ほとんどすべての電話がデータ送信を再開しますが、一部の電話はデータ送信を再開しません....

プログラミング構造。

    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 回だけトリガーされます。電話が開始されたとき、またはアプリがインストールされたとき/開いたとき。サービスがまだ開かれていない場合。

4

1 に答える 1

1

IntentServiceを使用しないのはなぜですか。そうすれば、すべてのスレッドを処理する必要はありません。onHandleIntent()メソッドに入力するすべてのコードは、別のスレッドで実行されるため、必要なだけ時間がかかる可能性があります。

于 2011-10-24T18:40:12.407 に答える