Why onReceive() keeps getting called? - android

I want my app to send a SMS when it receives a SMS, and I got it working. The problem is that once it gets 1 SMS it doesn't stop sending them.
Its like onReceive() method keeps getting called.
I would like it to send 1 SMS per 1 SMS received.
/******************************************************************************/
BroadcastReceiver class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
public class SMSListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("031130130", null, "sms text", null, null);
}
}
/*****************************************************************************/
/***************************************************************************/
MainActivity
broadcastReceiver = new SMSListener() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED") && intent.getExtras() != null){
try{
doStuffsIfStolenOnce();
}catch (Exception e){
Toast.makeText(Running.this,"Something went wrong:" + e.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
};
IntentFilter myFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(broadcastReceiver, myFilter);
/****************************************************************************/
added intent filter and permission in manifest
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name=".SMSListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
It feels like the problem is something obvious, but i can't find anybody else having this problem.

Solved my problem! Turns out it was my silly mistake, I was testing the app on my phone(real device) and i didn't have a second phone to send the "request" SMS. So I used my own phone to send the initial SMS and the app, working fine, send one back, but that meant it send a SMS to itself and once it received the second SMS it proceeded to send another one... and it got itself in a nice loop.

Related

How to start a service on android every time when a SMS message has been send

I'm using the following library to read the sms messages that have been sent from my default application. The problem that I'm having is that when the application is close the service is not working to save the message that i have send. Does anyone knows how i can start the service when a message has been sent from my device?
Library
https://github.com/tuenti/SmsRadar
UPDATE 1:
BroadcastReceiver class
public class Broadcast_Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")){
Intent service = new Intent(context, SMSService.class);
context.startService(service);
Log.d("Broadcast_Receiver", "message sent");
}
else if ((intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")))
{
Log.d("Broadcast_Receiver", "message received");
}
}
}
manifest
<receiver android:name=".APPServices.SMS.Broadcast_Receiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
Thank you
You'll need to use a "Broadcast Reciever" for that
As the name suggests: broadcast receiver waits for an event to occur and when the event occurs , it receives that info, which was broadcast-ed by the event.
Here is a great tutorial which describes how to implement it step by step (A little long to type it here)
http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87

How can i use Broadcast Receiver?

I want to receive notification when the messages arrives with using Broadcast Receiver. I wrote this code but it doesnt work;
I added this code in my AndroidManifest class;
<receiver android:name=".receiver.SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And my SMSReceiver class;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
Toast.makeText(context, "New SMS: " + messages.getMessageBody(),
Toast.LENGTH_LONG).show();
Log.d(getClass().getName().toString(), "SMS Arrived");
}
}
This code must show me a Toast Message when SMS arrived. How can i fix this problem?
Thank you.
You must have the permissions:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
If you want to send sms you will need permission :
<uses-permission android:name="android.permission.SEND_SMS" />
I think you need to register your broadcast receiver. Hope for help.
#Override
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));
}

BroadcastReceiver onReceive is not fired

I'm trying to cope with SMS receiving funcions in Android.
I read a lot of related topics on Stackoverflow and other sites and I tried with a very simple Class that simply print on the console that a message has been received and the message, but I cannot figure out why it doesn't work.
I see that similar questions remained unanswered in the past (see Android - Broadcast Receiver not being fired)
Hope someone could find where's the problem with my code.
Code:
package com.storassa.android.smsapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Intent recieved: " + intent.getAction());
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
System.out.println("Message recieved: " + messages[0].getMessageBody());
}
}
}
}
}
And Manifest
<manifest package="com.storassa.android.smsapp"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="6" android:targetSdkVersion="6"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application android:label="#string/app_name" android:permission="android.permission.RECEIVE_SMS">
<receiver android:name="com.storassa.android.smsapp.SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
I tried your code, and it's working fine for me.
The post you are referring to has the problem with the setPriority set to 999 and still the broadcast aren't received.
Try setting the priority to Integer.MAX_VALUE or when in xml it should be 2147483647
Some SMS apps will use this Integer.MAX_VALUE as their priority and hence "eat" all messages received.
If it doesn't help setting setPriority to Integer.MAX_VALUE or 2147483647, then try uninstalling all apps that are handling incoming SMS'es and see if the SMS'es comes through to your app.
If this help, then it's one of the applications already installed that are eating the SMS'es.
A work-around for this, would be always to install your application as the first one. By doing this, you will always be the first application receiving the SMS broadcast.
EDIT: setting the priority to Integer.MAX_VALUE or 2147483647 is a hack and should possibly not be used. The setPriority() method only states priority from -1000 - 1000. However I use it in an app myself, but I only handle specific SMS'es received from specific numbers, so I don't see a problem in it.
This is happens when you also have installed other application which has high priority then the current one apps, so increase your priority it will start working, i also got this problem many time, in one app my priority was 100 and it was not working but when i changed it to 999, then it starts working,
So try to set different and large no priority and check it again.
//In on resume and on pause of actvities register the receiver and also in manifest
protected void onPause(){
super.onPause();
unregisterReceiver(SmsReceiver );
}
protected void onResume()
{
super.onResume();
registerReceiver(SmsReceiver ,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver SmsReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};

Android SMS Receiver / Handler

I just want to ask if anyone knows or have a working SMS receiver / handler code for android. Because I've been searching the net for days now and I still haven't seen an updated code, most seem to have deprecated codes on them like the one here http://mobiforge.com/developing/story/sms-messaging-android I would REALLY appreciate it if someone could teach me the new codes for receiving SMS in an application. thanks!
I've just recently implemented a working BroadcastReceiver to handle SMS messages. The key parts are the manifest and the BroadcastReceiver.
In the manifest you need the RECEIVE_SMS permission:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
You don't need READ_SMS. Your receiver entry should look something like this:
<receiver
android:name=".IncomingSmsBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
The bit that most people seem to forget is android:exported="true" which is required because the broadcast originates from outside your application. Some postings suggest you need android:permission="android.permission.RECEIVE_SMS" or android:permission="android.permission.BROADCAST_SMS" but this isn't the case.
My BroadcastReceiver implementation looks like this:
package smsmanager;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class IncomingSmsBroadcastReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
final SmsMessage smsMessage = extractSmsMessage(intent);
processMessage(context, smsMessage);
}
}
private SmsMessage extractSmsMessage(final Intent intent) {
final Bundle pudsBundle = intent.getExtras();
final Object[] pdus = (Object[]) pudsBundle.get("pdus");
final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
return smsMessage;
}
private void processMessage(final Context context, final SmsMessage smsMessage) {
// Do something interesting here
}
}
And everything works just as I want it to, and I can stop burning up my SMS allowance testing ths
This Should work, and is not deprecated, if you replace android.telephony.gsm.SmsMessage with android.telephony.SmsMessage. it's just about listening for android.provider.Telephony.SMS_RECEIVE.
There is a thread here which includes code to do what you're asking for. Note that there are some corrections in the answers there.

Listen incoming calls through BroadcastReceiver, without PhoneStateIntentReceiver or PhoneStateListener

Is there any way to listen to incoming calls by extending BroadcastReceiver to listen to OS's broadcast,without using PhoneStateIntentReceiver or PhoneStateListener.
Also please tell me what will be action and permissions in manifest.
I've tried with as follows but it is not working for incoming calls but working for outgoing
The only one .java file of app is as follows(the app only one .java file and one manifest file)
package com.crsardar.media.audio;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class IncommingCallReceiverCRS extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Chitta : ", "Its working");
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.crsardar.media.audio"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="IncommingCallReceiverCRS" android:enabled="true">
<intent-filter>
<!--action android:name="android.intent.action.NEW_OUTGOING_CALL"/-->
<action android:name="android.intent.action.ANSWER" >
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
</manifest>
The action you have defined in your manifest is incorrect. this is an Action Intent that can be used to answer a call and not monitor incoming calls.
You can use two broadcast receivers that listen to ACTION_PHONE_STATE_CHANGED and NEW_OUTGOING_CALL broadcast intents.
The ACTION_PHONE_STATE_CHANGED will be received when there is a new incoming call, call answered or hangup (See the documentation for the EXTRAs received with this Intent).
The NEW_OUTGOING_CALL will be received when there is a new outgoing call placed on your device.
As for permissions, I think you got it about right in your manifest (I assume the RECORD_AUDIO permission is used for something else in your application)
Here is My demo for android unit test. You can refer to it.
public interface ICallVerify {
void onOutgoing(Context context, Intent intent);
void onCallStateChange(Context context, Intent intent);
}
protected void setUpCallVerify(final ICallVerify callVerify) { //listen ingoing and outgoing
final CountDownLatch latch = new CountDownLatch(1);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { //state change
Log.i(TAG, "outgoing call...");
callVerify.onOutgoing(context, intent);
} else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){ // state changed
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals("RINGING")) {
state += " number:" + intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
Log.i(TAG, "call state changed.... " + state);
callVerify.onCallStateChange(context, intent);
}
}
};
IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
ContextUtils.getTargetContext().registerReceiver(receiver, filter);
try {
latch.await(5, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Dont forget to add permissions
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>

Categories

Resources