Search code examples
javaandroidsmsbroadcastreceiverwritetofile

Android write String to file within Broadcast 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.


Solution

  • Try passing context into SaveMessage...

    SaveMessage(context, str);
    

    ...and change your SaveMessage method as follows...

    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
    }