TraceView using the detailed Android eclipse
Last Update:2015-04-17
Source: Internet
Author: User
<span id="Label3"></p>Look at the command first (if the environment variable is configured, directly traceview+ the space + trace file path):<p><p></p></p><strong><strong>What is traceview?</strong></strong><p class="p2"><p class="p2">TraceView is an Android platform equipped with a good performance analysis Tool. It can be graphically used to let us know the performance of the program we want to track, and can be specific to Method.</p></p>The role of TraceView<p class="p5"><p class="p5"><span class="s1">1. Review the execution time of the tracking code, and analyze what is a lengthy operation</span></p></p><p class="p6"><p class="p6">2. Calls that can be used to track methods, especially the method call relationships of the Android framework layer</p></p><p class="p4"><p class="p4"></p></p><strong><strong>TraceView steps to use</strong></strong><p class="p3"><p class="p3">is divided into the following three steps:</p></p><p class="p3"><p class="p3">1. Select a tracking range to add a record code</p></p><p class="p3"><p class="p3">2. Open the. trace file by using the tool trace view under Tools</p></p><p class="p3"><p class="p3">3. Analyzing trace files</p></p>1. Select a tracking range to add a record code<p class="p3"><p class="p3">first, The code must be added to the program to generate the trace file that can be converted to a graph with this trace file.</p></p><p class="p1"><p class="p1">The code to add is as Follows:</p></p> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>1</code></td> <td class="content"><code class="Brush plain">Debug.startMethodTracing(“wirelessqa”); </code><code class="Brush comments">//开始</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>2</code></td> <td class="content"><code class="Brush plain">Debug.stopMethodTracing(); </code><code class="Brush comments">//结束</code></td> </tr> </tbody> </table><p><p>Where parameter WIRELESSQA is the name of the trace file to create, wirelessqa.trace. The default path is/sdcard/wirelessqa.trace, or you can make your own/data/log/wirelessqa, indicating that the file is In/data/log/wirelessqa.trace.</p></p><p><p></p></p><p class="p1"><p class="p1"><strong>Example code reference:</strong></p></p><p><p></p></p> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>01</code></td> <td class="content"><code class="Brush plain">publicclass MainActivity </code><code class="Brush keyword">extends</code><code class="Brush plain">Activity {</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>02</code></td> <td class="content"></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>03</code></td> <td class="content"><code class="spaces"> </code><code class="Brush color1">@Override</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>04</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">protectedvoid onCreate(Bundle savedInstanceState) {</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>05</code></td> <td class="content"><code class="spaces"> </code><code class="Brush keyword">super</code><code class="Brush plain">.onCreate(savedInstanceState);</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>06</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">setContentView(R.layout.activity_main);</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>07</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">setTitle(</code><code class="Brush keyword">this</code><code class="Brush plain">.getClass().getName());</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>08</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">View toLoginView = findViewById(R.id.to_login);</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>09</code></td> <td class="content"><code class="spaces"> </code><code class="Brush comments">// 开始记录 sdcard/wirelessqa.trace文件</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>10</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">Debug.startMethodTracing(</code><code class="Brush string">"wirelessqa"</code><code class="Brush plain">);</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>11</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">toLoginView.setOnClickListener(</code><code class="Brush keyword">new</code><code class="Brush plain">View.OnClickListener() {</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>12</code></td> <td class="content"></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>13</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">publicvoid onClick(View view) {</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>14</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">Intent intent = </code><code class="Brush keyword">new</code><code class="Brush plain">Intent(getApplicationContext(), LoginActivity.</code><code class="Brush keyword">class</code><code class="Brush plain">);</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>15</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">startActivity(intent);</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>16</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">}</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>17</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">});</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>18</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">}</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>19</code></td> <td class="content"><code class="spaces"> </code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>20</code></td> <td class="content"><code class="spaces"> </code><code class="Brush color1">@Override</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>21</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">protectedvoid onStop() {</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>22</code></td> <td class="content"><code class="spaces"> </code><code class="Brush keyword">super</code><code class="Brush plain">.onStop();</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>23</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">Debug.stopMethodTracing();</code><code class="Brush comments">// 结束记录wirelessqa.trace</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>24</code></td> <td class="content"><code class="spaces"> </code><code class="Brush plain">}</code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>25</code></td> <td class="content"><code class="Brush plain">}</code></td> </tr> </tbody> </table><p><p></p></p><p class="p1"><p class="p1"><strong>Description</strong></p></p><p class="p3"><p class="p3">The development documentation says that you can add debug.startmethodtracing () to the Activity's oncreate (), and add debug.stopmethodtracing () to OnDestroy (), But in the actual test it is not easy to find this way, because normally our Activity's OnDestroy () is determined by the system when it is called, so it may wait for a long time to get the trace File.</p></p><p class="p3"><p class="p3">therefore, It is decided to call Debug.stopmethodtracing () in OnStop (). So when we switch to other activity or click on the home button OnStop () will be called, we can also get the complete trace file.</p></p><p class="p2"><p class="p2"></p></p><p class="p3"><p class="p3">Don't forget to add access to the SD card</p></p> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>1</code></td> <td class="content"><code class="Brush plain"><uses-permissionandroid:name=</code><code class="Brush string">"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"</code><code class="Brush plain">/> </code></td> </tr> </tbody> </table> <table class="table table-condensed table-striped table-bordered "> <tbody> <tr> <td class="number"><code>2</code></td> <td class="content"><code class="Brush plain"><uses-permissionandroid:name=</code><code class="Brush string">"android.permission.WRITE_EXTERNAL_STORAGE"</code><code class="Brush plain">/> </code></td> </tr> </tbody> </table><p><p></p></p>2. Open the. trace file by using the tool trace view under Tools<p><p></p></p><p><p></p></p><p><p></p></p>3. Analyzing trace files<p><p></p></p><p><p>TraceView using the detailed Android eclipse</p></p></span>