0

Bluetoothサーマルプリンター用のアプリをやっています。私はすでに印刷部分を行っています。

これはコードです:

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 を呼び出すと、プリンターは 2 回または 3 回印刷します。startService(sendIntent) が複数回発火するようです。

4

1 に答える 1

1

サービスを2回または3回開始することはできません。startService メソッドについてお読みください。

戻り値

サービスが開始されているか、すでに実行されている場合は、開始された実際のサービスのComponentNameが返されます。それ以外の場合、サービスが存在しない場合はnullが返されます。

コードをデバッグし、「ログ」行を追加して問題を修正してください。

于 2013-02-02T05:48:32.070 に答える