• 爱情文章
  • 亲情文章
  • 友情文章
  • 生活随笔
  • 校园文章
  • 经典文章
  • 人生哲理
  • 励志文章
  • 搞笑文章
  • 心情日记
  • 英语文章
  • 范文大全
  • 作文大全
  • 新闻阅读
  • 当前位置: 山茶花美文网 > 新闻阅读 > 正文

    【安卓开发之调试实例】安卓移动应用开发实例

    时间:2020-05-28来源:山茶花美文网 本文已影响 山茶花美文网手机站

    package AndroidApi;

    import android.util.Log;

    class Monitoring implements Runnable

    {

    public void run()

    {

    while (!Thread.currentThread().isInterrupted())

    {

    try

    {

    Thread.sleep(100);

    } catch (InterruptedException s)

    {

    Thread.currentThread().interrupt();

    }

    AndroidDebug.printVaryMemory();

    }

    }

    }

    public class AndroidDebug

    {

    private static boolean m_bIsDebug = false;

    /**

    * 设置调试模式

    *

    * @param bIsDebug

    */

    public static void setMode(boolean bIsDebug)

    {

    m_bIsDebug = bIsDebug;

    }

    /**

    * 是否调试模式

    * @return

    */

    public static boolean isDebug()

    {

    return m_bIsDebug;

    }

    /**

    * 打印信息

    *

    * @param strTxt

    */

    public static void println(String strTxt)

    {

    if (m_bIsDebug)

    {

    System.out.println(strTxt);

    }

    }

    /**

    * 打印信息

    *

    * @param strTxt

    */

    public static void Log(String strTag, String strTxt)

    {

    if (m_bIsDebug)

    {

    Log.i(strTag,strTxt);

    }

    }

    /**

    * 强制回收垃圾,可用于检测析构函数,检测未使用对象是否有

    */

    public static void gc()

    {

    if (m_bIsDebug)

    {

    System.gc();

    }

    }

    /**

    * 打印堆总量

    */

    public static void printTotalMemory()

    {

    Runtime r = Runtime.getRuntime();

    AndroidDebug.println("Total memory is :" + r.totalMemory());

    }

    /**

    * 打印堆剩余量

    */

    public static void printFreeMemory()

    {

    gc(); // 执行强制回收以获得准确的剩余量

    Runtime r = Runtime.getRuntime();

    AndroidDebug.println("Free memory is :" + r.freeMemory());

    }

    /**

    * 打印堆变化量

    */

    static long longPre = 0;

    public static void printVaryMemory()

    {

    gc(); // 执行强制回收以获得准确的剩余量

    Runtime r = Runtime.getRuntime();

    long longNow = r.freeMemory();

    if (longNow > longPre)

    {

    AndroidDebug.println("Free memory -> :" + (longNow - longPre));

    longPre = longNow;

    } else if (longNow < longPre)

    {

    AndroidDebug.println("Free memory <- :" + (longPre - longNow));

    longPre = longNow;

    }

    }

    /**

    * 监控内存

    *

    * @param bIsOpen

    */

    private static Thread m_pThread = null;

    public static void setMonitore(boolean bIsOpen)

    {

    if (bIsOpen)

    {

    if (null == m_pThread)

    m_pThread = new Thread(new Monitoring());

    m_pThread.setDaemon(true);

    m_pThread.start();

    }

    else

    {

    if (null != m_pThread)

    {

    m_pThread.interrupt();

    m_pThread = null;

    }

    }

    }

    • 【安卓开发之调试实例】安卓移动应用开发实例 相关文章:
    • 爱情文章
    • 亲情文章
    • 友情文章
    • 随笔
    • 哲理
    • 励志
    • 范文大全