android - 读取广播接收器中的文件

标签 android file-io broadcastreceiver

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

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

    }

}

最佳答案

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

关于android - 读取广播接收器中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9289957/

相关文章:

java - Android:检测整个 Activity 的长按

android - 房间 : related entities - usable public constructor

file-io - unix 命令 'mv' 失败

android - 无法检测用户 Activity 转换,使用广播接收器

android - 如何在 "offline"模式下检测耳机插拔事件

java - 如何在android中动态附加字符串并添加到requesturl?

带有图标的Android寻呼机滑动选项卡

python - 无法写入文件

C# 文件处理 : Create file in directory where executable exists

android - 如何在 Android 中捕获超过 80 字节的短信?