读取广播接收器中的文件



我在清单文件中注册了一个接收方,想看看即将到来的短信是否有我的应用程序存储的消息。但我很难访问接收器中的文件。似乎由于我扩展了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");

最新更新