如何在 Android Studio 中重新启动线程?
How can I restart a thread in Android Studio?
我有点烦恼,不知道有没有人能给我点亮。
我有一个方法,我在 onPostExecute 回调中调用 AsyncTask class:
@Override
protected void onPostExecute(Void result)
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
finish();
}
else
{
msg("Connected.");
isBtConnected = true;
beginListenForData();
}
progress.dismiss();
}
它启动一个线程(在这种情况下是从我的 arduino 给我一些信息):
private void beginListenForData()
{
Log.i("[BLUETOOTH]", "Creating handler");
mHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
//super.handleMessage(msg);
if(msg.what == ConnectedThread.RESPONSE_MESSAGE){
String txt = (String)msg.obj;
if(myLabel.getText().toString().length() >= 30){
myLabel.setText("");
myLabel.append(txt);
}else{
myLabel.append("\n" + txt);
}
}
}
};
Log.i("[BLUETOOTH]", "Creating and running Thread");
btt = new ConnectedThread(btSocket,mHandler);
btt.start();
}
这是我的 运行 方法的代码:
public void run(){
byte[] buffer = new byte[1024];
int bytes;
write("PIN".toString().getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(mmInStream));
Log.i("[THREAD-CT]","Starting thread");
while(true){
try{
bytes = mmInStream.read(buffer);
String resp = new String(buffer, 0, bytes);
//String resp = br.readLine();
//Transfer these data to the UI thread
Message msg = new Message();
msg.what = RESPONSE_MESSAGE;
msg.obj = resp;
uih.sendMessage(msg);
}catch(IOException e){
break;
}
}
Log.i("[THREAD-CT]","While loop ended");
}
问题是当我向我的 arduino 发送信息时(按下按钮)。这是我的写代码:
public void write(byte[] bytes){
try{
Log.i("[THREAD-CT]", "Writting bytes");
mmOutStream.write(bytes);
}catch(IOException e){}
}
它停止听我从头开始的内容。它没有再次进入 运行 方法。我已阅读 this 答案,但我不太确定如何在此处执行异步任务。
使用 onPostExecute() 可以运行 OnUiThread() 的异步任务。这将使它保持聆听。在此处查看此类异步任务的示例代码:
private class TheTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
return null;
}
protected void onPostExecute(String result) {
runOnUiThread(new Runnable() {
public void run() {
}
});
}
}
好吧,问题似乎不在我的 android 代码中,而是在我的 arduino 代码中。似乎在我发送信息后,我的 arduino 也停止发送了。我已经解决了这个问题并且它有效。
我有点烦恼,不知道有没有人能给我点亮。
我有一个方法,我在 onPostExecute 回调中调用 AsyncTask class:
@Override
protected void onPostExecute(Void result)
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
finish();
}
else
{
msg("Connected.");
isBtConnected = true;
beginListenForData();
}
progress.dismiss();
}
它启动一个线程(在这种情况下是从我的 arduino 给我一些信息):
private void beginListenForData()
{
Log.i("[BLUETOOTH]", "Creating handler");
mHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
//super.handleMessage(msg);
if(msg.what == ConnectedThread.RESPONSE_MESSAGE){
String txt = (String)msg.obj;
if(myLabel.getText().toString().length() >= 30){
myLabel.setText("");
myLabel.append(txt);
}else{
myLabel.append("\n" + txt);
}
}
}
};
Log.i("[BLUETOOTH]", "Creating and running Thread");
btt = new ConnectedThread(btSocket,mHandler);
btt.start();
}
这是我的 运行 方法的代码:
public void run(){
byte[] buffer = new byte[1024];
int bytes;
write("PIN".toString().getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(mmInStream));
Log.i("[THREAD-CT]","Starting thread");
while(true){
try{
bytes = mmInStream.read(buffer);
String resp = new String(buffer, 0, bytes);
//String resp = br.readLine();
//Transfer these data to the UI thread
Message msg = new Message();
msg.what = RESPONSE_MESSAGE;
msg.obj = resp;
uih.sendMessage(msg);
}catch(IOException e){
break;
}
}
Log.i("[THREAD-CT]","While loop ended");
}
问题是当我向我的 arduino 发送信息时(按下按钮)。这是我的写代码:
public void write(byte[] bytes){
try{
Log.i("[THREAD-CT]", "Writting bytes");
mmOutStream.write(bytes);
}catch(IOException e){}
}
它停止听我从头开始的内容。它没有再次进入 运行 方法。我已阅读 this 答案,但我不太确定如何在此处执行异步任务。
使用 onPostExecute() 可以运行 OnUiThread() 的异步任务。这将使它保持聆听。在此处查看此类异步任务的示例代码:
private class TheTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
return null;
}
protected void onPostExecute(String result) {
runOnUiThread(new Runnable() {
public void run() {
}
});
}
}
好吧,问题似乎不在我的 android 代码中,而是在我的 arduino 代码中。似乎在我发送信息后,我的 arduino 也停止发送了。我已经解决了这个问题并且它有效。