Android子线程中显示对话框和toast的问题
一般而言,Android的子线程中如果要显示UI相关,需要在显示的前后添加Looper.prepare和Looper.loop.但是实践中发现点问题,代码如下,并且把问题标志出来了。在第一个子线程MyThread2开头可以显示对话框,但是后面的对话框以及MyThread3中用同样方法却显示不了。
网上说应该只有一个Looper处理消息循环,但是我的多个子线程中均要在多处显示对话框该怎么办。而且我通过handmessage在UI现场中处理消息并显示对话框,也没显示出来。不知道是怎么回事?
package com.example.testusb;
import java.nio.ByteBuffer;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.*;
public class TestUSB extends Activity {
private Button button,button2;
private UsbManager usbManager;
private UsbDevice usbDevice;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private PendingIntent pendingIntent;
Intent intent;
StringBuffer sb = new StringBuffer();
private TextView info;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 3:
createAlert("TestUSB","33333333333333333");----------------------------------------这两个地方均没有显示出对话框或者toast
Toast.makeText(TestUSB.this, "33333333333333333", 1000).show();
break;
case 4:
createAlert("TestUSB","44444444444444444");
Toast.makeText(TestUSB.this, "44444444444444444", 1000).show();
break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
button = (Button) findViewById(R.id.button);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyThread2().start();
}
});
}
class MyThread2 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
createAlert("Waring","MyThread2 start");----------------------------------------------这个地方的对话框可以显示出来
Looper.loop();
try {
HashMap<String, UsbDevice> map = usbManager.getDeviceList();
if (map.isEmpty()) {
Looper.prepare();
createAlert("Waring","Empty devicelist");------------------------这个地方和以下所有调用createAlert的地方对话框均显示不出来
Looper.loop();
}
else
{
for(UsbDevice device : map.values()){
Log.e("device", "vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
if(5334 == device.getVendorId())
{
usbDevice = device;
}
createAlert("Warning","vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
}
if(usbManager.hasPermission(usbDevice)){
handler.sendEmptyMessage(3);
Looper.prepare();
createAlert("Waring","MyThread3 start");
Looper.loop();
new MyThread3().start();
}else{
handler.sendEmptyMessage(4);
Looper.prepare();
createAlert("Waring","requestPermission");
Looper.loop();
usbManager.requestPermission(usbDevice, pendingIntent);
}
}
/* for(UsbDevice device : map.values()){
sb.append( "vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
sb.append("\n");
info.setText(sb);
Log.e("device", "vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
//VendorID 鍜?ProductID 鍗佽繘鍒?
// if(5334 == device.getVendorId() && 12290 == device.getProductId())
if(5334 == device.getVendorId())
{
usbDevice = device;
}
createAlert("Warning","vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
}
if(usbManager.hasPermission(usbDevice)){
handler.sendEmptyMessage(3);
sb.append("MyThread3 start");
sb.append("\n");
info.setText(sb);
Looper.prepare();
createAlert("Waring","MyThread3 start");
Looper.loop();
new MyThread3().start();
}else{
handler.sendEmptyMessage(4);
sb.append("requestPermission");
sb.append("\n");
info.setText(sb);
Looper.prepare();
createAlert("Waring","requestPermission");
Looper.loop();
usbManager.requestPermission(usbDevice, pendingIntent);
}*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyThread3 extends Thread{
@Override
public void run() {
Looper.prepare();
createAlert("Warning","My thread3 receive data");
Looper.loop();
}
private void createAlert(String strTitle, String strMsg) {
Dialog alertDialog = new AlertDialog.Builder(TestUSB.this)
.setTitle(strTitle).setMessage(strMsg)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
setResult(RESULT_OK, intent);
//finish();
}
}).create();
alertDialog.show();
}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e( "action", action);
sb.append("action start");
sb.append("\n");
info.setText(sb);
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
handler.sendEmptyMessage(1);
if(usbDevice != null){
sb.append("BroadcastReceiver MyThread3 start");
sb.append("\n");
info.setText(sb);
new MyThread3().start();
}
}
else {
sb.append("permission denied for device"+usbDevice.toString());
sb.append("\n");
info.setText(sb);
Log.d("denied", "permission denied for device " + usbDevice);
}
}
}
}
};
}
网上说应该只有一个Looper处理消息循环,但是我的多个子线程中均要在多处显示对话框该怎么办。而且我通过handmessage在UI现场中处理消息并显示对话框,也没显示出来。不知道是怎么回事?
package com.example.testusb;
import java.nio.ByteBuffer;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.*;
public class TestUSB extends Activity {
private Button button,button2;
private UsbManager usbManager;
private UsbDevice usbDevice;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private PendingIntent pendingIntent;
Intent intent;
StringBuffer sb = new StringBuffer();
private TextView info;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 3:
createAlert("TestUSB","33333333333333333");----------------------------------------这两个地方均没有显示出对话框或者toast
Toast.makeText(TestUSB.this, "33333333333333333", 1000).show();
break;
case 4:
createAlert("TestUSB","44444444444444444");
Toast.makeText(TestUSB.this, "44444444444444444", 1000).show();
break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
button = (Button) findViewById(R.id.button);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyThread2().start();
}
});
}
class MyThread2 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
createAlert("Waring","MyThread2 start");----------------------------------------------这个地方的对话框可以显示出来
Looper.loop();
try {
HashMap<String, UsbDevice> map = usbManager.getDeviceList();
if (map.isEmpty()) {
Looper.prepare();
createAlert("Waring","Empty devicelist");------------------------这个地方和以下所有调用createAlert的地方对话框均显示不出来
Looper.loop();
}
else
{
for(UsbDevice device : map.values()){
Log.e("device", "vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
if(5334 == device.getVendorId())
{
usbDevice = device;
}
createAlert("Warning","vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
}
if(usbManager.hasPermission(usbDevice)){
handler.sendEmptyMessage(3);
Looper.prepare();
createAlert("Waring","MyThread3 start");
Looper.loop();
new MyThread3().start();
}else{
handler.sendEmptyMessage(4);
Looper.prepare();
createAlert("Waring","requestPermission");
Looper.loop();
usbManager.requestPermission(usbDevice, pendingIntent);
}
}
/* for(UsbDevice device : map.values()){
sb.append( "vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
sb.append("\n");
info.setText(sb);
Log.e("device", "vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
//VendorID 鍜?ProductID 鍗佽繘鍒?
// if(5334 == device.getVendorId() && 12290 == device.getProductId())
if(5334 == device.getVendorId())
{
usbDevice = device;
}
createAlert("Warning","vid:"+device.getVendorId()+" pid:"+device.getProductId()+" "+device.getDeviceName());
}
if(usbManager.hasPermission(usbDevice)){
handler.sendEmptyMessage(3);
sb.append("MyThread3 start");
sb.append("\n");
info.setText(sb);
Looper.prepare();
createAlert("Waring","MyThread3 start");
Looper.loop();
new MyThread3().start();
}else{
handler.sendEmptyMessage(4);
sb.append("requestPermission");
sb.append("\n");
info.setText(sb);
Looper.prepare();
createAlert("Waring","requestPermission");
Looper.loop();
usbManager.requestPermission(usbDevice, pendingIntent);
}*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyThread3 extends Thread{
@Override
public void run() {
Looper.prepare();
createAlert("Warning","My thread3 receive data");
Looper.loop();
}
private void createAlert(String strTitle, String strMsg) {
Dialog alertDialog = new AlertDialog.Builder(TestUSB.this)
.setTitle(strTitle).setMessage(strMsg)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
setResult(RESULT_OK, intent);
//finish();
}
}).create();
alertDialog.show();
}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e( "action", action);
sb.append("action start");
sb.append("\n");
info.setText(sb);
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
handler.sendEmptyMessage(1);
if(usbDevice != null){
sb.append("BroadcastReceiver MyThread3 start");
sb.append("\n");
info.setText(sb);
new MyThread3().start();
}
}
else {
sb.append("permission denied for device"+usbDevice.toString());
sb.append("\n");
info.setText(sb);
Log.d("denied", "permission denied for device " + usbDevice);
}
}
}
}
};
}
...全文
相关推荐
Android中FTP上传、下载的功能实现(含进度)
本篇文章主要介绍了Android中FTP上传、下载(含进度),具有一定的参考价值,有需要的可以了解一下。
Android开发踩坑之子线程中调用Toast
前言 今天在编写Android应用开发的登录程序时发生了一个问题:用户点击登录按钮,主界面会弹出一个窗口接收用户输入,当用户点击确定后,程序调用等待进度条等待3秒钟,接着弹出“用户登录成功”的Toast提示框,然后…就没有然后了,程序直接奔溃。 原因检查 尝试过debug、也上网找了一阵子,最终发现原来问题出现在程序调用进度条ProgressDialog类的时候:ProgressDialog...
Android学习路由
不错的Android学习范围指导资料, 可以帮助指引确立学习目标
Android特效 五种Toast详解
NULL 博文链接:https://george-ghc.iteye.com/blog/1101717
Android-dialog库可以在任意类内调用子线程或ui线程内均可显示
dialog库,可以在任意类内调用,子线程或ui线程内均可显示


