I'm currently working on an android app which must launch a video when the USB is plugged, and close the video when the USB is unplugged. The current problem is : it works only two times, after that the application doesn't launch (in log cat : bad process ...). I'm totally a begginer in android (and java too). This is how my project works : MainActivity Launch video which turn on in a loop. I've set a receiver which launch the MainActivity when the USB is plugged. I've another receiver which is triggered when the USB is unplugged
this is the Activity which kill my MainActivity :
public class OffPowerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Intent i = new Intent(context, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).finish();
}
}
I'm sure I don't do things well, and maybe it's not the right things.
This is my end of mainactivity :
@Override
protected void onDestroy() {
super.onDestroy();
}
Thanks for your help !
log :
10-12 17:26:01.159: E/AndroidRuntime(21814): FATAL EXCEPTION: main
10-12 17:26:01.159: E/AndroidRuntime(21814): Process: com.example.video1, PID: 21814
10-12 17:26:01.159: E/AndroidRuntime(21814): java.lang.RuntimeException: Unable to start receiver com.example.video1.OffPowerReceiver: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
10-12 17:26:01.159: E/AndroidRuntime(21814): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2414)
10-12 17:26:01.159: E/AndroidRuntime(21814): at android.app.ActivityThread.access$1700(ActivityThread.java:135)
10-12 17:26:01.159: E/AndroidRuntime(21814): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
10-12 17:26:01.159: E/AndroidRuntime(21814): at android.os.Handler.dispatchMessage(Handler.java:102)
10-12 17:26:01.159: E/AndroidRuntime(21814): at android.os.Looper.loop(Looper.java:136)
10-12 17:26:01.159: E/AndroidRuntime(21814): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-12 17:26:01.159: E/AndroidRuntime(21814): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 17:26:01.159: E/AndroidRuntime(21814): at java.lang.reflect.Method.invoke(Method.java:515)
10-12 17:26:01.159: E/AndroidRuntime(21814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-12 17:26:01.159: E/AndroidRuntime(21814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-12 17:26:01.159: E/AndroidRuntime(21814): at dalvik.system.NativeStart.main(Native Method)
10-12 17:26:01.159: E/AndroidRuntime(21814): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
10-12 17:26:01.159: E/AndroidRuntime(21814): at com.example.video1.OffPowerReceiver.onReceive(OffPowerReceiver.java:15)
10-12 17:26:01.159: E/AndroidRuntime(21814): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2407)
10-12 17:26:01.159: E/AndroidRuntime(21814): ... 10 more
10-12 17:26:05.223: W/BroadcastQueue(575): Unable to launch app com.example.video1/10091 for broadcast Intent { act=android.intent.action.ACTION_POWER_CONNECTED flg=0x4000010 }: process is bad