我想制作一个每天都会做某事的应用程序。我设法保存了一天,并在接下来的几天里,我希望与当天相比。写入并读取到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