gpt4 book ai didi

android - 在 SendSms 和接收 SMS 上创建日志文件

转载 作者:行者123 更新时间:2023-11-29 13:58:35 25 4
gpt4 key购买 nike

我正在开发一个 Android 版本 2.3.3 应用程序,它依赖于 SMS 进行 SMS 处理。我写了两个类 SendSMS 和 ReceiveSMS。我想创建一个日志文件来记录发送或接收的每条短信。为了创建日志文件,我编写了代码但不起作用。

以下是我的代码,

public void WriteOnLog()
{
File exportDir = Environment.getExternalStorageDirectory();

if (!exportDir.exists()) {
exportDir.mkdirs();
}
String fileName;

fileName = "log" + ".txt";

File file = new File(exportDir,fileName);
String txt=null ;
try {
if(!file.exists()){
file.createNewFile();
FileWriter Write = new FileWriter(file,true);
out = new BufferedWriter(Write);
txt = "Date Time |" + " Send/Receive |" + " Controller No |" +" msg";
}
FileWriter Write = new FileWriter(file,true);
out = new BufferedWriter(Write);

String dd=null,mm=null,yy=null,hh=null,min=null,ss=null,dt=null;
SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String newtime = sdfDateTime.format(new Date(System.currentTimeMillis()));
yy = newtime.substring(2, 4);
mm = newtime.substring(5, 7);
dd = newtime.substring(8, 10);
hh = newtime.substring(11, 13);
min = newtime.substring(14, 16);
ss = newtime.substring(17);
dt = dd+"-"+mm+"-"+yy +" " + hh + ":" + min +":"+ ss;
txt = dt + " |" +" Send " + "| " + phoneNumber + " | " + message.toUpperCase();
out.write(txt + "\n");


}
catch(IOException sqlEx) {
Log.e("MainActivity", sqlEx.getMessage(), sqlEx);

}
}

最佳答案

我使用了 OutputStreamWriter(而不是 FileWriter)方法。我让它在我的开发环境中工作并在设备(android 2.3.6)上进行了测试。试试这个:

try {
File directory;
if(isSDCard){
directory = new File(Environment.getExternalStorageDirectory().toString()+"/"+folderNameOrPath+"/");
}
else{
directory = new File(folderNameOrPath);
}

boolean fileReturnVal = directory.mkdirs();
if(fileReturnVal){
Log.d("Storage", "Write in SD Card: " + folderNameOrPath + " folder Created successfully");
}
else{
Log.d("Storage", "Write in SD Card: " + folderNameOrPath + " folder either already exists or creation failed");
}

File file = new File(directory, fileNameWithExtention);
FileOutputStream fOut = new FileOutputStream(file);

OutputStreamWriter osw = new OutputStreamWriter(fOut);

//Write the string to the file
osw.write(text);
osw.flush();
osw.close();

//file saved
Toast.makeText(context, "Text saved in SD Card", Toast.LENGTH_SHORT).show();
return true;

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.e("Storage", "Write in SD Card: File Not Found ERROR: " + e.toString());
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Storage", "Write in SD Card: IOException ERROR: " + e.toString());
return false;
} catch (Exception e) {
Log.e("Storage", "Write in SD Card: ERROR: " + e.toString());
return false;
}

您可以为您的任务设置追加模式。变量是SDCard、folderName、fileNameWithExtn。等是方法级变量。我将这些作为参数传递。

关于android - 在 SendSms 和接收 SMS 上创建日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10793538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com