在SendSms上创建日志文件并接收SMS

时间:2012-05-29 05:33:11

标签: android sms

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

以下是我的代码,

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);              

}
}

1 个答案:

答案 0 :(得分:0)

我使用了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;
    }

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