我正在创建一个活动,该活动在将USBDevice连接到Android设备时会对其进行检测,然后将文件复制到该USBDevice上,然后加载该文件并删除该文件。现在,我有一个BroadcastReceiver,由于某种原因,只要插入USBDevice,onReceive函数就会被连续调用,即使在移除设备后仍会继续,但是没有选中我的Checkbox。我只是不断看到onReceive函数中的println行。谁知道这是为什么吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
globalVars = ((GlobalVars) getApplicationContext());
strModel = globalVars.getModel();
strName = globalVars.getName();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_usb_host_test);
globalVars.ClearResultFile("USBHostTestResultFAIL.txt", this, Context.MODE_WORLD_READABLE);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
initViews();
registerReceiver(usbReceiver, filter);
Context context = getApplicationContext();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
context.registerReceiver(usbReceiver, filter);
}
public void initViews()
{
mPort1 = (CheckBox) findViewById(R.id.checkBoxPort1);
mPort2 = (CheckBox) findViewById(R.id.checkBoxPort2);
mPort3 = (CheckBox) findViewById(R.id.checkBoxPort3);
mTextViewMessage = (TextView) findViewById(R.id.textViewMessage);
mTextViewCopy = (TextView) findViewById(R.id.TextViewCopy);
mTextViewLoad = (TextView) findViewById(R.id.TextViewLoad);
mTextViewDelete = (TextView) findViewById(R.id.TextViewDelete);
mFailButton = (Button) findViewById(R.id.buttonFail);
color_default = Color.BLUE;
color_testing = Color.YELLOW;
color_pass = Color.GREEN;
color_fail = Color.RED;
mTextViewCopy.setTextColor(color_default);
mTextViewLoad.setTextColor(color_default);
mTextViewDelete.setTextColor(color_default);
mModel = (TextView) findViewById(R.id.textViewModel);
mModel.setText(strModel);
AllPassed = true;
mTextViewMessage.setText(getResources().getString(R.string.str_usb_host_test_6000_message));
CurrentPort = 0;
}
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println(action);
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
manager.requestPermission(device, permissionIntent);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
if (PortList[CurrentPort] != 1) {
if (CurrentPort < 4) {
switch (CurrentPort) {
case 1:
if (!CopyFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
if (!LoadFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
if (!DeleteFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
PassCurrentPort(CurrentPort);
CheckComplete();
break;
case 2:
if (!CopyFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
if (!LoadFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
if (!DeleteFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
PassCurrentPort(CurrentPort);
break;
case 3:
if (!CopyFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
if (!LoadFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
if (!DeleteFile(FileToCopy, CurrentPort))
FailCurrentPort(CurrentPort);
break;
default:
break;
}
if (CurrentPort < TotalPorts)
CurrentPort++;
}
}
}
} else {
Log.d(TAG, "permission denied for device " + device);
}
}
};
public boolean CopyFile(String Filename, int PortNumber)
{
AssetManager am = this.getAssets();
AssetFileDescriptor afd;
try {
afd = am.openFd(Filename);
File file;
file = new File("/mnt/ext_HSusb" + PortNumber + "/" + java.io.File.separator + Filename);
if(file.exists())
mTextViewCopy.setTextColor(color_pass);
file.createNewFile();
CopyToSDCard(afd.getFileDescriptor(), file);
} catch (IOException e) {
e.printStackTrace();
mTextViewCopy.setTextColor(color_fail);
return false;
}
mTextViewCopy.setTextColor(color_pass);
return true;
}
public static void CopyToSDCard(FileDescriptor src, File dst) throws IOException
{
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally
{
if (inChannel != null)
inChannel.close();
}
}
public boolean LoadFile(String Filename, int PortNumber)
{
File file = new File("/mnt/ext_HSusb" + PortNumber + "/" + java.io.File.separator + Filename);
if (file.exists())
mTextViewLoad.setTextColor(color_pass);
else
mTextViewLoad.setTextColor(color_fail);
return file.exists();
}
public boolean DeleteFile(String Filename, int PortNumber)
{
File file = new File("/mnt/ext_HSusb" + PortNumber + "/" + java.io.File.separator + Filename);
boolean deleted = file.delete();
if (deleted)
mTextViewDelete.setTextColor(color_pass);
else
mTextViewDelete.setTextColor(color_fail);
return deleted;
}
public void FailCurrentPort(int PortNumber)
{
PortList[PortNumber] = 1;
switch (PortNumber)
{
case 1:
mPort1.setTextColor(color_fail);
mPort2.setTextColor(color_testing);
break;
case 2:
mPort2.setTextColor(color_fail);
mPort3.setTextColor(color_testing);
break;
case 3:
mPort3.setTextColor(color_fail);
break;
}
AllPassed = false;
mTextViewCopy.setTextColor(color_default);
mTextViewLoad.setTextColor(color_default);
mTextViewDelete.setTextColor(color_default);
CurrentPort++;
step = 0;
}