Hier ist der Code:
Code:
public class MainActivity extends Activity {
Button btnClose;
CheckBox chk;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String currentDateandTime = sdf.format(new Date());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk = (CheckBox) findViewById(R.id.checkBox1);
chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
while (chk.isChecked()){
try {
File log = new File("/sdcard/log.txt");
if(!log.exists()){
Toast.makeText(getApplicationContext(), "We had to make a new file.", Toast.LENGTH_SHORT).show();
log.createNewFile();
}
FileWriter fileWriter = new FileWriter(log, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("date: " + currentDateandTime + "\n");
bufferedWriter.close();
Toast.makeText(getBaseContext(), "Done writing to 'log.txt'", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
try {
Thread.sleep(10000); }
catch (InterruptedException e) { }
}
}
});
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}







