ParKein ParKein - 3 months ago 20
Android Question

How to always listen to SMS android

Basically, I'm trying to make an app that always listens to new

SMS
.
The point is, i have made a Receiver that will auto-start at device boot, and it can also listen for new SMS, but only if the app is opened, if i close it, then it doesn't work anymore.
Here is my Service calling a BroadCastReceiver:

public class ServiceCommunicator extends Service {
private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;
private boolean receiverAlive = false;

@Override
public void onCreate(){
super.onCreate();

//SMS event receiver
mSMSreceiver = new SMSReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
}

public class SMSReceiver extends BroadcastReceiver {
public SmsMessage messages[] = null;

private void showNotification(Context context, String sms) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText(sms);
mBuilder.setContentIntent(contentIntent);

NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
messages = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
String sms = "";
String mobile = "34821049283";

for (int i = 0; i < pdus.length; i++) {
SmsMessage tmp = SmsMessage.createFromPdu((byte[]) pdus[i]);
String senderMobile = tmp.getOriginatingAddress();

if (senderMobile.equals(mobile)) {
sms = tmp.getMessageBody();
showNotification(context, sms);

break;
}
}
}
}
} catch (Exception e) {
Log.d("Exception caught", e.getMessage());
}

receiverAlive = false;
mSMSreceiver = new SMSReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
}
}


And here is my MainClass

public class MainActivity extends Activity {
private static SmsMessage[] messages = null;
public static Activity mActivity = null;
public static String mobile = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
}

@Override
protected void onStart(){
super.onStart();

mActivity = this;
mobile = ((EditText) findViewById(R.id.txtMobile)).getText().toString();

Intent intent = new Intent(this, ServiceCommunicator.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
}
}

Answer

Register your BroadcastReceiver in the manifest, using a <receiver> element, instead of using registerReceiver().

Note that:

  • You still will not get all SMS messages, as SMS broadcasts are ordered, and some receiver with higher priority than yours can abort the broadcast

  • Your receiver will not work when first installed on a device, until the user launches an activity of yours, or something else explicitly runs one of your components

Also, note that Android 4.4 significantly overhauled how the SMS process works, so keep that in mind.

Comments