私の簡単なアプリケーションでBluetoothチュートリアル私はBluetoothのスキャンガンに接続し、それから文字列の形でスキャンを受け取ります。私はMainActivity
にすべての接続コードを持っています、そして今私がやろうとしているのは、別のアクティビティで私のreceiveScans(String s)
関数をオーバーライドして他の目的のためにデータを使用できるようにすることです。しかし、receiveScans(String s)
は常にMainActivity
アクティビティで呼び出され、TestingScanner
アクティビティでは決してヒットしません。どうしてこれなの?私は何をする必要がありますか?私Handler
- - MainActivity
でMainActivity
で私receiveScans()
機能にでスキャンしたデータを送信します。ここオーバーライドされた関数はデータを受信していませんか?
コードのビットです。
private MessageHandler mHandler = new MessageHandler();
private class MessageHandler extends android.os.Handler{
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case MESSAGE_READ: // Received scan, get information...
byte[] readBuf = (byte[]) msg.obj;
String scan = new String(readBuf);
receiveScans(scan); // Send string of info to recieveScans
break;
default:
super.handleMessage(msg);
}
}
}
protected void receiveScans(String s){
}
そして、ここで私がreceiveScans関数をオーバーライドするamtrying TestingScanner
クラスです。私は単にスキャナが送信された文字列に応じてTextView
にテキストを設定しようとしています。
public class TestingScanner extends MainActivity {
TextView scannerText ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_scanner);
scannerText = (TextView)findViewById(R.id.scanner_text);
}
@Override
protected void receiveScans(String s) {
super.receiveScans(s);
scannerText.setText(s);
}
}
オーバーライドされた関数が呼び出されることはありません。
override関数は、TestingScannerアクティビティが現在のアクティビティ(またはフォーカスされているアクティビティ)の場合にのみ呼び出されます。 – RScottCarson
これは不明です。 'TestingScanner'は' MessageHandler'を拡張しないので、 'TestingScanner.receiveScans()'が呼び出されるべき理由は明確ではありません。 –