Android设备的USB检测代码崩溃

时间:2012-05-25 10:05:35

标签: java android usb

我已经为Android设备编写了一个用于usb检测的小代码,但是当我将设备电缆连接到计算机的CPU时它崩溃了

这是我的代码

public class IntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // TODO Auto-generated method stub

        if(intent.getAction().equals(Intent.ACTION_UMS_CONNECTED))

        {

            Toast.makeText(context, "mounted", Toast.LENGTH_LONG).show();
            Log.i("ANDROID DEVICE IS","CONNECTED");
            System.out.println(" ANDROID DEVICE IS CONNECTED");
            // Intent myStarterIntent = new Intent(context, CarHome.class);
            //   myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //   context.startActivity(myStarterIntent);
        }

        else if(intent.getAction().equals(Intent.ACTION_UMS_DISCONNECTED)){

            Toast.makeText(context, "Unmounted", Toast.LENGTH_LONG).show();
            Log.i("ANDROID DEVICE IS","DISCONNECTED");  
            System.out.println(" ANDROID DEVICE IS NOT CONNECTED");
        }
    }

}

清单文件在这里

<receiver android:name=".IntentReceiver">
    <intent-filter>
      <action android:name="android.intent.action.ACTION_UMS_CONNECTED" />
      <action android:name="android.intent.action.ACTION_UMS_DISCONNECTED" />
      <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
    </intent-filter>
</receiver>

Logcat显示这些错误

05-25 15:30:08.350: E/AndroidRuntime(3457):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1785)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at android.app.ActivityThread.access$2400(ActivityThread.java:121)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at android.os.Looper.loop(Looper.java:130)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at android.app.ActivityThread.main(ActivityThread.java:3701)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at java.lang.reflect.Method.invoke(Method.java:507)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at dalvik.system.NativeStart.main(Native Method)
05-25 15:30:08.350: E/AndroidRuntime(3457): Caused by: java.lang.ClassNotFoundException: com.kk88655.hello.IntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.kk88655.hello-2.apk]
05-25 15:30:08.350: E/AndroidRuntime(3457):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-25 15:30:08.350: E/AndroidRuntime(3457):     at  java.lang.ClassLoader.loadClass(ClassLoader.java:511)

最新的logcat消息

05-25 17:51:32.878: E/AndroidRuntime(5602): FATAL EXCEPTION: main
05-25 17:51:32.878: E/AndroidRuntime(5602): java.lang.RuntimeException: Unable to instantiate receiver com.kk88655.hello.IntentReceiver: java.lang.ClassNotFoundException: com.kk88655.hello.IntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.kk88655.hello-1.apk]
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1785)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.access$2400(ActivityThread.java:121)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.os.Looper.loop(Looper.java:130)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.main(ActivityThread.java:3701)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.reflect.Method.invoke(Method.java:507)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at dalvik.system.NativeStart.main(Native Method)
05-25 17:51:32.878: E/AndroidRuntime(5602): Caused by: java.lang.ClassNotFoundException: com.kk88655.hello.IntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.kk88655.hello-1.apk]
05-25 17:51:32.878: E/AndroidRuntime(5602):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1776)
05-25 17:51:32.878: E/AndroidRuntime(5602):     ... 10 more
05-25 17:51:32.878: W/ActivityManager(238):   Force finishing activity com.kk88655.hello/.CarHome

1 个答案:

答案 0 :(得分:1)

你应该阅读文档:

  

ACTION_UMS_CONNECTED

     

此常量已弃用。取而代之   android.os.storage.StorageEventListener

     

广播操作:设备已进入USB大容量存储模式。这个   主要用于USB设置面板。应用应该倾听   ACTION_MEDIA_MOUNTED和ACTION_MEDIA_UNMOUNTED广播是   安装或卸载SD卡文件系统时通知


针对您的ClassNotFoundException

在你的清单中,你用BoradcastReceiver的class-name作为前缀,这是一个捷径。它将使用您在<manifest> - 标记package - 属性中指定的包名称,并将给定的类名添加到其中。

因此,您应确保您的完全限定类路径为com.kk88655.hello.IntentReceiver,因为您似乎已将hello - 包重命名为hello2(如logcat中所示: /data/app/com.kk88655.hello-2.apk)。


此处已经提出了一个类似的问题:Android Broadcast Receiver Error: Class not found exception到目前为止没有接受或已投票的答案。

另一个想法是在<receiver> - 标记android:name - 属性中指定完全限定的包名称,如下所示:Unable to instantiate receiver in BroadcastReceiver SMS

另一种解释可能是旧版应用程序版本的更新问题,如下所述:ClassNotFoundException error reports when upgrading Android app阅读此问题,它也可能是App2SD问题。