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