VMDaemonManager.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.vmloft.develop.daemon;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.util.Log;
  5. import com.vmloft.develop.daemon.services.VMCoreService;
  6. /**
  7. * 守护进程管理类
  8. * Created by lzan13 on 2017/3/9.
  9. */
  10. public class VMDaemonManager {
  11. private final static String TAG = VMDaemonManager.class.getSimpleName();
  12. private static VMDaemonManager instance = null;
  13. private AppCompatActivity daemonActivity;
  14. private VMDaemonManager() {
  15. }
  16. /**
  17. * 获取单例类实例
  18. */
  19. public static VMDaemonManager getInstance() {
  20. if (instance == null) {
  21. instance = new VMDaemonManager();
  22. }
  23. return instance;
  24. }
  25. /**
  26. * 启动守护 Activity,其实就是一像素大小的流氓 activity
  27. */
  28. public void startDaemonActivity() {
  29. Log.i(TAG, "startCoreProcess: 启动流氓 Activity");
  30. VMApplication.getContext()
  31. .startActivity(new Intent(VMApplication.getContext(), VMDaemonActivity.class));
  32. }
  33. /**
  34. * 结束流氓的 activity
  35. */
  36. public void finishDaemonActivity() {
  37. Log.i(TAG, "startCoreProcess: 结束流氓 Activity");
  38. if (daemonActivity != null) {
  39. daemonActivity.finish();
  40. }
  41. }
  42. /**
  43. * 启动核心进程
  44. */
  45. public void startCoreProcess() {
  46. Log.i(TAG, "startCoreProcess: 启动核心进程");
  47. Intent wakeIntent = new Intent(VMApplication.getContext(), VMCoreService.class);
  48. VMApplication.getContext().startService(wakeIntent);
  49. }
  50. /**
  51. * 保存当前启动的一像素 Activity
  52. */
  53. public void setDaemonActivity(AppCompatActivity daemonActivity) {
  54. this.daemonActivity = daemonActivity;
  55. }
  56. }