我在清单文件中注册了一个接收器,想看看即将发布的短信是否有我的应用程序存储的消息。但我在访问接收器中的文件时遇到困难。似乎因为我扩展了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);
}
}
}
答案 0 :(得分:2)
而不是openFileInput("string.txt");
尝试使用context.getApplicationContext().openFileInput("string.txt");
。