2013-02-02 30 views
0

我正在爲藍牙熱敏打印機做一個應用程序。我已經完成了打印部分。startService(sendIntent)發射不止一次

這是代碼:

public void printText(View view)  
    { 
     Button b = (Button)findViewById(R.id.button2); 
     if (b.getText().toString().equals("Choose Printer")) 
      errorAlert("No Printer Selected"); 
     else 
     { 
      EditText e = (EditText)findViewById(R.id.printerText); 
      errorAlert("Printing...");  
      Intent sendIntent = new Intent(); 
      stopService(sendIntent); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra("MAC", mac); 
      sendIntent.putExtra("DATA", e.getText().toString()+"\n\n"); 
      sendIntent.setComponent(new ComponentName("com.example.bluetoothtest",""com.example.bluetoothtest.MainActivity"")); 
      startService(sendIntent); 
     } 
    } 

當我調用printText打印機打印兩次或三次。似乎startService(sendIntent)不止一次觸發。

回答

1

您無法兩次或三次啓動服務。閱讀有關startServicemethod

返回

如果該服務正在啓動或正在運行時, 組件名稱已啓動返回的實際服務的;否則 如果服務不存在返回null。

試着調試你的代碼,添加'日誌'行來解決這個問題。