Android Tutorials - Herong's Tutorial Examples - v3.05, by Herong Yang
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\ActivityLog.java, to write a short message to a log file:
/* ActivityLog.java * Version 1.0 - Creating the log file * Copyright (c) 2015, HerongYang.com, All Rights Reserved. */ package com.herongyang.activity; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.content.Context; import java.io.*; public class ActivityLog extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String msg = ""; msg += "Activity created\n"; msg += "----------------\n"; try { FileOutputStream fos = openFileOutput("Activity.log", Context.MODE_APPEND); OutputStreamWriter out = new OutputStreamWriter(fos); out.write(msg); out.close(); fos.close(); } catch (Exception e) { e.printStackTrace(System.err); } TextView tv = new TextView(this); tv.setText(msg); setContentView(tv); } }
Note that:
See the next tutorial on how to test this ActivityLog application.
Table of Contents
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
AboutAndroid - Application to Retrieve System Information
►android.app.Activity 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
Samsung Galaxy Tab 3 Mini Tablet
USB Debugging Applications on Samsung Tablet
USB Debugging Applications on LG-V905R Tablet