package com.androidexample.broadcastreceiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void writeToFilex(String fileName, String body)
{
FileOutputStream fos = null;
try {
final File dir = new File(Environment.getExternalStorageDirectory().toString() + "/MySMS/" );
if (!dir.exists())
{
dir.mkdirs();
}
final File myFile = new File(dir, fileName + ".txt");
if (!myFile.exists())
{
myFile.createNewFile();
}
String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + fileName;
FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, true);
mFileOutputStream.write(body.getBytes());
mFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeToFile()
{
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirNamexxx");
path.mkdirs();
// initiate media scan and put the new things into the path array to
// make the scanner aware of the location and the files you want to see
// MediaScannerConnection.scanFile(context, new String[] {path.toString()}, null, null);
}
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
int sim = currentMessage.getIndexOnSim();
String senderNum = phoneNumber;
String senderNumx = Integer.toString(sim) + "stf";
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
// setFileValue(senderNumx,message,context);
// writeToFilex(senderNumx,message);
try {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
path = new File(path, "SubDirNamexxx");
path.mkdirs();
// initiate media scan and put the new things into the path array to
// make the scanner aware of the location and the files you want to see
MediaScannerConnection.scanFile(context, new String[] {path.toString()}, null, null);
} catch (Exception e) {
message = e.toString();
writeToFilex("nnnnnnnnnnnnnn.txt",message);
}
int duration = 1000000;
Toast toast = Toast.makeText(context, "senderNum: "+ sim + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}