我在Android上有一个应用程序,它在系统启动时启动。有时互联网连接处于搜索模式,我已经设置了一个计时器来检查连接,然后连接,如果找到。我也为sd卡做了同样的事情,因为它也处于准备模式。当系统启动并且应用程序启动它从未从SD卡读取文本时,我在从SD卡读取文本文件时遇到问题。当我稍后手动启动应用程序时,它可以工作。这是我读取SD卡文件的代码。
if (isSDCardAvailable())
{
setTickerText();
}
else if(!isSDCardAvailable())
{
//pop up message
Toast toast=Toast.makeText(this, "Preparing SD card..", Toast.LENGTH_LONG);
toast.show();
//Run the sd card read process after 30 seconds
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run()
{
setTickerText();
}
}, 30000);
}
public void setTickerText()
{
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"TickerText.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
//text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
}
答案 0 :(得分:0)
完成编译系统启动后,您的应用程序将开始读取文件。
所以在此之前你可以这样检查:
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
Log.v(TAG, "storage state is " + state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
Log.v(TAG, "storage writable is " + writable);
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}