我在清單文件中註冊了一個接收器,希望看看即將到來的短消息是否有我的應用程序存儲的消息。但我在接收器中訪問文件時遇到困難。似乎自從我擴展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);
}
}
}
你得到任何錯誤? – Farhan 2012-02-15 08:25:43
是的,有關於openFileInput的錯誤:方法openFileInout(String)是未定義的類型BootReceiver – jedichen 2012-02-16 02:55:53