I have written a small code for usb detection for android device but its crashing while I connect the device cable to CPU of my computer
Here is my code
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");
}
}
}
Manifest file is here
<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 showing these errors
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)
Latest logcat messages
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
You should read the docs:
ACTION_UMS_CONNECTED
This constant is deprecated . replaced by
android.os.storage.StorageEventListener
Broadcast Action: The device has entered USB Mass Storage mode. This is used mainly for the USB Settings panel. Apps should listen for ACTION_MEDIA_MOUNTED and ACTION_MEDIA_UNMOUNTED broadcasts to be notified when the SD card file system is mounted or unmounted
Against your ClassNotFoundException
:
In your manifest, you prefixed the class-name of your BoradcastReceiver with a period, which is a shortcut. It will take the package-name you specified in your <manifest>
-tags package
-attribute and add the given class-name to it.
So, you should make sure that your fully qualified class-path is com.kk88655.hello.IntentReceiver
, because it seems that you renamed the hello
-package to hello2
(as seen in the logcat: /data/app/com.kk88655.hello-2.apk
).
A similar question has already been asked here: Android Broadcast Receiver Error: Class not found exception with no accepted or up-voted answer so far.
Another idea would be to specify the fully-qualified package-name in the <receiver>
-tags android:name
-attribute, as suggested here: Unable to instantiate receiver in BroadcastReceiver SMS
Another explanation might be an update-issue with your older application-version as discussed here: ClassNotFoundException error reports when upgrading Android app Reading this question, it also might be an App2SD problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.