我在 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/