Backward compatible killbackgroundProcesses
Jul 26
Android, Development, General, Tutorial No Comments
Here is the solution I came up with for Shouvik.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /* onCreate() */ final Button killbutton = (Button) findViewById(R.id.killbutton); killbutton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (this.killMethod != null) { try { this.killMethod.invoke(this.activityManager, packageName); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } }); /* snip */ /* method in activity */ private void initializeKillMethod() { try { this.killMethod = ActivityManager.class.getMethod("killBackgroundProcesses", String.class); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } if (this.killMethod != null) { return; } try { this.killMethod = ActivityManager.class.getMethod("restartPackage", String.class); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } |