0

I'm doing an app for a bluetooth thermal printer. I've done the printing part already.

This is the code:

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);
        }
    }

When I invoke printText the printer prints twice or thrice. It seems that startService(sendIntent) fires more than once.

user2034438
  • 71
  • 2
  • 7

1 Answers1

1

You can't start a service twice or thrice. Read about the startService method.

Returns

If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

Try to debug your code, add 'Log' lines to fix the issue.

Community
  • 1
  • 1
dimetil
  • 3,851
  • 2
  • 29
  • 47