ActivityLog - Application to Create Log File

This section provides a tutorial example on how to create application, ActivityLog, to create a log file to monitor activity lifecycle using the android.content.Context.openFileOutput() method.

The best way to monitor the lifecycle of an activity is to write messages into a log file. For that, we need to learn how to open a file and write text messages to the file on an Android system.

According to the Android reference document, the android.content.Context class provides you methods to interface with the data folder assigned to your application in the file system.

Now I am ready to create a new application, ActivityLog, with a new package, com.herongyang.activity:

C:\>cd \herong
C:\herong>set JAVA_HOME=\Program Files\Java\jdk1.8.0_45\

C:\herong>\local\android-sdk-windows\tools\android create project \
   --package com.herongyang.activity --activity ActivityLog --target 2 \
   --path .\ActivityLog
Created project directory: C:\herong\ActivityLog
Added file C:\herong\ActivityLog\build.xml

C:\herong>cd ActivityLog
C:\herong\ActivityLog>del .\res\layout\main.xml

Modify the Activity Java class, .\src\com\herongyang\activity\, to write a short message to a log file:

 * Version 1.0 - Creating the log file
 * Copyright (c) 2015,, All Rights Reserved.
package com.herongyang.activity;

import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
public class ActivityLog extends Activity {
   public void onCreate(Bundle savedInstanceState) {
      String msg = "";

      msg += "Activity created\n";
      msg += "----------------\n";

      try {
         FileOutputStream fos =
            openFileOutput("Activity.log", Context.MODE_APPEND);
         OutputStreamWriter out = new OutputStreamWriter(fos);
      } catch (Exception e) {

      TextView tv = new TextView(this);

Note that:

See the next tutorial on how to test this ActivityLog application.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

 Installing Apache Ant 1.9 on Windows System

 Developing First Android Application - HelloAndroid

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 AboutAndroid - Application to Retrieve System Information Class and Activity Lifecycle

 Introduction of Activity Lifecycle

 onCreate() and Other Callback Methods

ActivityLog - Application to Create Log File

 Viewing Activity Log File with "cat" Command in "adb shell"

 Implementing Activity Callback Methods

 ActivityLog Test - Activity Terminated by User

 ActivityLog Test - Activity Stopped and Restarted

 ActivityLog Test - Activity Paused and Resumed

 View Objects and Layout Resource Files

 Using "adb logcat" Command for Debugging

 Build Process and Package File Content

 Building Your Own Web Browser

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Archived Tutorials


 Full Version in PDF/EPUB