Search code examples
androidservicegpsbroadcastreceiver

to launch the application when the sms service recieves sms


I am trying to launch an application when a background sms service receives a particular senders sms.I have written the msg receiving part but my app does not start when the msg receives.it needs to start the map location activity.

My msgrecieve class

public class MsgRecieve extends Service {
public class MsgReciever extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
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();
        String senderNum = phoneNumber;
        String message = currentMessage.getDisplayMessageBody();
        log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
                        // Show Alert
        if (senderNum.equals("956xxxxxxx")) {
        int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
toast.show()
/*Intent  i1 = new Intent(context,MapsActivity.class);
  i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(i1);*/
                     }
                 } // end for loop
             } // bundle is null
        } catch (Exception e) {
                e.printStackTrace();
          }
      }
 }

My activity class

try {
super.onCreate(savedInstanceState);
setApplicationContext();
setContentView(R.layout.activity_main);
Intent intent1 =new Intent(this,MsgRecieve.class);
startService(intent1);
TextView txtImei1 = (TextView) findViewById(R.id.imei1);
TextView txtImei2 = (TextView) findViewById(R.id.imei2);
TextView txtSerial = (TextView) findViewById(R.id.serialNo);
TextView txtAlert = (TextView) findViewById(R.id.alertMsg);
IMEIHelper imeiHelper = new IMEIHelper();
TelephonyInfo telephonyInfo = imeiHelper.getInfo(getApplicationContext());
  if (telephonyInfo.isSIM1Ready()) {
      txtImei1.setText(StringHelper.IMEI + "1:" + telephonyInfo.getImeiSIM1());
      } else {
              txtImei1.setText(StringHelper.IMEI + ":NA");
             }
            if (telephonyInfo.isSIM2Ready()) {
                txtImei2.setText(StringHelper.IMEI + "2:" + telephonyInfo.getImeiSIM2());
               }

Manifest

<service
android:name="edu.am.edu.am.iotm.android.activity.MsgRecieve"
android:enabled="true"
android:exported="true" >
</service>
</application>
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<!--The ACCESS_COARSE/FINE_LOCATION permissions are not required to useGoogle Maps Android API v2, but are recommended.-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<intent-filter android:priority="901" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>

Solution

  • From this Tutorial For Intercepting SMS:

    Manifest:

    <receiver android:name="com.androidexample.broadcastreceiver.IncomingSms">   
      <intent-filter>
          <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
    </receiver>
    
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
       <uses-permission android:name="android.permission.READ_SMS" />
    

    Broadcast Receiver class:

    public class IncomingSms extends BroadcastReceiver {
        
        // Get the object of SmsManager
        final SmsManager sms = SmsManager.getDefault();  
        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();
                        
                        String senderNum = phoneNumber;
                        String message = currentMessage.getDisplayMessageBody();
    
                        Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
                        
    
                       // Show Alert
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText(context, 
                                     "senderNum: "+ senderNum + ", message: " + message, duration);
                        toast.show();
                        
                    } // end for loop
                  } // bundle is null
        
        
        /* Start a new application*/
              Intent i;
              PackageManager manager = getPackageManager();
              try { 
                  i = manager.getLaunchIntentForPackage("application package name");
                  if (i == null)
                      throw new PackageManager.NameNotFoundException();
                  i.addCategory(Intent.CATEGORY_LAUNCHER);
                  startActivity(i);
              } catch (PackageManager.NameNotFoundException e) {  } 
    
            } catch (Exception e) {
                Log.e("SmsReceiver", "Exception smsReceiver" +e);
                
            }
        }    
    }
    

    Edit: try this, i have made the edit in you messagereceive class:

    public class MsgRecieve extends Service {
    public class MsgReciever extends BroadcastReceiver {
    final SmsManager sms = SmsManager.getDefault();
    
    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();
            String senderNum = phoneNumber;
            String message = currentMessage.getDisplayMessageBody();
            log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
                            // Show Alert
            if (senderNum.equals("956xxxxxxx")) {
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
            toast.show()
            Intent mIntent;
            PackageManager manager = getPackageManager();
            try { 
                mIntent = manager.getLaunchIntentForPackage("com.application.package.name");
                if (mIntent == null)
                    throw new PackageManager.NameNotFoundException();
                mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(mIntent);
            } catch (PackageManager.NameNotFoundException e) {  } 
            }
            } // end for loop
            } // bundle is null
        } catch (Exception e) {
                e.printStackTrace();
        }
        }
     }