我想製作一個每天都會做某事的應用程序。我設法保存了一天,並在接下來的幾天裏,我希望與當天相比。寫入並讀取到SDcard
例如: 天= 5; aux = 5;
明天:
天= 6; aux = 5;
如果(天!= AUX)做一些別的 不採取行動
我想保存上的SD卡文件中的輔助的狀態,但它是很難找到工作的代碼。我希望有人會看看並回答它,明天我會需要它。
public class Castle extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.castle);
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
int aux=Reading();
if(day==aux)
{
Intent intent = new Intent(Castle.this, Hug.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(Castle.this, Hug_Accepted.class);
startActivity(intent);
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File file = new File(root, "Tedehlia/state.txt");
file.mkdir();
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
out.write(day);
out.close();
}
} catch (IOException e) {
}}
}
public int Reading()
{int aux = 0;
try{
File f = new File(Environment.getExternalStorageDirectory()+"/state.txt");
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
if((readString = buf.readLine())!= null){
aux=Integer.parseInt(readString.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return aux;
}
}
但問題是什麼?你有異常嗎? (在你的catch子句中打印出LogCat的例外) – YuviDroid 2012-03-04 20:19:29
我現在將測試它。我100%肯定它不會工作。我甚至不確定文件是否會被創建。 – AnTz 2012-03-04 20:23:32
我也想只保存一個值在文件上。我想我可能需要刪除文件後,我從中獲得的價值,有人可以告訴我如何做到這一點? – AnTz 2012-03-04 20:24:20