16

I am trying to get phone number and phonebook name from a text message. When I run it from application, and close application, it works, but, when I restart my mobile, it doesn't work. Anybody?

public class IncomingSMSReceiver extends BroadcastReceiver {
    private static final String queryString = "@zovi";
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    public void onReceive(Context _context, Intent _intent) {
        if (_intent.getAction().equals(SMS_RECEIVED)) {
            Intent intent = new Intent(_context, IncomingSMSService.class);
            _context.startService(intent);
            Bundle bundle = _intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++)
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                for (SmsMessage message : messages) {
                    String msg = message.getMessageBody();
                    Log.i("Poruka", msg);
                    String to = message.getOriginatingAddress();
                    String contactName = TelefonUtils.getContact(_context, to);
                    Log.i("Od", contactName + "\n" + to);
                }
            }
        }
    }
}

My XML:

<receiver android:name=".telefon.receivers.IncomingSMSReceiver"
          android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="500">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>
4
  • 1
    did you exit your application before restarting the device? What version of Android are you running on there? Sep 7, 2015 at 9:28
  • create another receiver for BOOT_COMPLETE. and register your msg receiver in it ..
    – Moinkhan
    Sep 8, 2015 at 9:09
  • also add this action <action android:name="android.intent.action.BOOT_COMPLETED" /> in Intent filtert
    – Moinkhan
    Sep 9, 2015 at 6:25
  • @Miljan Vulovic if my answer helps you pls mark as accepted. Sep 28, 2015 at 8:15

7 Answers 7

9
+50

In your AndroidManifest.xml try to add <action android:name="android.intent.action.BOOT_COMPLETED" /> action under IncomingSMSReceiver receiver tag.

To start Services or BroadcastReceiver automatically after the Android system restarts or starts you can register a BroadcastReceiver to the Android android.intent.action.BOOT_COMPLETED system event.

Try this code.

<receiver android:name=".telefon.receivers.IncomingSMSReceiver"
 android:permission="android.permission.BROADCAST_SMS">
       <intent-filter android:priority="500">
       <action android:name="android.provider.Telephony.SMS_RECEIVED" />
       <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

If you have tried android.intent.action.BOOT_COMPLETED then it will not work in your case because you forgot to add BOOT_COMPLETED intent in your
IncomingSMSReceiver and it will work only in case of android.provider.Telephony.SMS_RECEIVED because of if condition you have used in IncomingSMSReceiver. so change if condition from

if (_intent.getAction().equals(SMS_RECEIVED)) {

to

if (_intent.getAction().equals(SMS_RECEIVED) || _intent.getAction().equals(BOOT_COMPLETED)) {

and also define private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; in IncomingSMSReceiver. Below is full code of IncomingSMSReceiver.

Change your IncomingSMSReceiver code to this:

public class IncomingSMSReceiver extends BroadcastReceiver {
    private static final String queryString = "@zovi";
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";

    public void onReceive(Context _context, Intent _intent) {
        if (_intent.getAction().equals(SMS_RECEIVED) || _intent.getAction().equals(BOOT_COMPLETED)) {
            Intent intent = new Intent(_context, IncomingSMSService.class);
            _context.startService(intent);
            Bundle bundle = _intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++)
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                for (SmsMessage message : messages) {
                    String msg = message.getMessageBody();
                    Log.i("Poruka", msg);
                    String to = message.getOriginatingAddress();
                    String contactName = TelefonUtils.getContact(_context, to);
                    Log.i("Od", contactName + "\n" + to);
                }
            }
        }
    }
}

So when your phone restarts it will receive android.intent.action.BOOT_COMPLETED and call your IncomingSMSReceiver receiver then it will start your IncomingSMSService.

I hope it will help you.

3

Make sure that the receiver starts after phone reboots.

Add <action android:name="android.intent.action.BOOT_COMPLETED" /> in intent-filter

1
  • Try adding boot completed permission in your manifest file like <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    – Alfred Woo
    Sep 2, 2015 at 2:58
2

I used this in my project, find this from Android SMS Receive Listener

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import java.util.ArrayList;

/**
* @@author Chathura Wijesinghe <[email protected]> 
* 
* <receiver android:name=".SMSReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
*/
public class SMSReceiver extends BroadcastReceiver {

private static final String SMS_RECEIVED         = "android.provider.Telephony.SMS_RECEIVED";

private static ArrayList<SMSReceivedListner> smsListner = new ArrayList<SMSReceivedListner>();

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    final Bundle extras = intent.getExtras();


    if (action.equals(SMSReceiver.SMS_RECEIVED)) {
        final boolean smsValid = extras != null;

        if (smsValid) {
            //Create SMSMessages from PDUs in the Bundle
            final Object[] pdus = (Object[])extras.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++)
                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);

            //Assemble
            final ArrayList<Long> vibrations = new ArrayList<Long>();

            for (SmsMessage message : messages) {
                for (SMSReceivedListner smsReceivedListner : smsListner )
                    smsReceivedListner.message(message);
            }
        }
    }
}

public static void addSMSListner(SMSReceivedListner listner){
    smsListner.add(listner);
}

public static void removeSMSListner(SMSReceivedListner listner){
    smsListner.remove(listner);
}

public interface SMSReceivedListner{
    public void message(SmsMessage message);
}
}
1

You have to remove the android:permission="android.permission.BROADCAST_SMS" from the receiver declaration. No BOOT_COMPLETED or other permission are required.

1

explicitly set exported to true in your manifest as an element for the Receiver

 android:exported="true"

and make sure both of your application element and receiver element are enabled

android:enabled="true"
2
  • try this sir, add a Log as the first line right in onReceive and see if it gets called, if it does then its prolly your code, then we can digg there@VulovicVukasin
    – Elltz
    Sep 5, 2015 at 14:17
  • have added logging on start of every receiver. Everything gets called after reboot only sms receiver doesnt. Sep 5, 2015 at 15:08
1

Broadcast code seems to be okay and Make sure that you are using any other SMS apps in your device(May this SMS Broadcast might interrupting your apps Broadcast) . If you are not using no other SMS apps then it should work otherwise un-install that SMS app and try once.

and also check you have added the following permissions in your AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

Hope this will work.

1

Ideally it should have worked.Is weird that its not working.

Remove android:permission="android.permission.BROADCAST_SMS" from the broadcast receiver.It is not required.

Try increasing the priority android:priority="2147483647" to maximum.It might be some other app is consuming the event or try the below solution.

Add <action android:name="android.intent.action.BOOT_COMPLETED" /> in your intent filter and see if it works.

Something like this:

<receiver android:name=".telefon.receivers.IncomingSMSReceiver"
          android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="500">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

and add permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>    
15
  • tried that, not working. Also, tried calling activity from BOOT_COMPLETED receiver, also not working... Sep 1, 2015 at 22:38
  • @MiljanVulovic did u add the permission Sep 1, 2015 at 22:40
  • yes. I Have one receiver that is BOOT_COMPLETED, it works, just this incoming message isnt Sep 1, 2015 at 22:41
  • should I create a service for this? Sep 1, 2015 at 22:42
  • make the priority to maximum. Sep 1, 2015 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.