Log your action : Log « Core Class « Android






Log your action

    

package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class Test extends Activity implements OnTouchListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
        layout1.setOnTouchListener(this);
    }

  public boolean onTouch(View v, MotionEvent event) {
    String myTag = v.getTag().toString();
    Log.v(myTag, describeEvent(event));
    logAction(event);
    if( "true".equals(myTag.substring(0, 4))) {
      return true;
    }
    else {
      return false;
    }
  }

  protected static String describeEvent(MotionEvent event) {
    StringBuilder result = new StringBuilder(500);
    result.append("Action: ").append(event.getAction()).append("\n");
    int numPointers = event.getPointerCount();
    result.append("Number of pointers: ").append(numPointers).append("\n");
        int ptrIdx = 0;
    while (ptrIdx < numPointers) {
        int ptrId = event.getPointerId(ptrIdx);
            result.append("Pointer Index: ").append(ptrIdx);
            result.append(", Pointer Id: ").append(ptrId).append("\n");
            result.append("   Location: ").append(event.getX(ptrIdx));
            result.append(" x ").append(event.getY(ptrIdx)).append("\n");
            result.append("   Pressure: ").append(event.getPressure(ptrIdx));
            result.append("   Size: ").append(event.getSize(ptrIdx)).append("\n");

            ptrIdx++;
        }
        result.append("Downtime: ").append(event.getDownTime()).append("ms\n");
        result.append("Event time: ").append(event.getEventTime()).append("ms");
        result.append("  Elapsed: ").append(event.getEventTime()-event.getDownTime());
        result.append(" ms\n");
        return result.toString();
    }

  private void logAction(MotionEvent event) {
      final String TAG = "Action";
    int ptrIndex = event.getActionIndex();
    int ptrId = event.getPointerId(ptrIndex);
    int action = event.getActionMasked();

    Log.v(TAG, "Pointer index: " + ptrIndex);
    Log.v(TAG, "Pointer Id: " + ptrId);
    Log.v(TAG, "True action value: " + action);
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"  android:tag="trueLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"  android:layout_height="wrap_content"
    android:layout_weight="1" >

    <TextView android:text="Touch fingers on the screen and look at LogCat"
    android:id="@+id/message"  android:tag="trueText"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

   
    
    
    
  








Related examples in the same category

1.Use log
2.Log Utility
3.Log events
4.Write an activity that looks like a pop-up dialog with a custom theme using a different text color.
5.Responsible for delegating calls to the Android logging system.
6.Dynamically defined), space efficient event logging to help instrument code for large scale stability and performance monitoring.
7.Write Exception Stack to Log
8.Logger and Logger Listener
9.Log Exception trace
10.Log a list of objects
11.Utility log tool
12.Debug Util