【问题标题】:Android write String to file within Broadcast ReceiverAndroid将字符串写入广播接收器中的文件
【发布时间】:2012-01-15 01:02:35
【问题描述】:

我设置了一个广播接收器来捕获 SMS 消息。该类使用 Toast 显示消息,但我需要将字符串写入文件,但因为它不是 Activity,所以它不起作用。

有没有办法可以将“短信”字符串写入广播接收器中的文件?

如果没有,任何人都可以想到另一个收到短信时如何在不运行应用程序的情况下将短信保存到文件?

   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>

这是我第一次发帖,所以我希望我做的一切都是正确的,在此先感谢这个网站已经帮助了我很多。

【问题讨论】:

    标签: java android sms broadcastreceiver writetofile


    【解决方案1】:

    尝试将context 传递给SaveMessage...

    SaveMessage(context, str);
    

    ...并更改您的SaveMessage 方法如下...

    public void SaveMessage(Context context, String message) {
    
        FileOutputStream FoutS = null;
        OutputStreamWriter outSW = null;
    
        try {
            FoutS = context.openFileOutput("Message.dat", Context.MODE_PRIVATE);
    
            // Rest of try block here
        }
        // 'catch' and 'finally' here
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用以下方法为您的数据获取应用程序私有的 File 对象:

      File outFile = new File(getExternalFilesDir(null), "DemoFile.jpg");
      

      (使用onReceive中传递的Context对象。)然后您可以使用java.io中的标准类打开并写入它:

      Writer writer = new FileWriter(outFile);
      . . .
      

      卸载您的应用时,您的外部文件目录中的文件将被删除。

      【讨论】:

        【解决方案3】:

        您没有写入 SDCard 的权限。

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
        

        这里有一个很好的代码示例
        http://androidgps.blogspot.com/2008/09/writing-to-sd-card-in-android.html

        但是,我不会写入 SD 卡的根目录。写入 /data/[application package]/

        是一种很好的做法

        将 [应用程序包] 替换为您的包,例如:com.example.helloandroid

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多