2013-01-19 35 views
0

我的程序在活动运行代码的While循环中运行。它每100ms左右读取一次USB数据。 当在USB数据流集的字节,我把这个startVoiceRecognitionActivity():While Run in Run停止执行

private void startVoiceRecognitionActivity() { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo..."); 
     startActivityForResult(intent, REQUEST_CODE);  
} 

语音识别运行良好,我的USB数据流,同时循环继续运行,而语音识别器做它的东西。

但是,在onActivityResult执行后,我的While循环终止。我需要留在这个while循环中,我该怎么做?

编辑:我一直在调试这一点,并会显示相关的当前代码。看起来onActivityReturn会在程序崩溃之前以某种方式中断USB。我编辑了我的while循环,以显示在语音识别器调用后我如何重新建立USB。

通过显示的代码,在调试运行的语音识别器调用之后,代码中断而没有断点。如果我从那里继续执行代码,程序就会像点击Terminate按钮一样停止。这是我的计算机屏幕,当它自己发生执行中断时,它似乎是一个NullPointerException异常问题,它仍然不在我的知识库中。希望有人能指出我正确的方向。

我开始相信,在下面的图片中看到的线程[< 12> Thread-130](正在运行)是罪魁祸首。此线程在从语音识别器调用返回时出现。我想我需要确保在语音识别器调用后执行返回到原始线程,但是我可以在这里离开吗?

enter image description here

或者更好的(也许),有没有一种方式,我打电话的语音识别器,我可以杀死原线< 11>?

WOW,就是这样或者至少我认为是这样,我所需要做的就是打破;在我启动语音识别器之后,while循环之外(我也必须在usb请求失败时跳出while循环)。

而且,我不必在runOnUiThread()中启动语音识别器。

我认为这工作像一个魅力,但我注意到暂停的线程积累,并最终程序停止执行语音识别器。我相信我会跟踪这一点,但任何智慧的话都会有所帮助。

对不起,我会留下整个初学者思考过程中的后续行动(除非你开始投票)。

P.S.这是一个了不起的调试环境!

看来挂起的线程是在我调试的时候创建的,还没有想出它为什么停止工作,但可能是所有那些挂起的线程。我会弄清楚。我一直无法复制这个问题,我想我真的已经找到了答案(上面),但我会等一会儿才发布这个答案,只是为了确保。

1/21 11:14 EST:在以后的onResume还是2个问题

- 关于1语音识别的10个呼叫在执行的onResume(结果),但运行()将不会执行()(在onResume()之后输入另外9次run()。这会导致我的程序挂起,因为while循环没有输入。

- 每当用户使用系统的“返回”箭头来使用另一个程序,然后返回到我的程序时,将出现另一个线程(带有它自己的while循环)。实际上,这会创建我的程序的多个实例,不可接受。

而且,我回到了在runOnUiThread()中启动语音识别器,我觉得不那么重要。

1/21下午1点27分斯:

,并再次,我想我有工作。我已经调用了100次语音识别,间歇性地使用了其他程序,并且在调试窗口中仍然有相同数量的正在运行的线程,并且没有挂起。

我做的是,声明一个活动级别整数exitloopnow。我在onCreate,onStart和onResume中将exitloopnow设置为0。我在onPause,onStop和onDestroy中将exitloopnow设置为1。在run()while循环中,如果exitloopnow == 1,我会中断。

再次,如果这继续工作,我会设置这个答案,除非有人提出一个更好的答案。

1/21 2:00 pm EST:

我猜100次是不够的。我在调试模式下将其挂起。它似乎卡住了.resquestWait()如下图。

我无法在'不可用[native method]'上找到太多内容,所以我再次陷入困境。如果我恢复代码执行并再次中断,它总是在他的行(350)上。

1/22 9:20 AM EST:

虽然代码在350行挂了,我可以拔掉USB连接线,然后重新插入其中并创造另一个工作线程但是这是不可取的。我也可以使用Android Back按钮并通过在最近的应用程序中选择程序来获得另一个线程,这也是不可取的。

enter image description here

下面是onActivityResult():

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) 
    { 
     // Populate the wordsList with the String values the recognition engine thought it heard 
     final ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

     this.runOnUiThread(new Runnable() { 
      public void run() { 
       ((TextView) findViewById(R.id.tvTitle)).setText(matches.get(0)); 
      } 
     }); 
     BusyV=0; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

而且目前While循环:

@Override 
public void run() { 
    while (true) { 
     request.queue(buffer, 64); 
     if (mConnection.requestWait() == request) { 
      if (buffer.get(7)==0) { //New Box (Yellow) button pressed now 
       this.runOnUiThread(new Runnable() { 
        public void run() { 
         ((TextView) findViewById(R.id.tvSpine)).setText("Spine"); 
         ((TextView) findViewById(R.id.tvPage)).setText("Page"); 
         ((TextView) findViewById(R.id.tvThick)).setText("Thick"); 
        } 
       }); 
       if (CanRecog==1) { 
        if (BusyV!=1) { 
         BusyV=1; 

         this.runOnUiThread(new Runnable() { 
          public void run() { 
           Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
           intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
             RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
           intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition ..."); 
           startActivityForResult(intent, REQUEST_CODE);  
          } 
         }); 
        } 
       } 
      } 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
      } 
     } else { 
      this.runOnUiThread(new Runnable() { 
       public void run() { 
        ((TextView) findViewById(R.id.tvZnum)).setText("USB lost at "+ System.currentTimeMillis()); 
        ((TextView) findViewById(R.id.tvXnum)).setText(" "); 
       } 
      }); 
     } 
    } 
} 

和我对试图重新建立USB(中的onResume代码因为onResume会自动调用onActivityResult()后的语音识别代码:

@Override 
public void onResume() { 
    super.onResume(); 
    Intent intent = getIntent(); 
    Log.d(TAG, "intent: " + intent); 
    savetofile("1 intent: " + intent); 
    connectToUsb(); 
} 

private void connectToUsb() { 
    if (amconnected==1) { 
     mConnection.close(); 
     request.close(); 
    } else {    
     setupusb(); 
    } 
    amconnected=0; 
    while(amconnected==0) { 
     doconnect(); 
    } 
    request.initialize(mConnection, mEndpointIntr); 
} 

private void setupusb() { 
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); 
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); 
    registerReceiver(mUsbReceiver, filter); 
} 

private void doconnect() { 
    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList(); 
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 
    while (deviceIterator.hasNext()) { 
     UsbDevice device1 = deviceIterator.next(); 
     if (device1.getVendorId() == 1240 & device1.getProductId() == 63) { 
      mUsbManager.requestPermission(device1, mPermissionIntent); 
      setDevice(device1); 
     } 
    } 
} 

private void setDevice(UsbDevice device) { 
    Log.d(TAG, "setDevice " + device); 
    savetofile("2 setDevice " + device); 
    if (device.getInterfaceCount() != 1) { 
     Log.e(TAG, "could not find interface"); 
     savetofile("3 could not find interface"); 
     return; 
    } 
    UsbInterface intf = device.getInterface(0); 
    UsbEndpoint ep = intf.getEndpoint(0); 
    if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) { 
     Log.e(TAG, "endpoint is not interrupt type"); 
     savetofile("5 endpoint is not interrupt type"); 
     return; 
    } 
    mEndpointIntr = ep; 
    if (device != null) { 
     UsbDeviceConnection connection = mUsbManager.openDevice(device); 
     if (connection != null && connection.claimInterface(intf, true)) { 
      Log.d(TAG, "open SUCCESS"); 
      savetofile("6 open SUCCESS"); 
      mConnection = connection; 
      Thread thread = new Thread(this); 
      thread.start(); 
      amconnected=1; 
     } else { 
      Log.d(TAG, "open FAIL"); 
      savetofile("7 open FAIL"); 
      mConnection = null; 
     } 
    } 
} 

,我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="act.ProMeasure2D"> 

<uses-feature android:name="android.hardware.usb.host" /> 
<uses-sdk android:minSdkVersion="13" /> 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application android:theme="@android:style/Theme.Black" android:label="ProMeasure2D"> 
    <activity android:name="act.ProMeasure2D.ProMeasure2DActivity" 
     android:label="ProMeasure2D"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

     <intent-filter> 
      <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
     </intent-filter> 

     <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
      android:resource="@xml/device_filter" /> 
    </activity> 
</application> 

,我的项目结构: enter image description here

+2

由于上面显示的代码中没有'while'循环,所以任何人都很难回答这个问题。 – CommonsWare

+0

@CommonsWare你问了它! – KiloOne

+0

进一步的调试显示,似乎正在发生的事情是,当执行onActvityResult时,我的USB连接中断。我还没有任何运气重新建立连接。 – KiloOne

回答

0

就是这样,所有我需要做的是打破;在我启动语音识别器之后,while循环之外(我也必须在usb请求失败时跳出while循环)。

而且,我不必在runOnUiThread()中启动语音识别器。

现在工作就像一个魅力。

对不起,我会留下整个初学者思考过程中的后续行动(除非你开始投票)。

P.S.这是一个了不起的调试环境!