startService(sendIntent)多次触发

时间:2013-02-02 05:21:17

标签: android android-intent bluetooth thermal-printer

我正在为蓝牙热敏打印机做一个应用程序。我已经完成了打印部分。

这是代码:

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 个答案:

答案 0 :(得分:1)

您无法启动服务两次或三次。阅读startService method

  

返回

     

如果服务正在启动或已在运行,则   返回已启动的实际服务的ComponentName;其他   如果服务不存在则返回null。

尝试调试代码,添加“日志”行以解决问题。

相关问题