呼叫期间的线程块

时间:2011-01-17 18:50:06

标签: android multithreading service timer android-asynctask

我有一个严重的问题,我正在开发一个应用程序,可以在通话期间调用通话时间;当手机放在耳朵上时,计时器所具有的线程会阻塞并且在取下我的耳朵之前不再响应时出现问题。下一个日志显示了问题。

01-11 16:14:19.607 14558 14566 I Estado:postDelayed Async Service

01-11 16:14:20.607 14558 14566 I Estado:postDelayed Async Service

01-11 16:14:21.607 14558 14566 I Estado:postDelayed Async Service

01-11 16:14:22.597 14558 14566 I Estado:postDelayed Async Service

01-11 16:14:23.608 14558 14566 I Estado:postDelayed Async Service

01-11 16:14:24.017 1106 1106 D iddd:select()< 0,可能是处理信号:系统调用中断

01-11 16:14:24.607 14558 14566 I Estado:postDelayed Async Service

01-11 16:18:05.500 1106 1106 D iddd:select()< 0,可能是处理信号:系统调用中断

01-11 16:18:06.026 14558 14566 I Estado:postDelayed Async Service

01-11 16:18:06.026 14558 14566 I Estado:postDelayed Async Service

01-11 16:18:06.026 14558 14566 I Estado:postDelayed Async Service

01-11 16:18:06.026 14558 14566 I Estado:postDelayed Async Service

01-11 16:18:06.026 14558 14566 I Estado:postDelayed Async Service

01-11 16:18:06.026 14558 14566 I Estado:postDelayed Async Service

我一直在尝试使用服务,计时器,线程,AyncTasks,他们都会遇到同样的问题。

我的代码:

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);     
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,     
          WindowManager.LayoutParams.FLAG_FULLSCREEN);        
        setContentView(R.layout.main); 
 HangUpService.setMainActivity(this);
 objHangUpService = new Intent(this, HangUpService.class);

     Runnable rAccion = new Runnable() {
      public void run() {
          TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
          tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

          objVibrator = (Vibrator) getSystemService(getApplicationContext().VIBRATOR_SERVICE);

          final ListView lstLlamadas = (ListView) findViewById(R.id.lstFavoritos);
          final EditText txtMinutos = (EditText) findViewById(R.id.txtMinutos);
          final EditText txtSegundos = (EditText) findViewById(R.id.txtSegundos);

       ArrayList<Contacto> cContactos = new ArrayList<Contacto>();
          ContactoAdapter caContactos = new ContactoAdapter(HangUp.this, R.layout.row,cContactos);

          Cursor curContactos =  getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            null,
            null,
            null,
            ContactsContract.Contacts.TIMES_CONTACTED + " DESC");

          while (curContactos.moveToNext()){

        String strNombre = curContactos.getString(curContactos.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String strID = curContactos.getString(curContactos.getColumnIndex(ContactsContract.Contacts._ID));
        String strHasPhone=curContactos.getString(curContactos.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        String strStarred=curContactos.getString(curContactos.getColumnIndex(ContactsContract.Contacts.STARRED));

           if (Integer.parseInt(strHasPhone) > 0 && Integer.parseInt(strStarred) ==1 ) {

            Cursor CursorTelefono = getContentResolver().query(
                  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                  null, 
                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = " + strID, 
                  null, null);

            while (CursorTelefono.moveToNext()) {

             String strTipo=CursorTelefono.getString(CursorTelefono.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
             String strTelefono=CursorTelefono.getString(CursorTelefono.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
             strNumero=strTelefono; 

             String args[]=new String[1];     
             args[0]=strNumero;

             Cursor CursorCallLog = getContentResolver().query(
                 android.provider.CallLog.Calls.CONTENT_URI, 
                 null, 
                 android.provider.CallLog.Calls.NUMBER + "=?",
                 args, 
                 android.provider.CallLog.Calls.DATE+ " DESC");

             if (Integer.parseInt(strTipo)==2)
             {
                 caContactos.add(
                     new Contacto(
                      strNombre,             
                      strTelefono
                     )
                    );
             }
            }
            CursorTelefono.close();
           }
          } 
          curContactos.close();

          lstLlamadas.setAdapter(caContactos);   
          lstLlamadas.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView a, View v, int position, long id) {

           Contacto mContacto=(Contacto)lstLlamadas.getItemAtPosition(position);

           i = new Intent(HangUp.this, Llamada.class);

           Log.i("Estado","Declaro Intent");
           Bundle bundle = new Bundle();    
           bundle.putString("telefono", mContacto.getTelefono());
           i.putExtras(bundle);
           startActivityForResult(i,SUB_ACTIVITY_ID);                  
           Log.i("Estado","Inicio Intent");
           blActivo=true;

           try {

                String strMinutos=txtMinutos.getText().toString();
                String strSegundos=txtSegundos.getText().toString();

                if(!strMinutos.equals("") && !strSegundos.equals("")){

                 int Tiempo = (
                   (Integer.parseInt(txtMinutos.getText().toString())*60) + 
                   Integer.parseInt(txtSegundos.getText().toString()) 
                  )* 1000;

                 handler.removeCallbacks(rVibrate);
                 cTime = System.currentTimeMillis();
                 cTime=cTime+Tiempo;

                 objHangUpAsync = new HangUpAsync(cTime,objVibrator,objPowerManager,objKeyguardLock);
                 objHangUpAsync.execute();

                 objPowerManager.userActivity(Tiempo+3000, true);
                 objHangUpService.putExtra("cTime", cTime);
   //startService(objHangUpService);


           } catch (Exception e) {
            e.printStackTrace();
           } finally {

           }
          }
          });
         }
     };
    }        

的AsyncTask:

@Override
protected String doInBackground(String... arg0) {

  blActivo = true;

  mWakeLock = objPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
  objKeyguardLock.disableKeyguard();
  Log.i("Estado", "Entro a doInBackground");

  timer.scheduleAtFixedRate( 
  new TimerTask() { 
   public void run() {
    if (blActivo){
     if (cTime<System.currentTimeMillis()){

      blActivo=false;
      objVibrator.vibrate(1000);
      Log.i("Estado","Vibrar desde Async");
      this.cancel();

     }else{
      try{
        mWakeLock.acquire();                   
        mWakeLock.release();
 Log.i("Estado","postDelayed Async Service");
      }catch(Exception e){
        Log.i("Estado","Error: " + e.getMessage());
      }
     }
    }
   } 
  }, 0, INTERVAL);
  return null;
 } 

1 个答案:

答案 0 :(得分:0)

您是否需要一直跟踪?如何记录通话开始的那一刻,然后当他们把手机放到你的线程块时。当他们完成通话并挂机时,他们将电话拉开,你的线程再次开始重新启动,你可以捕捉时间&#34;现在&#34;并从另一个中减去一个。