gpt4 book ai didi

android - Android中的线程导致手机崩溃

转载 作者:太空狗 更新时间:2023-10-29 15:27:49 26 4
gpt4 key购买 nike

我不知道这段代码有什么问题,因为我无法阅读崩溃日志。我们不是在谈论应用程序崩溃,而是可能由死锁线程或某种锁定引起的电话崩溃。欢迎提出建议!


背景:

当我启动我的连接时,会显示一个对话框,当我按下“返回”按钮时,该对话框会卡住,一段时间后手机会崩溃...

代码:

这是处理与设备连接的线程。我完全没有连接到设备的问题。我所知道的是 mmSocket.connect() 在我按下后退按钮时运行。认为问题出在某处...

class ConnectThread extends Thread {

/**
*
*/
private Handler threadhandler;
private BluetoothDevice mmDevice;
private volatile BluetoothSocket mmSocket;
private Message toMain;
// private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

public ConnectThread(Handler threadhandler, BluetoothDevice device) {

this.threadhandler = threadhandler;
this.mmDevice = device;


// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);

}catch (NoSuchMethodException e) {

e.printStackTrace();
} catch (IllegalArgumentException e) {

e.printStackTrace();
} catch (IllegalAccessException e) {

e.printStackTrace();
} catch (InvocationTargetException e) {

e.printStackTrace();
}

}

public void run() {
Looper.prepare();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception

mmSocket.connect();

toMain = threadhandler.obtainMessage();
toMain.arg1 = 1;
threadhandler.sendMessage(toMain);


} catch (SecurityException e) {

Log.e("SecurityExcep", "Oh noes" , e);
toMain = threadhandler.obtainMessage();
toMain.arg1 = 2;
threadhandler.sendMessage(toMain);
Log.w("MESSAGE", e.getMessage());

}catch (IOException e) {
//Bad connection, let's get the hell outta here
try {
Log.e("IOExcep", "Oh noes" , e);
Log.w("MESSAGE", e.getMessage());

mmSocket.close();
toMain = threadhandler.obtainMessage();
toMain.arg1 = 2;
toMain.obj = e.getMessage();
threadhandler.sendMessage(toMain);


return;
} catch (IOException e1) {
Log.e("IOExcep2", "Oh noes" , e);
}
}
try {
mmSocket.close();
} catch (IOException e) {
Log.d("CONNECT_CONSTRUCTOR", "Unable to close the socket", e);
}

toMain = threadhandler.obtainMessage();
toMain.arg1 = 3;
threadhandler.sendMessage(toMain);

Looper.loop();
return;
// Now it should be paired.. only thing to do now is let the user commit to the rest
}

/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}

}

下一个代码是来自对话框创建者的 fragment ,线程称为d:

(...)

case DIALOG_BT_ADDING:
search_dialog = new ProgressDialog(this);
search_dialog.setTitle(R.string.adding);
search_dialog.setMessage(res.getText(R.string.bluetooth_add_accept));
search_dialog.setIndeterminate(true);
search_dialog.setCancelable(true);
search_dialog.setOnCancelListener(new OnCancelListener() {

@Override
public void onCancel(DialogInterface dialog) {
Log.i("THREAD CONNECT", "Is it alive?: " + d.isAlive());
if(d != null && d.isAlive()){
d.cancel();
//d = null;
}
if(d2 != null && d2.isAlive()){
d2.cancel(false);
//d2 = null;

}
search_dialog.dismiss();
showDialog(DIALOG_NEW_DEVICE_FOUND);

}
});
return search_dialog;
(...)

这是执行 ConnectThread 类的代码 fragment

private void connectBluetooth(boolean nextstage, IOException e1){
if(!nextstage){
showDialog(DIALOG_BT_ADDING);

d = new ConnectThread(threadhandler, selected_car.getDevice());
d.start();

}
else{
if(e1 != null){

d2 = new BluetoothCheckThread(checkthreadhandler,mBluetoothAdapter,
5000, car_bt, after_bt);
d2.start();
search_dialog.dismiss();
}
else{
showDialog(DIALOG_BT_ADDING_FAILED);
}
}
}

希望你们能帮助我!感谢任何反馈

最佳答案

好的,您正在从看起来像 UI 线程的地方调用 BluetoothSocket.close()。这可能是导致“卡住”的原因。

当您说手机“崩溃”时,是指它会重新启动吗?如果是这样,这是完全重启(屏幕返回到您第一次打开手机时发生的情况)还是运行时重启(手机通常显示某种动画,在 Nexus 设备上,它是四色粒子喷雾)?如果不是重启,您的意思是您只是看到一个对话框,让您终止该应用程序?

无论哪种情况,您都可能希望获得对调用 BluetoothSocket.connect() 并调用 Thread.interrupt() 的线程的引用。我不确定 BluetoothSocket 是否可中断,但我们希望如此。然后在中断之后,调用 close(),这可能不应该在主线程上调用。

关于android - Android中的线程导致手机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6898773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com