0

I'm creating an application that needs to read incoming SMS. Saving to a SMS provider is not required - just showing the toast at the time of receiving SMS.

<receiver android:name=".SmsHandler"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="2147483647">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

Receiver

public class SmsHandler extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Receive sms", Toast.LENGTH_LONG).show();
    }
}

Receiver working when app running, if app kill, receiver not call.

I know that with Android KitKat you need to specify the default applications for working with sms. But this is necessary if you want to save SMS to the SMS provider. This is not required if you need to catch the moment of receiving SMS.

What should I do to make the receiver work when the application is killed?

UPDATE

I tried to use the service to register BroadcastReceiver

public class AlarmService extends Service {
private static BroadcastReceiver SmsHandler;

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

@Override
public void onCreate()
{
    registerScreenOffReceiver();
}

@Override
public void onDestroy()
{
    unregisterReceiver(SmsHandler);
    SmsHandler = null;
}

private void registerScreenOffReceiver()
{
    SmsHandler = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Receive sms", Toast.LENGTH_LONG).show();
        }
    };
    IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(SmsHandler, filter);
}

}

user1854307
  • 580
  • 4
  • 8
  • 21
  • 1
    Your device is killing the app in background please check if there is any protected app feature or run in background app setting in your device. – Ashish Pardhiye Jun 05 '17 at 08:40
  • on which android version are you testing this? – marmor Jun 05 '17 at 08:44
  • You need to create service and register receiver inside that service Like this:- https://stackoverflow.com/questions/16824341/keep-broadcast-receiver-running-after-application-is-closed – santoXme Jun 05 '17 at 08:44
  • I'm testing for android 6. – user1854307 Jun 05 '17 at 08:47
  • are you killing your app using the "force stop" button? if so, android will refrain from calling any of your receivers until the user manually launches the app again, see: https://stackoverflow.com/a/19383077/819355 and https://developer.android.com/about/versions/android-3.1.html (Launch controls on stopped applications) – marmor Jun 05 '17 at 08:52
  • I created the service by analogy https://stackoverflow.com/questions/16824341/keep-broadcast-receiver-running-after-application-is-closed, but still the receiver does not work after the application is killed. – user1854307 Jun 05 '17 at 10:27
  • I updated my description to show how I used the service. – user1854307 Jun 05 '17 at 10:33

0 Answers0