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>
addAction("pl. example. CHECK_RECEIVER"); br_exemple = new BroadCastReceiver_example(); getApplicationContext(). registerReceiver(br_exemple , filter); //register the Receiver } // call it whenever you want to check if Broadcast Receiver is running.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.
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 yourIncomingSMSReceiver
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With