android 简单实现写入日志信息到日志文件功能
1)AppLogHelper.java定义
public class AppLogHelper { private static final String LOG_DIR = "appLog"; // 日志文件夹名 private static final String LOG_NAME = "log.txt"; // 日志文件名 private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static File logDir = null; private static File logFile = null; public static void init(Context context) { // 获取外部存储的日志目录 File externalStorageDir = context.getExternalFilesDir(null); /* logDir = new File(externalStorageDir, LOG_DIR); logFile = new File(logDir, LOG_NAME);*/ //可以使用上面两句代码,我这边要特殊文件名称,如:log_20250116(动态每天的) String newLogName = getFileName("log_"); logDir = new File(externalStorageDir, LOG_DIR); logFile = new File(logDir, newLogName); if (!logDir.exists()) { logDir.mkdirs(); } if (!logFile.exists()) { try { logFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } public static void log(String tag, String msg) { if (logDir != null && logFile != null) { try { FileWriter fileWriter = new FileWriter(logFile, true); fileWriter.write(dateFormat.format(new Date()) + " - " + tag + ": " + msg + "\n"); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } /*** * 重新设置Log.txt文件名(由log.txt改成log_20250116.txt) * */ public static String getFileName(String prefix){ SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd"); String newLogName = prefix + dateFormat2.format(new Date())+".txt"; return newLogName; } }
2)在AppApplication.java文件初始化日志组件
public class AppApplication extends Application { public static Context context; @Override public void onCreate() { super.onCreate(); this.context = getApplicationContext(); //初始化日志文件 AppLogHelper.init(context); } }
3)AndroidManifest.xml 文件增加AppApplication
<application android:name=".app.AppApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
4)activity_main.xml 布局页面简单加两个按钮(开始、结束)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <LinearLayout android:id="@+id/lay_out_pack" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_alarm_start" android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginLeft="5dp" android:layout_marginTop="0dp" android:layout_marginRight="5dp" android:layout_marginBottom="10dp" android:layout_weight="1" android:text="开启闹钟" android:textSize="30sp" android:textStyle="bold" /> <Button android:id="@+id/btn_alarm_cancel" android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginLeft="5dp" android:layout_marginTop="0dp" android:layout_marginRight="5dp" android:layout_marginBottom="10dp" android:layout_weight="1" android:text="关闭闹钟" android:textSize="30sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout>
5)MainActivity.java 增加开始、结束点击事件
public class MainActivity extends AppCompatActivity { private final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFindView(); } private void initFindView(){ Button btnAlarmStart = findViewById(R.id.btn_alarm_start); Button btnAlarmCancel = findViewById(R.id.btn_alarm_cancel); //btnAlarmStart添加点击事件 btnAlarmStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AppLogHelper.log(TAG,"-----闹钟开启-----"); } }); //btnAlarmCancel添加点击事件 btnAlarmCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AppLogHelper.log(TAG,"-----闹钟关闭-----"); } }); } }
6、运行项目,并点击按钮的开始、结束,效果图如下
标签:String,写入,public,static,new,日志,Android,log From: https://www.cnblogs.com/xielong/p/18674723