Android Open Source - lost-phone-tracker-app Sms Broadcast Receiver






From Project

Back to project page lost-phone-tracker-app.

License

The source code is released under:

MIT License

If you think the Android project lost-phone-tracker-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package at.fhj.ase13.mobcomp2.phonetracker;
/*from  w  w  w.  j a v a 2 s.  c  o  m*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;

public class SmsBroadcastReceiver extends BroadcastReceiver {

    public SmsBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null
                || !Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
            return;
        }

        Bundle extras = intent.getExtras();
        if (extras != null) {
            PreferenceManager prefs = new PreferenceManager(context);

            Object[] pdus = (Object[]) extras.get("pdus");

            boolean abort = false;
            for (Object pdu : pdus) {
                SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
                String sender = message.getOriginatingAddress();
                String body = message.getMessageBody();

                if (prefs.isStartPasswordCorrect(body)) {
                    // we also enable GPS here as it takes some time to get a location fix
                    GpsToggler.enableGps(context);
                    // hide activity to prevent changing passwords
                    MainActivity.disableActivity(context);
                    LogAlarmReceiver.startAlarm(context, sender);
                    prefs.setReportReceiver(sender);
                    abort = true;

                    break;
                } else if (prefs.isStopPasswordCorrect(body)) {
                    GpsToggler.disableGps(context);
                    MainActivity.enableActivity(context);
                    LogAlarmReceiver.stopAlarm(context);
                    abort = true;

                    break;
                }
            }

            if (abort) {
                // this is not working under KITKAT anymore
                abortBroadcast();

                // as a hack, disable sound and vibration to hide the incoming SMS notification from any possible thief
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    AudioManager audioManager = (AudioManager) context.getSystemService(
                            Context.AUDIO_SERVICE);

                    audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
                    audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
                            AudioManager.VIBRATE_SETTING_OFF);
                    audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
                            AudioManager.VIBRATE_SETTING_OFF);
                }
            }
        }
    }
}




Java Source Code List

at.fhj.ase13.mobcomp2.phonetracker.BootReceiver.java
at.fhj.ase13.mobcomp2.phonetracker.GpsToggler.java
at.fhj.ase13.mobcomp2.phonetracker.LogAlarmReceiver.java
at.fhj.ase13.mobcomp2.phonetracker.MainActivity.java
at.fhj.ase13.mobcomp2.phonetracker.PreferenceManager.java
at.fhj.ase13.mobcomp2.phonetracker.SmsBroadcastReceiver.java
at.fhj.ase13.mobcomp2.phonetracker.SmsLocationReporter.java