I am writing an app, which should work even after the app is closed. Here is the code which I wrote to accept the incoming SMS.
public class SMSReceiver extends BroadcastReceiver {
private ResultReceiver receiver;
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
// Parse the SMS.
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
// Retrieve the SMS.
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 += " :\n";
str += " :\n";
str += "MessageBody: ";
str += msgs[i].getMessageBody();
}
Log.i("SmsReceiver message := " + str);
}
}
}
I had registered this Broadcast, in the my main activity. and I am not unregistering broadcast.
This works when my App is launched, but doesn't work when app is closed.
How can I receive SMS even after the app is closed? Any help is appreciated.
Thanks