0

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
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • How you're able to say. it works 2 times? whether the receiver is called from manifest or MainActivity? Whether the application should close after usb is unplugged? – Harsha Vardhan Oct 11 '14 at 17:47
  • Hello Harsha, The receivers are in the manifest. It works 2 times because I do it, the USB wire is just a physical trigger : Plug the usb launch the app, unplug usb close the app ... one more time, then the app doesn't launch. I'm thinking that android kill the apps because it crashes ... – Mathieu Guarino Oct 11 '14 at 20:15
  • If the intent is triggered properly then it should trigger everytime...check the logs and post the exception occured – Harsha Vardhan Oct 12 '14 at 14:56
  • Yeah I'm sure I'm doing something wrong, but don't know where ! I posted the exception, hope that can help you. I can give you the full log if you want, thanks for your time – Mathieu Guarino Oct 12 '14 at 15:33
  • 1
    You are trying to cast a non-activity Context to an Activity. You need to call finish() on the instance of Activity, not the broadcast receiver's Context. – Chris Stratton Oct 12 '14 at 15:40
  • Hello Chris, what should the broadcast receiver must do to activate the finish of the Activity, in other word what method the receiver must call and where do I put the finish() ? – Mathieu Guarino Oct 12 '14 at 16:03

1 Answers1

0

You can close the application in 2 ways.

  1. Trigger broadcast receiver of usb unplugged call programmatically rather than adding in manifest file. So that You can get instance of activity to finish.

  2. onRecieve of broadcast message, kill the process of the application (optionally, in worst case you can go with it)

android.os.Process.killProcess(android.os.Process.myPid());

Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22
  • You should never kill an app. – IAmGroot Oct 12 '14 at 18:30
  • @Doomsknight, I agree with you. but lot of apps uses, when there is a need of using it. – Harsha Vardhan Oct 12 '14 at 18:33
  • There is never need, unless you design your app badly. – IAmGroot Oct 12 '14 at 18:34
  • @Doomsknight, It is not about the design, Its about the usage and thinking about other apps also...Even I am supporting you but there are some cases and I found such converstaon in stackoverflow too. http://stackoverflow.com/questions/11035328/why-calling-process-killprocessprocess-mypid-is-a-bad-idea – Harsha Vardhan Oct 12 '14 at 18:45
  • Thanks for the advices Harsha :-) ! I understand your concern Doomsknight, and I agree, but as you see in my first post I'm a begginer in java so I don't know the good way of .. coding ? Moreover it's not going on the market, only the goal count. I hope for my next app I'll have more knowledge in java/android :-) – Mathieu Guarino Oct 12 '14 at 20:27
  • @helscream The issue is that you are casting `context` to `Activity` when it is not one. As seen in your log. My comments on killing a process are just a side note to this suggested answer. The linked question, although marked as "correct" in it being "ok", has far more support on the "dont do it" answers. You would be better served trying to close it down properly. See http://stackoverflow.com/questions/9888013/finishing-an-activity-from-broadcastreceiver-android?lq=1 and its duplicate – IAmGroot Oct 13 '14 at 08:20