Android往SD卡写数据范例

2023-10-08 11:58
文章标签 数据 android sd 范例 卡写

本文主要是介绍Android往SD卡写数据范例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前几天,为了监控WIFI开关情况,把有效信息保存在SD,下面是相关代码:

1、读写工具类

package com.android.server;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;import android.content.Context;
import android.os.Environment;
import android.util.Log;public class WifiLogFileUtil
{protected static final String TAG = "WifiLogFileUtil";private static final boolean DBG = true;private static final String FILE_NAME = "/WIFI/wifi.txt";private static final String FILE_PATH = Environment.getExternalStorageDirectory( ).getAbsoluteFile( ) + FILE_NAME;// 0.5Mprivate static final int FILE_SIZE = 1024*512;public static void writeData2Sdcard( Context context, boolean enable, String packageName ){FileWriter fileWriter = null;try{if ( Environment.getExternalStorageState( ).equals( Environment.MEDIA_MOUNTED ) ){File sdFile = new File( FILE_PATH );boolean ret = true;if ( sdFile.exists( ) == false ){ret = createFile( FILE_PATH );}if ( ret ){long length = sdFile.length( );if ( DBG )Log.d( TAG, "目标文件length=" + length );if ( length > FILE_SIZE ){fileWriter = new FileWriter( sdFile, false );}else{fileWriter = new FileWriter( sdFile, true );}SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );Date curDate = new Date( );String buffer = dateFormat.format( curDate );String desBuffer = String.format( "%s %s %s\n", buffer, enable, packageName );fileWriter.write( desBuffer );if ( DBG )Log.d( TAG, "写入内容:" + desBuffer );}}} catch ( Exception exception ){exception.printStackTrace( );} finally{try{if ( fileWriter != null ){fileWriter.close( );}} catch ( IOException e ){// TODO Auto-generated catch blocke.printStackTrace( );}}}public static void readDat4Sdcard( Context context ){FileReader fileReader = null;BufferedReader bufferedReader = null;try{if ( Environment.getExternalStorageState( ).equals( Environment.MEDIA_MOUNTED ) ){File sdFile = new File( FILE_PATH );if ( sdFile.exists( ) ){fileReader = new FileReader( sdFile );bufferedReader = new BufferedReader( fileReader );String buffer = bufferedReader.readLine( );StringBuffer stringBuffer = new StringBuffer( );while ( buffer != null ){stringBuffer.append( buffer );buffer = bufferedReader.readLine( );}if ( DBG )Log.d( TAG, "目标文件内容:\n" + stringBuffer );}}} catch ( Exception exception ){exception.printStackTrace( );} finally{if ( bufferedReader != null ){try{bufferedReader.close( );} catch ( IOException e ){// TODO Auto-generated catch blocke.printStackTrace( );}}if ( fileReader != null ){try{fileReader.close( );} catch ( IOException e ){// TODO Auto-generated catch blocke.printStackTrace( );}}}}public static boolean createFile( String destFileName ){File file = new File( destFileName );if ( file.exists( ) ){if ( DBG )Log.d( TAG, "创建单个文件" + destFileName + "失败,目标文件已存在!" );return false;}if ( destFileName.endsWith( File.separator ) ){if ( DBG )Log.e( TAG, "创建单个文件" + destFileName + "失败,目标文件不能为目录!" );return false;}// 判断目标文件所在的目录是否存在if ( !file.getParentFile( ).exists( ) ){// 如果目标文件所在的目录不存在,则创建父目录if ( DBG )Log.d( TAG, "目标文件所在目录不存在,准备创建它!" );if ( !file.getParentFile( ).mkdirs( ) ){if ( DBG )Log.e( TAG, "创建目标文件所在目录失败!" );return false;}}// 创建目标文件try{if ( file.createNewFile( ) ){if ( DBG )Log.d( TAG, "创建单个文件" + destFileName + "成功!" );return true;}else{if ( DBG )Log.e( TAG, "创建单个文件" + destFileName + "失败!" );return false;}} catch ( IOException e ){e.printStackTrace( );if ( DBG )Log.e( TAG, "创建单个文件" + destFileName + "失败!" + e.getMessage( ) );return false;}}
}

2、调用地方

/*** see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}* @param enable {@code true} to enable, {@code false} to disable.* @return {@code true} if the enable/disable operation was*         started or is already in the queue.*/public synchronized boolean setWifiEnabled(boolean enable) {enforceChangePermission();String packageName = getPackageName(mContext, Binder.getCallingPid());Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid()+ ", uid=" + Binder.getCallingUid()+ ", packageName=" + packageName);if (DBG) {Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");}if (enable) {reportStartWorkSource();}mWifiStateMachine.setWifiEnabled(enable);// 调用写入函数WifiLogFileUtil.writeData2Sdcard(mContext, enable, packageName);/** Caller might not have WRITE_SECURE_SETTINGS,* only CHANGE_WIFI_STATE is enforced*/long ident = Binder.clearCallingIdentity();try {handleWifiToggled(enable);} finally {Binder.restoreCallingIdentity(ident);}if (enable) {if (!mIsReceiverRegistered) {registerForBroadcasts();mIsReceiverRegistered = true;}} else if (mIsReceiverRegistered) {mContext.unregisterReceiver(mReceiver);mIsReceiverRegistered = false;}return true;}

3、效果

2015-09-17 14:36:03 true com.xtc.register
2015-09-17 16:12:58 false com.xtc.cleanmemtool
2015-09-17 16:25:48 true com.xtc.cleanmemtool
2015-09-17 17:57:38 true com.eebbk.videoclassjunior
2015-09-17 17:57:39 true com.eebbk.videoclassjunior
2015-09-17 17:58:03 true com.eebbk.videoclassjunior
2015-09-17 21:58:23 false com.xtc.cleanmemtool
2015-09-18 08:10:30 true com.xtc.cleanmemtool
2015-09-18 10:08:03 false com.xtc.cleanmemtool
2015-09-18 10:20:59 true com.xtc.cleanmemtool
2015-09-18 12:35:34 false com.xtc.cleanmemtool
2015-09-18 14:31:12 true com.xtc.cleanmemtool
2015-09-18 23:09:12 false android.process.media
2015-09-18 23:09:12 true android.process.media
2015-09-19 13:26:04 false com.xtc.cleanmemtool
2015-09-19 14:09:04 true com.xtc.cleanmemtool
2015-09-19 15:56:21 false com.xtc.cleanmemtool
2015-09-19 16:15:41 true com.xtc.cleanmemtool
2015-09-19 19:05:44 false com.xtc.cleanmemtool
2015-09-21 08:13:12 true com.xtc.cleanmemtool
2015-09-21 09:57:15 false com.xtc.cleanmemtool
2015-09-21 10:10:43 true com.xtc.cleanmemtool
2015-09-21 11:40:08 false com.xtc.cleanmemtool
2015-09-21 11:45:44 true com.xtc.cleanmemtool
2015-09-21 13:03:09 false com.xtc.cleanmemtool
2015-09-21 14:08:45 true com.xtc.cleanmemtool
2015-09-21 18:23:12 false com.xtc.cleanmemtool
2015-09-21 19:10:05 true com.xtc.cleanmemtool
2015-09-21 19:19:35 false com.xtc.systemsettings
2015-09-21 19:23:07 true com.xtc.systemsettings
2015-09-21 19:23:34 false com.xtc.systemsettings
2015-09-21 19:42:04 true com.eebbk.synchinese
2015-09-21 21:56:59 false com.xtc.cleanmemtool
2015-09-22 08:08:46 true com.xtc.cleanmemtool

这篇关于Android往SD卡写数据范例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/165096

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE