Search This Blog

Showing posts with label LogCat. Show all posts
Showing posts with label LogCat. Show all posts

Saturday, August 27, 2011

How to add LogCat ?

Logcat is the command to view the internal logs of the Android system. Viewing logs is often the best way to diagnose a problem, and is required for many issues. This way you'll find out what apps are doing in the background without you noticing.

Advantages of Logcat

  • Debugging
  • You can see what processes are running, if a process is running after a certain interval of time, it will eat battery. So you can also find out what is draining your battery.

In Eclipse, Goto Window-> Show View -> Other -> Android-> Logcat.


How do I install it?
  1. Make sure you've enabled on USB debugging on your phone in Settings -> Application -> Development
  2. 2. Get the Android SDK here: Android SDK | Android Developers
  3. Extract the SDK to any folder say C:\Android
  4. Make sure that you have installed the drivers for your phone and the operating system recognizes your phone.
Everything installed? Continuing...
Now how to actually access the log, everything in italic are commands you need to enter.

  1. Open the 'run' dialog by pressing the 'Windows' + 'r' buttons on your keyboard (minimize the browser & other non-windows programs)
  2. cmd (this will open a DOS prompt)
  3. cd c:\Android\tools (go to the directory where you extracted the SDK)
  4. adb shell
  5. Now you'll see just a '$'
  6. logcat

You can now just press the power button on your phone to see what happens. It displays everything the device is doing.

So the thing is: leave your device connected for a while and see what is actually going on when you're not using it.

Create application to display log of various lifecycle events of Android Activity. Please make at least three activities

                main.xml                              second.xml                                    setting.xml



main.xml (code)



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView
            android:id="@+id/uname"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="@string/Uname">
      </TextView>
      <EditText
            android:id="@+id/utextbox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
      </EditText>
    </LinearLayout>
    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView
            android:id="@+id/pass"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="@string/Pass">
      </TextView>
      <EditText
            android:id="@+id/ptextbox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true">
      </EditText>
    </LinearLayout>
    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center">
      <Button
            android:id="@+id/login"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="@string/Login">
      </Button>
      <Button
            android:id="@+id/clear"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="@string/Clear">
      </Button>
    </LinearLayout>
   
</LinearLayout>


second.xml (code)



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/mainLayout"
  android:gravity="center">
      <TextView
             android:id="@+id/welcomeNote"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Welcome !!"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:gravity="center">
</TextView>
      <Button
            android:id="@+id/setting"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="@string/setting"
            android:textSize="20dp"
            android:gravity="center">
</Button>
     
</LinearLayout>

setting.xml (code)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center">
    <TextView
      android:layout_width="wrap_content"
      android:text="@string/msg"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:id="@+id/cs"
      android:textSize="30dp"
      android:gravity="center"
      android:textColor="@color/yellow">
    </TextView>    
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string name="hello">Hello World, Ex1Activity!</string>
    <string name="app_name">Ex1</string>
    <string name="Uname">User Name : </string>
    <string name="Pass">Password : </string>
    <string name="Login">Login</string>
    <string name="Clear">Clear</string>
    <string name="WelcomeNote">Welcome </string>
    <string name="setting">Setting</string>
    <string name="msg">Coming Soon...</string>
</resources>




color.xml (How to add new xml file ?)


<?xml version="1.0" encoding="UTF-8"?>
<resources>
        <color name="yellow">#FFFF00</color>      
</resources>

Ex1Activity.java



package com.exc.first;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Ex1Activity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      //read component from xml file
        Button login = (Button)findViewById(R.id.login);
       
      /**
       * set OnClickListener on button
       * so that code which is in between { } is run on click on login button
       */
        login.setOnClickListener(new OnClickListener() {
                 
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                       
                        /**
                         * make Intent to call other activity
                         * here Ex1Activity.this to go second.class
                         * on click login button
                         */
                        Intent SecAct = new Intent(Ex1Activity.this,second.class);
                       
                        //start second activity
                        startActivity(SecAct);
                       
                  }
            });
       
       
    }
    @Override
      protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            /**
             * it show massage on LogCat when Activity starts
             */
            Log.i("Login Activity", "Start");
      }
     
      @Override
      protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            /**
             * It show massage on LogCat when Activity OnPause
             */
            Log.i("Login Activity", "Pause");
      }
      @Override
      protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
            /**
             * it show massage on LogCat when Activity onRestart
             */
            Log.i("Login Activity", "Restart");
      }
      @Override
      protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            /**
             * it show massage on LogCat when Activity OnResume
             */
            Log.i("Login Activity", "Resume");
      }
     
      @Override
      protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            /**
             * it show massage on LogCat when Activity Stop
             */
            Log.i("Login Activity", "Stop");
      }
      @Override
      protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            /**
             * it show massage on LogCat when Activity Destroys
             */
            Log.i("Login Activity", "Destroy");
      }
}


second.java (How to add new java file ? )



package com.exc.first;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class second extends Activity{

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
           
            //read button from xml file
            Button setting =(Button)findViewById(R.id.setting);
            setting.setOnClickListener(new OnClickListener() {
                 
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                       
                        Intent goToSetting = new Intent(second.this,setting.class); //go to third Activity
                        startActivity(goToSetting);                    
                  }
            });
           
      }
      @Override
      protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            /**
             * it show massage on LogCat when Activity starts
             */
            Log.i("Second Activity", "Start");
      }
     
      @Override
      protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            /**
             * it show massage on LogCat when Activity on pause
             */
            Log.i("Second Activity", "Pause");
      }
      @Override
      protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
            /**
             * it show massage on LogCat when Activity Restart
             */
            Log.i("Second Activity", "Restart");
      }
      @Override
      protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            /**
             * it show massage on LogCat when Activity Resume
             */
            Log.i("Second Activity", "Resume");
      }
     
      @Override
      protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            /**
             * it show massage on LogCat when Activity  stop
             */
            Log.i("Second Activity", "Stop");
      }
      @Override
      protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            /**
             * it show massage on LogCat when Activity Destroy
             */
            Log.i("Second Activity", "Destroy");
      }

}


setting.java



package com.exc.first;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class setting extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.setting);
           
           
      }
      @Override
      protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            /**
             * it show massage on LogCat when Activity starts
             */
            Log.i("Third Activity", "Start");
      }
     
      @Override
      protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            /**
             * it show massage on LogCat when Activity Pause
             */
            Log.i("Third Activity", "Pause");
      }
      @Override
      protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
            /**
             * it show massage on LogCat when Activity Restarts
             */
            Log.i("Third Activity", "Restart");
      }
      @Override
      protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            /**
             * it show massage on LogCat when Activity Resume
             */
            Log.i("Third Activity", "Resume");
      }
     
      @Override
      protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            /**
             * it show massage on LogCat when Activity stop
             */
            Log.i("Third Activity", "Stop");
      }
      @Override
      protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            /**
             * it show massage on LogCat when Activity Destroy
             */
            Log.i("Third Activity", "Destroy");
      }


}

and don't forget to enter entry of your new java file into manifest file

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.exc.first"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Ex1Activity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".second"
                  android:label="@string/app_name">
           
        </activity>
        <activity android:name=".setting"
                  android:label="@string/app_name">
           
        </activity>

    </application>
</manifest>


Output




  • How to check LogCat ?