1

I know this is a similar question to mine. https://stackoverflow.com/a/22511290/2208342 but i have read that the solution to that question will become obsolete as Android are taking away that code.

qoute below taken from https://stackoverflow.com/a/5921190/2208342

I'd discourage the use of this solution. For Android L they're removing ActivityManager.getRecentTasks() and it had the same note in the documentation. So be warned!

I have also heard that this solution does not work on android kitkat.

BroadCastReceiver Class

public class IncomingSms extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();


public IncomingSms(){}


@Override
public void onReceive(Context context, Intent intent) {

    final Bundle bundle = intent.getExtras();

    Intent intent1 = new Intent(context, CallDetectService.class);



    if (context.stopService(intent1)) {
        Toast.makeText(context,"if context",Toast.LENGTH_LONG).show();
        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();


                    String sms = "TEST";
                    try {
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(senderNum, null, sms, null, null);

                        Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();

                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
                    }


                    Log.i("SmsReciver", "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

        } catch (Exception e) {
            Log.e("SmsReciever", "Exeption smsReceiver" + e);
        }
    }
    else{
        Toast.makeText(context,"else context",Toast.LENGTH_LONG).show();
    }

}
}

Service Class

public class CallDetectService extends Service {


private CallHelper callHelper;

public CallDetectService() {
}


@Override
public int onStartCommand(Intent intent, int flags, int startId){
    callHelper = new CallHelper(this);


    int res = super.onStartCommand(intent, flags, startId);
    callHelper.start();
    Toast.makeText(getApplicationContext(), "Starting Service", Toast.LENGTH_LONG).show();

    return res;
    }

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


    callHelper.stop();
    //Toast.makeText(getApplicationContext(),"Stop Service",Toast.LENGTH_LONG).show();


}

@Override
public IBinder onBind (Intent intent) {
        return  null;
}
}

in my MainActivity i have this piece of code which is exactly what i want to in my broadcast receiver.

Intent intent = new Intent(this,CallDetectService.class);

    if (stopService(intent)){
Community
  • 1
  • 1
Andrew Irwin
  • 691
  • 12
  • 40

1 Answers1

0

There is a workaround but only if you are using the receiver in your activity:

  • add the receiver class as inner class in your activity

    public final class MyActivity extends Activity {
        public final class IncomingSms extends BroadcastReceiver {
           ...
    }
    
  • register/unregister the receiver dynamically in the activity onStart()/onStop()
  • have your activity getting the reference of your service whenever you are binding to this service
  • in your receiver onReceive() use the reference

Note

Keep in mind though that above implementation comes with some flaws:

  • inner classes having explicit access to any outer class member variables (fields and methods)
  • this opens a potential memory leakage front, especially if your service is doing something in a separate thread
  • your receiver is alive while activity exists
Dimitar Genov
  • 2,056
  • 13
  • 11
  • Thank you. How do i Register/UnRegister receiver and how do i reference service. If my service is running i want the broadcast receiver to be on and if service is not running i want my broadcast receiver to be off. – Andrew Irwin Jul 12 '15 at 12:25
  • Please elaborate the logic behind having a service: 1) who needs to listen when a service is on/off 2) what exactly do you need the receiver for – Dimitar Genov Jul 12 '15 at 13:13
  • when my app is switched to on mode (when service is running). any incoming sms will be auto replied to with a sms. when my app is switched to off mode (when service is not running). I do not want incoming sms to be auto replied to. – Andrew Irwin Jul 12 '15 at 13:16
  • I am new to android. this is my first app. so if could explain with a bit more code i would be very greatfull. as I am finding it hard to understand – Andrew Irwin Jul 12 '15 at 13:18