13

My Android app has a WebView that requires the Flash plugin. The webview will provide a Market link to Adobe Flash if it's not installed. After installing Flash, the only way to instantiate it in my app is to force quit and restart the app. I would like to present a button to the user that does this for them because it's too complicated for casual users to open up their running processes and force quit the app manually. What is the Java code to implement such a restart button?

JoJo
  • 19,587
  • 34
  • 106
  • 162
  • 3
    Why exactly do you need to force quit and restart your app to initialize things? Just call the initializion code again after flash is installed.. – Cheryl Simon Jun 29 '11 at 23:25
  • 1
    Do you really need to force close the app itself? Wouldn't just closing the Activity which is hosting the WebView be enough? – Squonk Jun 29 '11 at 23:28
  • The WebView's Javascript fails to find Flash if the app is not restarted. I can reinstantiate the WebView and reset its plugins all I want, but it won't pick up Flash until the app is restarted. – JoJo Jun 30 '11 at 00:05
  • I have been seeing a similar problem with javascript intermittently not running after reloading a web page into a WebView under Android 4.3. The problem goes away in 4.4, probably because Google replaced the guts of WebView with chrome. I've been looking for a workaround for weeks -- I'll give this a try -- it sometimes takes a day or two for the issue to appear. – Gdalya Jun 18 '14 at 10:38

3 Answers3

30

You can restart your app in two steps:

  1. Kill your own process. Use Process.myPid(), pass it into Process.killProcess(). You might need to add a permission to your manifest (possibly android.permission.KILL_BACKGROUND_PROCESSES) to get this to work.
  2. Before you kill your own process, register an Alarm with a pending intent to restart your Activity in the very near future. The second parameter to alarm.set is the triggerAtTime. Setting it to 0 causes the alarm to fire immediately. The example here sets a time to launch the app one second into the future.

The Code goes like this:

AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()), 0));
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
mportuesisf
  • 5,587
  • 2
  • 33
  • 26
  • How do I access this `Process` object from within my main `Activity`? – JoJo Jun 30 '11 at 00:40
  • These methods on Process are static. You should just be able to call them. – mportuesisf Jun 30 '11 at 00:44
  • Yes, this works. We have an admin button that does a clean shutdown of various things before calling `Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL)` – jtoberon Jun 30 '11 at 13:53
  • @mportuesisf . Now I have an `alarm.set()` followed by a `Process.killProcess()`. How do I make the alarm trigger *after* the process is killed? I've replaced the `0` with `System.currentTimeMillis() + 1000`, but I do not see the activity restart 1 second later. Perhaps when the process is killed, all of its alarms are also cleaned up. – JoJo Jun 30 '11 at 21:59
  • Yes, that was the speculative part of my suggestion. I knew the `Process.killProcess()` would do the trick. What I wasn't sure about was whether the system would keep the alarms set by the process, or kill the alarms with the process. It sounds like the latter happens. – mportuesisf Jun 30 '11 at 22:40
  • One thing you could consider doing is calling `Process.sendSignal()` instead, as suggested by @jtoberon three comments above this one. It's possible that `sendSignal()` will work at a lower level, whereas `killProcess()` does cleanup work. – mportuesisf Jun 30 '11 at 22:42
  • His variant of killing the process didn't help. Another interesting observation is that after removing all kill calls, the alarm doesn't even work by itself. Nothing happens. No errors either. It has only worked when the time is set to `0`. I have a feeling that `System.currentTimeMillis() + 1000` is wrong... – JoJo Jun 30 '11 at 22:57
  • 1
    Try changing the constant `AlarmManager.ELAPSED_REALTIME` to `AlarmManager.RTC`. `RTC` is wall clock time in UTC, `ELAPSED_REALTIME` is the time since boot. (You may need to adjust for timezone to UTC as well). – mportuesisf Jun 30 '11 at 23:13
  • That fixed the problem. Killing the process doesn't actually clean up the alarm. I was just setting the alarm incorrectly. Now the full restart works beautifully. I hope 1 second is enough for most Android phones to force quit.... – JoJo Jun 30 '11 at 23:18
  • Quite excellent! Glad it works. – mportuesisf Jun 30 '11 at 23:23
  • why not to use System.exit()? It doesn't require any permissions – Vasily Makarov Sep 18 '16 at 20:06
0

I suppose you could call finish to make your activity stop but you will not be able to make your app start back up again. That would require (if it is even possible) root and it would be a terrible idea. It would mean that any app could start itself whenever it wanted to: which is just a bad idea and would actually be a bug if it was possible.

Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
  • Does `Activity.finish` actually force quit the app? It seems like it only stores the app in memory. When I ran the code, it did close the app. But when I went to manage the app, the "Force stop" button was still activated. That button should be disabled if the app no where to be found in memory. For some reason, Flash requires the app to be out of memory and then restarted for it to be recognized by the `WebView`. – JoJo Jun 30 '11 at 00:15
  • @JoJo: Activity.finish only closes the current activity. Did you finish all of the activities in the app or just one? Because if any activities or background processes still exist then naturally the App will still exist too. – Robert Massaioli Jun 30 '11 at 00:50
  • I added `this.finish()` at the end of `Activity.onCreate`. So when you launch the app, it immediately closes. Shouldn't this cause the one and only activity to be closed? – JoJo Jun 30 '11 at 01:51
  • @JoJo: Yes that should work. I don't know why it is not closing an App with no activities. What version of Android are you on? – Robert Massaioli Jun 30 '11 at 05:25
  • I'm actually seeing similar behavior on 2.2 (Motorola Droid (1)). Calling finish() exits the activity, but it still shows up under "running applications". Also, the "force stop" button doesn't appear to do anything. – snapfractalpop Mar 22 '12 at 23:15
-1

you can exit of app with System.exit(0) : this doesn't require permission for your app

arnouf
  • 790
  • 2
  • 7
  • 16