Android 将字符串写入广播接收器中的文件
我已经设置了广播接收器来捕获短信。该类使用 Toast 显示消息,但我需要将字符串写入文件,但因为它不是活动,所以不起作用。
有没有办法将“短信”字符串写入广播接收器内的文件?
如果没有,谁能想出另一种方法将短信保存到文件中,而无需在收到短信时运行应用程序?
public class SMSReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//---Save SMS message---
SaveMessage(str);
}
}
public void SaveMessage(String message) {
FileOutputStream FoutS = null;
OutputStreamWriter outSW = null;
try {
FoutS = openFileOutput("Message.dat", MODE_PRIVATE);
outSW = new OutputStreamWriter(FoutS);
outSW.write(message);
outSW.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
outSW.close();
FoutS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
语法错误“MODE_PRIVATE 无法解析为变量”
- 该代码仅在活动类中使用时才有效
清单文件:
<receiver android:name=".SMSReciever">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED"
android:enabled="true" />
</intent-filter>
</receiver>
这是我的第一篇文章,所以我希望我所做的一切都是正确的,提前感谢这个网站已经帮助了我很多。
I have set up a Broadcast Receiver to capture SMS messages. The Class display the message using Toast but I need to write the String to a file but because it's not an Activity it doesn't work.
Is there a way I can write the "sms message" String to a file within the Broadcast Receiver?
If not can anyone think of another way to save an SMS message to a file without the app running when the text message is received?
public class SMSReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//---Save SMS message---
SaveMessage(str);
}
}
public void SaveMessage(String message) {
FileOutputStream FoutS = null;
OutputStreamWriter outSW = null;
try {
FoutS = openFileOutput("Message.dat", MODE_PRIVATE);
outSW = new OutputStreamWriter(FoutS);
outSW.write(message);
outSW.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
outSW.close();
FoutS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
syntax error "MODE_PRIVATE cannot be resolved to a variable"
- The code only works when used in an activity class
manifest file:
<receiver android:name=".SMSReciever">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED"
android:enabled="true" />
</intent-filter>
</receiver>
This is my first ever post so I hope I did everything correctly, Thanks in advance this site has helped me so much already.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将
context
传递到SaveMessage
......并更改您的
SaveMessage
方法,如下所示...Try passing
context
intoSaveMessage
......and change your
SaveMessage
method as follows...您可以使用以下方法获取数据的应用程序私有 File 对象:(
使用
onReceive
中传递的Context
对象。)然后您可以使用以下命令打开并写入它:来自java.io
的标准类:卸载应用程序时,外部文件目录中的文件将被删除。
You can use the following to obtain an application-private File object for your data:
(Use the
Context
object passed inonReceive
.) Then you can open and write to it using standard classes fromjava.io
:The files in your external files directory will be deleted when your app is uninstalled.
您无权写入 SDCard。
这里有一个很好的代码示例
http://androidgps.blogspot.com/2008 /09/writing-to-sd-card-in-android.html
但是,我不会写入 SD 卡的根目录。最好写入 /data/[application package]/
将 [application package] 替换为您的包,例如:com.example.helloandroid
You don't have permission to write to the SDCard.
A good code sample is here
http://androidgps.blogspot.com/2008/09/writing-to-sd-card-in-android.html
However, I wouldn't write to the root of the SD Card. It's good practice to write to /data/[application package]/
Replace [application package] with your package, ex: com.example.helloandroid