-2

I have a broadcast receiver class and I want to use my method "ReadInternalData" to read the internal data in my app, but I can't manage to solve the problem of Cannot resolve method 'openFileInput(java.lang.String)' on openFileInput!

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsBroadcastReceiver extends BroadcastReceiver{
  public void onReceive(Context context, Intent intent){
    //Some code here
  }
  //Some code here

  public String ReadInternalData(String str){
    String temp="";
    try{
        FileInputStream fin = openFileInput(str);
        int c;
        while( (c = fin.read()) != -1)
        {
            temp = temp + Character.toString((char)c);
        }
    } catch(Exception e)
    {
        e.printStackTrace();
    }
    return temp;
}
}

I tried to use context:

FileInputStream fin = SmsBroadcastReceiver.openFileInput(str); 

But it doesn't work!

Edit: OK Sorry I though "SmsBroadcastReceiver" is a context, however, my class is not service neither Activity and I want to read the internal data in my app? I do not see any reason to give minus mark to my question!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Majid ff
  • 249
  • 7
  • 22
  • 1
    Possible duplicate of [Android: Reading from file (Openfileinput)](https://stackoverflow.com/questions/6030744/android-reading-from-file-openfileinput) – Mitch Feb 17 '18 at 18:04
  • 3
    The [BroadcastReceiver class](https://developer.android.com/reference/android/content/BroadcastReceiver.html) doesn't have this method, so the bigger question is why you think this code *should* work. – DontKnowMuchBut Getting Better Feb 17 '18 at 18:04
  • And you don't appear to be using context anywhere in the code above, even though you state that you tried. Also check the number of parameters once you *do* start using context. – DontKnowMuchBut Getting Better Feb 17 '18 at 18:07
  • 1
    Possible duplicate of [android what is wrong with openFileOutput?](https://stackoverflow.com/questions/3625837/android-what-is-wrong-with-openfileoutput) – DontKnowMuchBut Getting Better Feb 17 '18 at 18:08
  • @Mitchel0022 It is not the same question, because the method I used to read the internal data is work fine in Service class, but I want to read the internal data in my class described above!!!!!! – Majid ff Feb 17 '18 at 18:11
  • @DontKnowMuchBut Getting Better Please read my question well, I wrote my method not BroadcastReceiver class method!!! – Majid ff Feb 17 '18 at 18:19
  • I read your question, and no where do you show where you wrote this method. The method is part of Context, and you need to use your Context object, something that you're most definitely **not** doing. – DontKnowMuchBut Getting Better Feb 17 '18 at 18:44
  • 1
    You're probably getting down votes because your question could have been answered by you by simply looking at the api before asking where you'd see what methods SmsBroadcastReceiver has available to it, and that it does not inherit from Context, and in fact you received a link to that api in the second comment to this question but didn't address it. – Hovercraft Full Of Eels Feb 17 '18 at 19:23

3 Answers3

2

You should replace

 FileInputStream fin = openFileInput(str);

with

FileInputStream fin = getApplicationContext().openFileInput(str);

as mentioned in this thread

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • thanks for tring to answer, it dosn't work, your method work fine in an Activity and what I have is not Activity! – Majid ff Feb 17 '18 at 18:28
  • @Sonali Kale gave the full code to use context , you have to use context from onRecieve methode but maybe you're skipping the line private Context context; from the Sonali's answer – Rainmaker Feb 17 '18 at 19:25
2

Use context in proper way try this.call ReadInternalData() in onReceive

    public class SmsBroadcastReceiver extends BroadcastReceiver{
private Context context;

      public void onReceive(Context context, Intent intent){

this.context = context;
        //Some code here
      }
      //Some code here

      public String ReadInternalData(String str){
        String temp="";
        try{
            FileInputStream fin = context.openFileInput(str);
            int c;
            while( (c = fin.read()) != -1)
            {
                temp = temp + Character.toString((char)c);
            }
        } catch(Exception e)
        {
            e.printStackTrace();
        }
        return temp;
    }
    }
Sonali Kale
  • 136
  • 9
  • thanks, but still the line "this.context = context;" gave the error "Cannot resolve symbol context" – Majid ff Feb 17 '18 at 18:46
  • @Majidff: then you don't have a `Context context` field. Please read the tutorials on variables and fields which you can find here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html – DontKnowMuchBut Getting Better Feb 17 '18 at 19:00
-1

This work with me, thanks for every one tried to solve my problem.

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsBroadcastReceiver extends BroadcastReceiver{
  public void onReceive(Context context, Intent intent){
    //Some code here
    Log.i(TAG, "" + +ReadInternalData("variableName", context));
  }
  //Some code here

  public String ReadInternalData(String str, Context context){

    String temp="";
    try{
        FileInputStream fin = context.openFileInput(str);
        int c;
        while( (c = fin.read()) != -1)
        {
            temp = temp + Character.toString((char)c);
        }
    } catch(Exception e)
    {
        e.printStackTrace();
    }
    return temp;
  }
 }
Majid ff
  • 249
  • 7
  • 22