read files in broadcast receiver

烈酒焚心 提交于 2019-12-10 22:47:52

问题


I registered a receiver in manifest file want to see if the coming sms has the message stored by my application. but I have difficulties in accessing files in the receiver. it seems since i extends BroadcastReceiver, i cannot read files. but i'm not quite sure. how somebody can help me. below is the code

public class BootReceiver extends BroadcastReceiver {


    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    String password;

    @Override
    public void onReceive(Context context, Intent intent) {

        try {
            InputStream in = openFileInput("string.txt");
            if (in != null) {
                InputStreamReader tmp = new InputStreamReader(in);
                BufferedReader reader = new BufferedReader(tmp);
                String str;
                StringBuilder buf = new StringBuilder();
                while ((str = reader.readLine()) != null) {
                    buf.append(str);
                }
                in.close();
                password = buf.toString();
            }
        }
        catch (Throwable t) {
            ;
        }


        if (intent.getAction().equals(ACTION)) {

            Intent initial = new Intent(context, SMSActivity.class);
            initial.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(initial);
        }

    }

}

回答1:


Instead of openFileInput("string.txt"); try using context.getApplicationContext().openFileInput("string.txt");.



来源:https://stackoverflow.com/questions/9289957/read-files-in-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!