无法解析方法'openFileInput(java.lang.String)'

时间:2018-02-17 18:03:02

标签: java android android-context android-broadcastreceiver

我有一个广播接收器类,我想使用我的方法“ReadInternalData”来读取我的应用程序中的内部数据,但我无法解决无法解决方法'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;
}
}

我尝试使用上下文:

FileInputStream fin = SmsBroadcastReceiver.openFileInput(str); 

但它不起作用!

编辑:好的抱歉我虽然“SmsBroadcastReceiver”是一个上下文,但是,我的课程既没有服务也没有服务,我想阅读我的应用程序中的内部数据?我认为没有理由对我的问题给予减号!

3 个答案:

答案 0 :(得分:2)

你应该替换

 FileInputStream fin = openFileInput(str);

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

如此thread

中所述

答案 1 :(得分:2)

以正确的方式使用上下文在onReceive中尝试this.call ReadInternalData()

    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;
    }
    }

答案 2 :(得分:-1)

这项工作与我合作,感谢每一个人都试图解决我的问题。

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;
  }
 }