首先,您必须在主要活动中创建一个静态处理程序:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Log.e("Test", msg.getData().getCharSequence("MAINLIST").toString());
}
};
}
然后在你的套接字类中:
public void OnSerialsData(final byte[] buffer, final int size) {
Message msg = MainActivity.myHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putCharSequence("MAINLIST", "IS_OK");
msg.setData(bundle);
MainActivity.myHandler.sendMessage(msg);
}
但是您必须确保在调用 OnSerialsData 方法之前必须创建您的处理程序。
我希望这会有所帮助。