【发布时间】:2015-06-29 08:44:09
【问题描述】:
我创建了一个简单的activity 来测试intent 的传入,方法是将USB 设备附加/分离到我的Android 平板电脑中。我启动activity 并尝试附加USB,此时弹出错误“应用程序...停止工作。” 出现。当我在连接USB 电缆的情况下启动应用程序并尝试断开USB 时,会出现同样的情况。
提示:此示例仅用于测试。我需要写一个service,它会自动通过USB从设备接收数据并将它们上传到服务器(这部分已经可以运行了)。现在我处理如何自动启动service,最好的将在USB设备连接的那一刻出现。
考虑1:问题可能是device_filter的方式不对?系统向 Receiver 发送意图,他想处理它,但没有找到 device_filter 并且比下降?我尝试路径:TestUSB.xml.device_filter.xml 或 TestUSB.Resources.xml.device_filter.xml 但没有成功。
注意事项 2:AndroidManifest.xml 配置不正确,缺少一些权限?
注意事项 3:还是平板电脑/操作系统有问题?目前我有一个不同的硬件来测试它。
我使用的是平板 alps 874v3 android 4.4.2,USB 设备是我们的特殊硬件,带有 FTDI 芯片。
我使用 Visual Studio 2010 和 Xamarin 在 .net 中编写 Android 应用程序
[Activity(Label = "TestUSB", MainLauncher= true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 0;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
}
public class UsbBroadcastReceiver : BroadcastReceiver
{
public UsbBroadcastReceiver()
{
}
public override void OnReceive(Context context, Intent intent)
{
InvokeAbortBroadcast();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="TestUSB.TestUSB" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" />
<application android:label="TestUSB" android:icon="@drawable/Icon" android:launchMode="singleTask">
<receiver android:enabled="true" android:exported="true" android:name=".UsbBroadcastReceiver" android:launchMode="singleTask">
<uses-feature android:name="android.hardware.usb.host" android:required="false" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
</receiver>
</application>
</manifest>
device_filter.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--<usb-device vendor-id="0403" product-id="6001" /> FTDI FT232R UART in hexa-->
<usb-device vendor-id="1027" product-id="24577" /><!--FTDI FT232R UART in decimal-->
</resources>
**编辑:** 我又借了一部华为新手机试了试。它就像一个平板电脑
实际日志
D/ActivityManager( 654): AP_PROF:AppLaunch_LaunchTime:TestUSB.TestUSB/md537e255c671aa66f9ada9a57df4bd0038.MainActivity:1104:3576760
D/ActivityManager( 654): ACT-IDLE_NOW_MSG from windowsVisible() for idle: ActivityRecord{41fd3280 u0 TestUSB.TestUSB/md537e255c671aa66f9ada9a57df4bd0038.MainActivity t33}
W/UsbSettingsManager( 654): no meta-data for ResolveInfo{4266c658 TestUSB.TestUSB/md537e255c671aa66f9ada9a57df4bd0038.MainActivity m=0x108000}
W/System.err( 7880): java.lang.RuntimeException: Unable to instantiate receiver
TestUSB.TestUSB.UsbBroadcastReceiver: java.lang.ClassNotFoundException:
Didn't find class "TestUSB.TestUSB.UsbBroadcastReceiver" on path:
DexPathList[[zip file "/data/app/TestUSB.TestUSB-1.apk"],nativeLibraryDirectories=[/data/app-lib/TestUSB.TestUSB-1,
/vendor/lib, /system/lib]]
VS+Xamarin 生成的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="TestUSB.TestUSB" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<application android:label="TestUSB" android:icon="@drawable/icon" android:name="mono.android.app.Application" android:debuggable="true">
<receiver android:enabled="true" android:exported="true" android:name=".UsbBroadcastReceiver" android:launchMode="singleTask">
<uses-feature android:name="android.hardware.usb.host" android:required="false" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter" />
<!--<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter" />-->
</receiver>
<activity android:icon="@drawable/icon" android:label="TestUSB" android:name="md537e255c671aa66f9ada9a57df4bd0038.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
</activity>
<provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="2147483647" android:authorities="TestUSB.TestUSB.mono.MonoRuntimeProvider.__mono_init__" />
<receiver android:name="mono.android.Seppuku">
<intent-filter>
<action android:name="mono.android.intent.action.SEPPUKU" />
<category android:name="mono.android.intent.category.SEPPUKU.TestUSB.TestUSB" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
【问题讨论】:
-
您没有任何来自此错误的崩溃日志?你的调试器不会在这种情况下停止吗?
-
我没有任何日志,因为在测试中 android 没有连接到 PC。从较新的 Android 版本开始 - aLogCat 无法处理来自其应用程序外部的消息。我尝试将我的登录代码写入文件,但在到达之前代码崩溃了。
-
崩溃后您始终可以从设备获取系统日志。请参阅:sites.google.com/site/androidhowto/how-to-1/… 然后只需搜索神奇的单词“死”(在它前面加上你的包名),你就会得到你的堆栈跟踪。
-
感谢您的信息。我在问题中添加了日志。日志有关于不存在接收器类的警告。我在 androidmanifest 中注册了接收器,但不是一个活动。在 VS + xamarin 中,活动填充到 androidManifest 中,由 Xamarin 自动填充并更改其名称。问题可能在哪里?我添加了由 xamarin 生成的 AndroidManifest。
-
Xamarin 具有属性 [BroadcastReceiver] - 将其放在您的 UsbBroadcastReceiver 上方,以便 Xamarin 在构建过程中注册它。您可能还想使用 [IntentFilter] 属性,请参阅 xamarin 上的文档操作/示例
标签: c# android xamarin usb ftdi