今天要來說一下android的log檔

我們讓使用者輸入資料後,有時希望可以記錄在手機端

若使用者程式有任何問題,方便我們日後查詢log來了解

以下程式碼 MainActivity.java

 

public class MainActivity extends AppCompatActivity {
private EditText acc;
private Button login;
private Write write = new Write(MainActivity.this);//先宣告write.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   login = (Button)findViewById(R.id.login);
 acc=(EditText)findViewById(R.id.acc);
 login.setOnClickListener(getDBRecord);
}

private Button.OnClickListener getDBRecord = new Button.OnClickListener() {
    public void onClick(View v) {
     String car_num=acc.getText().toString().toUpperCase();
           write.WriteFileExample(car_num);//存入資料到log
    }
};
}

 

以下是write.java

public Write(Context context) {
    this.context = context;
}

public static void WriteFileExample(String message) {
    FileOutputStream fop = null;
    File file;
    String content = message;
    try {
        File sdcard = Environment.getExternalStorageDirectory();
        file = new File(sdcard, "myLog.log");
        // 建立檔案
        if (!file.exists())
            file.createNewFile();
        
       fop =new  FileOutputStream(file);
        byte[] contentInBytes = content.getBytes();// 取內容bytes
        fop.write(contentInBytes); //輸出
  } catch (IOException e) {} 
    finally {
        try {
            if (fop != null) 
                fop.close();
            } catch (IOException e) {}
    }
}
}

以上,就可以記錄檔案了~~

 

arrow
arrow
    文章標籤
    android log outstream
    全站熱搜

    kyo 發表在 痞客邦 留言(0) 人氣()