0

我已经为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
4

1 回答 1

1

您应该阅读文档:

ACTION_UMS_CONNECTED

不推荐使用此常量。取而代之 android.os.storage.StorageEventListener

广播动作:设备已进入 USB 大容量存储模式。这主要用于 USB 设置面板。应用程序应侦听 ACTION_MEDIA_MOUNTED 和 ACTION_MEDIA_UNMOUNTED 广播,以便在挂载或卸载 SD 卡文件系统时收到通知


反对你的ClassNotFoundException

在您的清单中,您在 BoradcastReceiver 的类名前面加上一个句点,这是一个快捷方式。它将采用您在<manifest>-tags package-attribute 中指定的包名并将给定的类名添加到其中。

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


这里已经提出了一个类似的问题:Android Broadcast Receiver Error: Class not found exception with no accepted or up-voted answer so far。

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

另一种解释可能是您的旧应用程序版本的更新问题,如下所述:升级 Android 应用程序时的 ClassNotFoundException 错误报告阅读这个问题,它也可能是 App2SD 问题。

于 2012-05-25T10:36:08.183 回答