3

I m looking to add sms reading functionality in my app. The user enters his mobile number in an activity and then a sms containing one time password is sent to the user mobile phone from the server. What I want to do is trigger the app to read incoming messages after the user has entered the mobile number and is expecting incoming message from the server.

I have seen some questions like this answered here How to read the incoming message using service in background in android?

but the solution seems to be reading all the sms that come on the phone. In my case I just want the reading functionality to start only when user is expecting a sms and end when the message has been received.

Community
  • 1
  • 1
Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126

2 Answers2

1

check this answer that show you how to register a receiver https://stackoverflow.com/a/7049747/774944

final String SOME_ACTION = "android.provider.Telephony.SMS_RECEIVED";
IntentFilter intentFilter = new IntentFilter(SOME_ACTION);
SMSReceiver mReceiver = new SMSReceiver();
registerReceiver(mReceiver,intentFilter);
Community
  • 1
  • 1
jamil
  • 352
  • 1
  • 3
  • 12
0

You can enable/disable broadcast receivers at run time using package manager.

public void enableSmsModule()
{
    PackageManager pm = getPackageManager();
    ComponentName componentName = new ComponentName(getApplicationContext(), SMSReceiver.class);
    pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
            PackageManager.DONT_KILL_APP);
}

public void disableSmsModule()
{
    PackageManager pm = getPackageManager();
    ComponentName componentName = new ComponentName(getApplicationContext(), SMSReceiver.class);
    pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
            PackageManager.DONT_KILL_APP);
}

Code for reading sms : How to read the incoming message using service in background in android?

Community
  • 1
  • 1
Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126