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);
}
}