Java 在Android中使用线程发送消息
我有以下代码:Java 在Android中使用线程发送消息,java,android,multithreading,android-alertdialog,Java,Android,Multithreading,Android Alertdialog,我有以下代码: if (value) { thread = new Thread() { @Override public void run() { try { while (!isConnected()) { synchronized (this) {
if (value) {
thread = new Thread() {
@Override
public void run() {
try {
while (!isConnected()) {
synchronized (this) {
wait(3000);
}
}
} catch (InterruptedException ex) {
}
if(wifiManager.isWifiEnabled()){
sendMessageWidget();
} else {
showWifiSettingsAlert();
}
}
};
thread.start();
}
我希望我的应用程序等到谷歌api客户端连接后再发送消息
断开连接方法的代码为:
public boolean isConnected() {
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected()) {
return true;
}
return false;
}
但我得到了这个错误信息:
NullPointerException:无法在未调用Looper.prepare()的线程内创建处理程序,并且它说错误在id showWifiSettingsAlert()的某处
代码如下:
public void showWifiSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
// Setting Dialog Title
alertDialog.setTitle("Location accuracy tips");
// Setting Dialog Message
alertDialog
.setMessage("You can improve the accuracy of your location by turning on\n- Wi-Fi");
// On pressing Settings button
alertDialog.setPositiveButton("Turn on",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
wifiManager.setWifiEnabled(true);
// Posalji poruke al pre toga jos jednom azuriraj
// lokaciju al ako je pozvana aplikacija iz widgeta
if (value) {
sendMessageWidget();
}
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
wifiManager.setWifiEnabled(false);
// Posalji poruke al pre toga jos jednom azuriraj
// lokaciju al ako je pozvana aplikacija iz widgeta
if (value) {
sendMessageWidget();
}
}
});
// Showing Alert Message
alertDialog.show();
}
我想,如果wifi未启用,用户可以选择启用或不启用,但无论哪种方式都应该发送消息。。。你能帮忙吗 您正在使用的是
mgoogleapclient.connect()代码>,这是一个异步方法,在线程中,这是不允许的
您可以尝试改用runOnUiThread
:
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//do your stuff here
}
});
由于不能从主线程以外的线程接触UI,因此必须将这些更改发布回UI线程及其循环器和相关处理程序。您可以通过创建与UI线程关联的处理程序(该处理程序将在任何地方工作,因为Looper.getMainLooper()
是一个静态调用)来显式执行此操作,例如:
或者,如果您在执行相同操作的活动中,则可以在runOnUiThread()
中的run()
方法中包装零件,而不使用任何处理程序
但是,您应该注意,这里实际上不需要使用任何线程。如果您遵循以下示例:您会发现,通过实现ConnectionCallbacks,OnConnectionFailedListener
,您可以从活动的onStart()
调用mGoogleApis.connect()
,当它连接或失败时,将在调用线程上执行相应的回调。比如说,
@Override
public void onConnected(Bundle connectionHint) {
if(wifiManager.isWifiEnabled()){
sendMessageWidget();
} else {
showWifiSettingsAlert();
}
}
实现同样的目标 因为您正试图从worker线程创建或更新应用程序视图,android不允许这样做,因为您只能在主应用程序UI线程中调用与视图相关的代码,因为我不知道sendMessageWidget()中的代码是什么代码>所以只需放置showwifisettingsart()
在runOnUiThread()
中。使用Handler或使用runOnUiThread执行此操作,选中sendMessageWidget仅从widget发送消息,没有什么特别的,您完全可以从任何线程调用它:它是异步的,只是意味着它将返回,而不会阻止当前线程执行,直到它完成连接或连接失败。从技术上讲,op应该适当地处理回调——也就是说,等待回调被触发;这样他就不用担心穿线了。但这不是他最初的问题。没问题,Handler h=new Handler(Looper.getMainLooper())
是一个方便的技巧,无论何时需要回到主线程:)
@Override
public void onConnected(Bundle connectionHint) {
if(wifiManager.isWifiEnabled()){
sendMessageWidget();
} else {
showWifiSettingsAlert();
}
}