Looper Internet, said there should be only one message loop handle , but I have multiple sub- threads to display a dialog box that how many do . And I live in the UI through handmessage messages are processed and displayed dialog box , did not show up . I do not know how else ?
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"); --------------------------------- ------- these two places are not showing dialog or 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"); ------------------------------- --------------- this place dialog box can be displayed
Looper.loop ();
try {
HashMap
if (map.isEmpty ()) {
Looper.prepare ();
createAlert ("Waring", "Empty devicelist"); ------------------------ this place and all of the following calls createAlert where dialog are not showing up
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 Xia ? ProductID Hua Ci Yu Liu ?
/ / 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 (" OK ", 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);
}
}
}
}
};
}
------ Solution ------------------------------------ --------
classmates, Dialog and Toast abandon it, your case is to use the most appropriate Log.
------ For reference only -------------------------------------- -
in addition, Looper.loop () after adding Looper.myLooper (). quit () is useless
------ For reference only ------------ ---------------------------
Looper.loop (); later entered the news cycle, you want to call quit () before exits , so you Looper.loop (); afterwards have not been implemented into the code .
you put Loop related code are removed, have changed handler.sendEmptyMessage (× × ×), and then united in the handler inside the handle bar, Toast or Dialog are displayed as you will .
But you should not be in a thread has demonstrated several Dialog, Dialog directly behind this cover in front of the Dialog displayed , you click OK , the previous display after Dialog , the latter will be displayed.
suggest you do not use so many Dialog and Toast, no meaning , with the Log tracking what processes better.
------ For reference only -------------------------------------- -
package 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 1和2的处理
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.main);
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) {
new MyThread2().start();
}
});
}
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();
}
class MyThread2 extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
// Log.v("test", String.valueOf(getLineNumber(new Exception())));
// createAlert("Waring", "MyThread2 start");// ----------------------------------------------这个地方的对话框可以显示出来
// Looper.loop();
handler.sendEmptyMessage(1);
try {
HashMap<String, UsbDevice> map = usbManager.getDeviceList();
if (map.isEmpty()) {
// Looper.prepare();
// createAlert("Waring", "Empty devicelist");// ------------------------这个地方和以下所有调用createAlert的地方对话框均显示不出来
// Looper.loop();
handler.sendEmptyMessage(2);
}
else
{
for (UsbDevice device : map.values()) {
Log.e("device",
"vid:" + device.getVendorId() + " pid:" + device.getProductId()
+ " " + device.getDeviceName());
if (5334 == device.getVendorId())
{
usbDevice = device;
}
//这里也不要用Dialog
createAlert("Warning",
"vid:" + device.getVendorId() + " pid:" + device.getProductId()
+ " " + device.getDeviceName());
}
if (usbManager.hasPermission(usbDevice)) {
handler.sendEmptyMessage(3);
// Looper.prepare();
// Log.v("test", String.valueOf(getLineNumber(new Exception())));
// createAlert("Waring", "MyThread3 start");
// Looper.loop();
new MyThread3().start();
} else {
handler.sendEmptyMessage(4);
// Looper.prepare();
// Log.v("test", String.valueOf(getLineNumber(new Exception())));
// createAlert("Waring", "requestPermission");
// Looper.loop();
usbManager.requestPermission(usbDevice, pendingIntent);
}
}
} catch (Exception e) {
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) {
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);
}
}
}
}
};
}
}
help you put the code probably changed a bit . The details you need to improve on the above said .
------ For reference only -------------------------------------- -
Thank you very much , but there are a few queries .
1. due MyThread3 () call inside the AlertDialog, but there is , then MyThread3 () This branch must have been executed. Before that performed handler.sendEmptyMessage (1); I handleMessage read as follows , but these did not appear toast .
public void handleMessage (Message msg) {
/ / TODO Auto-generated method stub
super.handleMessage (msg);
switch (msg.what) {
case 1:
Toast.makeText (TestUSB.this, "111111111111", 1000). show ();
break;
case 2:
Toast.makeText (TestUSB.this, "22222222222", 1000). show ();
break;
case 3:
Toast.makeText (TestUSB.this, "33333333333333333", 1000). show ();
break;
case 4:
Toast.makeText (TestUSB.this, "44444444444444444", 10000). show ();
break;
}
}
};
2. I said earlier, I'm calling looper.quit (), but the back is not executed , can I do this in a function which , for example , these two alert can show you ?
void fun1 ()
{
Looper.prepare ();
createAlert ("Warning", "111");
Looper.loop;
Looper.myLooper (). quit ();
Looper.prepare ();
createAlert ("Warning", "222");
Looper.loop;
Looper.myLooper (). quit ();
}
------ For reference only --------------------------------- ------
I verified it, describe two approaches are not feasible in a timely manner in front called quit, behind the alert will not be displayed .
------ For reference only -------------------------------------- -
no way to play LOG, because U port used by another device , do not use this method if you can only use WIFI ADB
------ For reference only ---------- -----------------------------
that use so many Dialog or Toast What significance does it ?
------ For reference only -------------------------------------- -
can display the data in the debugging process ah. Also, I found that as you say is not never displayed , but not seen on a tablet , but an android * 86 on the PC to see
------ For reference only ----- ----------------------------------
then play it, mad bomb Dialog. Debugging finished them all removed on the line.
没有评论:
发表评论