在广播接收器中读取文件 [英] read files in broadcast receiver

查看:75
本文介绍了在广播接收器中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在清单文件中注册了一个接收器,希望查看即将发送的短信是否具有我的应用程序存储的消息。但是我在访问接收器中的文件时遇到困难。似乎因为我扩展了BroadcastReceiver,所以我无法读取文件。但我不太确定。有人可以如何帮助我。下面是代码

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

    }

}


推荐答案

而不是 openFileInput( string.txt); 尝试使用 context.getApplicationContext()。 openFileInput( string.txt);

这篇关于在广播接收器中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆