Blogger Information
Blog 143
fans 1
comment 0
visits 440324
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实现重启App的功能
弘德誉曦的博客
Original
4050 people have browsed it

实现重启App的功能

自杀式服务重启app完整代码

  1. /**
  2. * 此工具类用来重启APP,只是单纯的重启,不做任何处理。
  3. * Created by 13itch on 2016/8/5.
  4. */
  5. public class RestartAPPTool {
  6. /**
  7. * 重启整个APP
  8. * @param context
  9. * @param Delayed 延迟多少毫秒
  10. */
  11. public static void restartAPP(Context context,long Delayed){
  12. /**开启一个新的服务,用来重启本APP*/
  13. Intent intent1=new Intent(context,killSelfService.class);
  14. intent1.putExtra("PackageName",context.getPackageName());
  15. intent1.putExtra("Delayed",Delayed);
  16. context.startService(intent1);
  17. /**杀死整个进程**/
  18. android.os.Process.killProcess(android.os.Process.myPid());
  19. }
  20. /***重启整个APP*/
  21. public static void restartAPP(Context context){
  22. restartAPP(context,2000);
  23. }
  24. }
  25. /***
  26. * 该服务只用来让APP重启,生命周期也仅仅是只是重启APP。重启完即自我杀死
  27. */
  28. public class killSelfService extends Service {
  29. /**关闭应用后多久重新启动*/
  30. private static long stopDelayed=2000;
  31. private Handler handler;
  32. private String PackageName;
  33. public killSelfService() {
  34. handler=new Handler();
  35. }
  36. @Override
  37. public int onStartCommand(final Intent intent, int flags, int startId) {
  38. stopDelayed=intent.getLongExtra("Delayed",2000);
  39. PackageName=intent.getStringExtra("PackageName");
  40. handler.postDelayed(()->{
  41. Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(PackageName);
  42. startActivity(LaunchIntent);
  43. killSelfService.this.stopSelf();
  44. },stopDelayed);
  45. return super.onStartCommand(intent, flags, startId);
  46. }
  47. @Override
  48. public IBinder onBind(Intent intent) {
  49. return null;
  50. }
  51. }

最后别忘了,在AndroidManifest.xml文件中去注册Service哦

  1. <service
  2. android:name=".restart_app.killSelfService"
  3. />
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post