Log events : Log « Core Class « Android






Log events

    
package app.test;

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class Test extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    logEvent("CREATE");
  }

  @Override
  public void onStart() {
    super.onStart();
    logEvent("START");
  }

  @Override
  public void onResume() {
    super.onResume();
    logEvent("RESUME");
  }

  @Override
  public void onPause() {
    super.onPause();
    logWarning("PAUSE");
  }

  @Override
  public void onStop() {
    super.onStop();
    logWarning("STOP");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    logWarning("DESTROY");
  }

  private void logEvent(String event) {
    Intent intent = new Intent(this, OperationsManager.class);
    intent.setAction(OperationsManager.ACTION_EVENT);
    intent.putExtra(OperationsManager.EXTRA_NAME, event);

    startService(intent);
  }

  private void logWarning(String event) {
    Intent intent = new Intent(this, OperationsManager.class);
    intent.setAction(OperationsManager.ACTION_WARNING);
    intent.putExtra(OperationsManager.EXTRA_NAME, event);

    startService(intent);
  }
}

class OperationsManager extends IntentService {

  public static final String ACTION_EVENT = "ACTION_EVENT";
  public static final String ACTION_WARNING = "ACTION_WARNING";
  public static final String ACTION_ERROR = "ACTION_ERROR";
  public static final String EXTRA_NAME = "eventName";

  private static final String LOGTAG = "EventLogger";

  private IntentFilter matcher;

  public OperationsManager() {
    super("OperationsManager");
    matcher = new IntentFilter();
    matcher.addAction(ACTION_EVENT);
    matcher.addAction(ACTION_WARNING);
    matcher.addAction(ACTION_ERROR);
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    if (!matcher.matchAction(intent.getAction())) {
      Toast.makeText(this, "OperationsManager: Invalid Request",
          Toast.LENGTH_SHORT).show();
      return;
    }
    if (TextUtils.equals(intent.getAction(), ACTION_EVENT)) {
      logEvent(intent.getStringExtra(EXTRA_NAME));
    }
  }

  private void logEvent(String name) {
    try {
      Thread.sleep(5000);
      Log.i(LOGTAG, name);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Use log
2.Log Utility
3.Log your action
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