Im new to coding and Im trying to learn it on my own but I have run into the problem in the app I am developing of being unable to use openFileInput in a broadcast reciever. Here is the code:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
//end of read froms
DateFormat format = DateFormat.getDateInstance();
TextView text;
Button btn;
DateFormat formate = DateFormat.getTimeInstance();
Calendar calender2 = Calendar.getInstance();
TextView txt2;
Button btn2;
//read froms start
private String readFromFilename() {
String ret = "";
try {
InputStream inputStream = openFileInput("config.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
If anyone can help i would appreciate any tips or work arounds. The exact error it gives is that it cannot resolve the method. When I tried to research it on my own they mentioned something about context but I couldnt find an example of it in code.
openFileInput()is a method onContext. You will notice that the second parameter toonReceive()is aContext. Hence, you can callopenFileInput()on thatContextobject.That being said, please note that
onReceive()is called on the main application thread, and doing disk I/O on the main application thread is not a good idea.