Monday, April 14, 2014

Android child thread display the dialog box and toast problem


In general, Android sub-thread if you want to display UI related, you need to add Looper.prepare front display and Looper.loop., But in practice find point problem, as follows, and the question mark out. At the beginning of the first sub-thread MyThread2 can display the dialog box, but the dialog box using the same method and MyThread3 back but can not be displayed.
Online said Looper treatment should be only one news cycle, but my multiple sub-threads have to do this how many displayed in the dialog box. And I live in the UI handled by handmessage messages and displays the 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 did not show 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 map = usbManager.getDeviceList ();
if (map.isEmpty ()) {
Looper.prepare ();
createAlert ("Waring", "Empty devicelist"); ------------------------ this place and where all of the following calls createAlert dialog does not show out
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 Coffee &? ProductID Ticijuerou?
/ / 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);
}
}
}
}
};

}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
In addition, Looper.loop () after adding Looper.myLooper (). Quit () is useless
Reply:
Looper.loop (); then entered the news cycle, you want to call quit () will not quit, so you Looper.loop (); codes are not executed after the.
You put Loop related code are removed, have changed handler.sendEmptyMessage (× × ×), then the handler inside unity deal with it, Toast or Dialog are displayed as you will.
But you should not have displayed several Dialog in a thread, so that the back of the Dialog is displayed directly on the front cover of Dialog, you click OK, the front Dialog shows later, the back will be displayed.
I suggest you do not use so much Dialog and Toast, no meaning, with the Log to track a little better about the process.
Reply:
 
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) {
/ / This increase case 1 and 2 deal with
case 3:
/ / CreateAlert ("TestUSB", "33333333333333333") ;/ / ----------------------------------- ----- these two places did not show 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.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 ("OK", 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") ;/ / ---------------------------------- ------------ This place dialog box can be displayed
/ / Looper.loop ();
handler.sendEmptyMessage (1);

try {

HashMap map = usbManager.getDeviceList ();
if (map.isEmpty ()) {
/ / Looper.prepare ();
/ / CreateAlert ("Waring", "Empty devicelist") ;/ / ------------------------ this place and where all of the following calls createAlert dialogue boxes are not showing up
/ / 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;
}
/ / Here is not to use 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 ("OK", 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. You need to say that according to the above details to perfection.
Reply:
Thank you very much, but there are a few questions.
1 Since () inside MyThread3 call 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 called looper.quit (), but the back is not executed, can I do this in a function which, for example, two alert can show it?
void fun1 ()
{
Looper.prepare ();
createAlert ("Warning", "111");
Looper.loop;
Looper.myLooper () quit ();.
Looper.prepare ();
createAlert ("Warning", "222");
Looper.loop;
Looper.myLooper () quit ();.



}
Reply:
I verified it, describing two of the practice is not feasible, promptly called the front quit, behind the alert is not displayed.
Reply:
Students, Dialog and Toast abandon it, your situation is to use the most suitable Log.
Reply:
No way to fight LOG, because U port is occupied by another device, if not this method can only be used WIFI ADB
Reply:
reference to the 7th floor bjskyhorse reply:
no way to fight LOG, because U port is occupied by another device, this method only if you do not use WIFI ADB

That so many Dialog or Toast with what is the point?
Reply:
Data can be displayed during debugging ah. Also, I found did not follow your argument is not displayed, but you do not see on a tablet, but an android * 86 on the PC to see the
Reply:
reference to the 9th floor bjskyhorse reply:
data can be displayed during debugging ah. Also, I found did not follow your argument is not displayed, but you do not see on a tablet, but an android * 86 on the PC to see the

Then bomb it, mad bomb Dialog. Finished debugging them are removed on the line.

No comments:

Post a Comment