主题 : 【代码】android开发:调试实例 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
贡献值: 0 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-07-22 17:33

 【代码】android开发:调试实例

复制代码
  1. package AndroidApi;
  2. import android.util.Log;
  3. class Monitoring implements Runnable
  4. {
  5. public void run()
  6. {
  7. while (!Thread.currentThread().isInterrupted())
  8. {
  9. try
  10. {
  11. Thread.sleep(100);
  12. } catch (InterruptedException s)
  13. {
  14. Thread.currentThread().interrupt();
  15. }
  16. AndroidDebug.printVaryMemory();
  17. }
  18. }
  19. }
  20. public class AndroidDebug
  21. {
  22. private static boolean m_bIsDebug = false;
  23. public static void setMode(boolean bIsDebug)
  24. {
  25. m_bIsDebug = bIsDebug;
  26. }
  27. public static boolean isDebug()
  28. {
  29. return m_bIsDebug;
  30. }
  31. public static void println(String strTxt)
  32. {
  33. if (m_bIsDebug)
  34. {
  35. System.out.println(strTxt);
  36. }
  37. }
  38. public static void Log(String strTag, String strTxt)
  39. {
  40. if (m_bIsDebug)
  41. {
  42. Log.i(strTag,strTxt);
  43. }
  44. }
  45. public static void gc()
  46. {
  47. if (m_bIsDebug)
  48. {
  49. System.gc();
  50. }
  51. }
  52. public static void printTotalMemory()
  53. {
  54. Runtime r = Runtime.getRuntime();
  55. AndroidDebug.println("Total memory is :" + r.totalMemory());
  56. }
  57. public static void printFreeMemory()
  58. {
  59. gc(); // 执行强制回收以获得准确的剩余量
  60. Runtime r = Runtime.getRuntime();
  61. AndroidDebug.println("Free memory is :" + r.freeMemory());
  62. }
  63. static long longPre = 0;
  64. public static void printVaryMemory()
  65. {
  66. gc(); // 执行强制回收以获得准确的剩余量
  67. Runtime r = Runtime.getRuntime();
  68. long longNow = r.freeMemory();
  69. if (longNow > longPre)
  70. {
  71. AndroidDebug.println("Free memory -> :" + (longNow - longPre));
  72. longPre = longNow;
  73. } else if (longNow < longPre)
  74. {
  75. AndroidDebug.println("Free memory <- :" + (longPre - longNow));
  76. longPre = longNow;
  77. }
  78. }
  79. private static Thread m_pThread = null;
  80. public static void setMonitore(boolean bIsOpen)
  81. {
  82. if (bIsOpen)
  83. {
  84. if (null == m_pThread)
  85. m_pThread = new Thread(new Monitoring());
  86. m_pThread.setDaemon(true);
  87. m_pThread.start();
  88. }
  89. else
  90. {
  91. if (null != m_pThread)
  92. {
  93. m_pThread.interrupt();
  94. m_pThread = null;
  95. }
  96. }
  97. }
欢迎加入android开发交流群,群号是:314230976