Wygląda to tak:
public class MainActivity extends Activity {
public SpiInterface spiInterface;
public HandlerThread handlerThread;
public StringBuffer stringBuffer = new StringBuffer();
public byte[] numberOfReadBytes;
public byte[] numberOfWrittenBytes;
public char[] readBuffer;
public byte status;
public TextView textView;
public final static String EXTRA_MESSAGE = "com.mgr.adam.hmi_10.MESSAGE";
public boolean startActivity = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textView = new TextView(this);
textView.setLayoutParams(layoutParams1);
textView.setId(TextView.generateViewId());
textView.setPadding(5, 5, 5, 5);
textView.setText("HMI Prototype");
textView.setTextSize(40);
relativeLayout.addView(textView);
setContentView(relativeLayout);
readBuffer = new char[64];
numberOfReadBytes = new byte[1];
spiInterface = new SpiInterface(this);
handlerThread = new HandlerThread(handler);
handlerThread.start();
}
@Override
protected void onResume() {
super.onResume();
spiInterface.resumeAccessory();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
spiInterface.destroyAccessory();
super.onDestroy();
}
final Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
if (numberOfReadBytes[0] != 0x00) {
appendData(readBuffer, numberOfReadBytes[0]);
StringBuffer stringBuffer1 = new StringBuffer("A");
if (stringBuffer.charAt(0) == stringBuffer1.charAt(0) && startActivity == false) {
textView.setText("za********e!");
startActivity = true;
if (startActivity) {
Intent intent = new Intent(MainActivity.this, HMI.class);
String message = stringBuffer.toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
}
};
private class HandlerThread extends Thread {
volatile boolean stopThread = false;
Handler handler;
HandlerThread(Handler h) {
handler = h;
}
public void run() {
while (!stopThread) {
Message msg = handler.obtainMessage();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
numberOfReadBytes[0] = 0x00;
status = spiInterface.readData((byte) 64, readBuffer, numberOfReadBytes);
if (status == 0x00) {
handler.sendMessage(msg);
stopThread = true;
}
}
}
}
public void appendData(char[] data, int length) {
if (length >= 1)
stringBuffer.append(String.copyValueOf(data, 0, length));
stringBuffer.append("\n");
textView.setText(stringBuffer);
}
}
Drugie activity:
public class HMI extends Activity {
public TextView textView;
public StringBuffer stringBuffer;
public SpiInterface spiInterface;
public HandlerThread handlerThread;
public byte[] numberOfReadBytes;
public byte[] numberOfWrittenBytes;
public char[] readBuffer;
public byte status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
stringBuffer = new StringBuffer(message);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textView = new TextView(this);
textView.setLayoutParams(layoutParams1);
textView.setId(TextView.generateViewId());
textView.setPadding(5, 5, 5, 5);
textView.setTextSize(50);
textView.setText(message);
relativeLayout.addView(textView);
setContentView(relativeLayout);
readBuffer = new char[64];
numberOfReadBytes = new byte[1];
spiInterface = new SpiInterface(this);
handlerThread = new HandlerThread(handler);
handlerThread.start();
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (numberOfReadBytes[0] != 0x00) {
appendData(readBuffer, numberOfReadBytes[0]);
}
}
};
private class HandlerThread extends Thread {
Handler handler;
HandlerThread(Handler h) {
handler = h;
}
public void run() {
while (true) {
Message msg = handler.obtainMessage();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
numberOfReadBytes[0] = 0x00;
status = spiInterface.readData((byte) 64, readBuffer, numberOfReadBytes);
if (status == 0x00) {
handler.sendMessage(msg);
}
}
}
}
public void appendData(char[] data, int length) {
if (length >= 1)
stringBuffer.append(String.copyValueOf(data, 0, length));
stringBuffer.append("\n");
textView.setText(stringBuffer);
}
}
@@[usunięty]
Chodzi o to, że odbieram dane z USB na podstawie których chcę dynamicznie stworzyć layout. Urządzenie zewnętrzne przesyła najpierw informacje o tym jak ma wyglądać layout, a później przesyła dane bieżące takie jak np prędkość itp. Pomysł mam taki żeby w głównym activity sprawdzać i analizować te początkowe dane konfiguracyjne, na ich podstawie stworzyć nowe activity i jego layout, a następnie w tym activity odczytywać sobie on-line dalsze informacje. To jest mój pierwszy kontakt z programowaniem pod Androida także chętnie rozważę każdą propozycję. Sposób o którym mówisz wydaje się być dobry, mógłbyś mi chociaż tak z grubsza opisać jak należałoby to zrobić?