SMS Broadcast Receiver onReceive() method not being called - android

I am building an SMS application and there is one activity and a broadcast receiver. The problem is that the broadcast receiver onReceive() method is not getting called. The code for the receiver class and manifest file is given below.
Broadcast Receiver
public class SmsBroadcastReceiver extends BroadcastReceiver {
public AudioManager audioManager;
SharedPreferences sharedPreferences;
boolean b=false;
String smsBody;
final int REQUEST_CODE_ASK_PERMISSIONS = 123;
public static final String SMS_BUNDLE = "pdus";
SmsMessage smsMessage;
public void onReceive(Context context, Intent intent) {
Toast.makeText(context.getApplicationContext(),"Toast Long",Toast.LENGTH_LONG).show();
sharedPreferences = context.getApplicationContext().getSharedPreferences("Pritom", context.MODE_PRIVATE);
Bundle intentExtras = intent.getExtras();
String format = intentExtras.getString("format");
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
String pass3 = sharedPreferences.getString("password", null);
for (int i = 0; i < sms.length; ++i) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
} else {
smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
}
smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
/*if (smsBody.equals("#general" + pass3)) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(audioManager.RINGER_MODE_NORMAL);
}*/
Toast.makeText(context, "SMS : " + smsBody + pass3, Toast.LENGTH_SHORT).show();
Log.d("Message:", smsBody + pass3);
}
}
}
The problem here is that the Toast message just below onReceive() is not being displayed which indicates that the method onReceive() is not being called.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mytrendin.inappmessaging">
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsBroadcastReceiver" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
I have used Android 5.0.1 for testing my application.
Thank you for your help :)

Try setting the higher priority android:priority="2147483647" in receivers intent-filter:
<receiver android:name=".SmsBroadcastReceiver" android:exported="true" >
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

Related

Cannot Receive SMS with broadcast

I just need to get incoming SMS to handle it, but nothing is called from onRecieve method. everything looks just ok , but nothing is happened when i receive SMS!
here is my manifest tags inside application tags :
<receiver android:name="com.chargeirancell.key0098.view.RubinAppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/rubinwidgetinfo" />
</receiver>
<receiver android:name="com.chargeirancell.key0098.RubinRecieveSMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
here also are permissions outside application tag:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
this my receiver class :
public class RubinRecieveSMS extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("SMS", "HI");
if (ChargeApp.mShared.getBoolean("sms", true)) {
Log.i("SMS", "HI");
String number = "";
String message = "";
Bundle bundle = intent.getExtras();
if (bundle != null) {
try {
int i;
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] currentMessages = new SmsMessage[pdusObj.length];
for (i = 0; i < currentMessages.length; i++) {
currentMessages[i] = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
number = currentMessages[i].getOriginatingAddress();
message = message
+ currentMessages[i].getMessageBody();
}
String pin;
if (number.contains("3453")) {
pin = message.substring(message.indexOf("ز:"),
message.indexOf("ک")).replace("ز:", "");
Intent i1 = new Intent(context,
ActivityDialogCharge.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.putExtra("pin", pin);
context.startActivity(i1);
} else if (number.contains("8801 9574")) {
pin = message.substring(message.indexOf("ز:"),
message.indexOf("ک")).replace("ز:", "");
Intent i1 = new Intent(context,
ActivityDialogCharge.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.putExtra("pin", pin);
context.startActivity(i1);
}
} catch (Exception e) {
}
}
}
}
}
even Hi is not printed in logCat!
can anyone please help me on this?
Replace code into your manifest file:
<receiver android:name=".RubinRecieveSMS" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

can not receive a message (broadcast recceiver ) through this code

am not getting a message through my app - help me out i dont know what changes i have to make in this code , tell me should i make it to default(so it will give me a message) or something else do in my code - i do receive a message but not from this app
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage
.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum
+ "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "
+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.smsmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I think, you should add this code
<receiver android:name=".yourService" android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
in application tag in Manifest
or
or you must register your InComingservice in activity class.
For example
private InComingSMSReceiver inComingSMSReceiver = new InComingSMSReceiver();
#Override
protected void onResume() {
super.onResume();
registerReceiver(inComingSMSReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(inComingSMSReceiver);
}
It is correct answer , MainAtivity.java class was fine but i did make changes in manifest as u people helped me. and its working perfectly
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.smsmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".IncomingSms"
android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Android SMS dynamic Broadcast

I have a Program that is sending a SMS to a device witch answers with an SMS.
I want to receive the SMS in an give the message back to my program.
Cause I will only wait for a certain time I need a dynamic registration
Here my Code for the Breadcast Receiver
public class SMSReceiver extends BroadcastReceiver {
private MainActivity father = null;
private String call_number = null;
public void setFather (MainActivity father) {
this.father = father;
call_number = father.getCallNumber().trim();
while (call_number.startsWith("0")) {
call_number = call_number.substring(1);
}
}
public void onReceive(Context cxt, Intent intent)
{
//if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
String sender = smsMessage[0].getDisplayOriginatingAddress();
String mess = smsMessage[0].getDisplayMessageBody();
String toast = "Received SMS from: " + sender + "\nMessage: " + mess;
//if ((sender != null) && (sender.endsWith(call_number))) {
father.setSMSMessage((mess ==null ? "": mess)+ "\n from "+sender);
//}
Toast.makeText(cxt, toast, Toast.LENGTH_LONG).show();
}
}
}
Here my Code for the Registration
myreceiver=new SMSReceiver();
myreceiver.setFather(this);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(myreceiver, filter);
Here my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.standheizung"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMSChecker">
<intent-filter>
<action android:name="SMS_SENT" />
<action android:name="SMS_DELIVERED" />
</intent-filter>
</receiver>
</application>
</manifest>
My problem is that I get a SMS from the Device but my receiver does nothing.
Comment the Receiver for Sending the SMS works without any Problem
Has anybody an idea whats the problem ???

my sms receiver can't accept new sms

i want to receive sms on my application, but when i try to get new sms my application didn't get the new sms. I cannot find where I am doing wrong. I'm not sure if there's something wrong with the code, or debugging.
I'm trying to be notified if a new SMS arrives and save the sms on my database.
this is my receiver.
public void onReceive( Context c, Intent i) {
Bundle b = i.getExtras();
SmsMessage[] m = null;
String s = "";
TelephonyManager teleponyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
int x;
if (b != null) { //IF valid SMS
Object[] p = (Object[]) b.get("p");
m = new SmsMessage[p.length];
for (x = 0; x < m.length; x++) { //FOR ambil konten SMS
m[x] = SmsMessage.createFromPdu((byte[])p[x]);
s += "SMS dari " +m[x].getOriginatingAddress().toString().trim();
s += " :";
s += m[x].getMessageBody().toString();
s += "\n";
}
Toast.makeText(c, s, Toast.LENGTH_LONG).show();
nomor = m[x].getOriginatingAddress().toString().trim();
pesan = m[x].getMessageBody().toString();
Cursor cursorKontak = data.pilihKontak(nomor);
if(cursorKontak.moveToFirst()) {
idkontak = cursorKontak.getString(cursorKontak.getColumnIndex("idkontak"));
}
if(idkontak == null) {
nama = nomor;
data.inputKontak(nama, nomor);
Cursor cursorKontak2 = data.pilihKontak(nomor);
if(cursorKontak2.moveToFirst()) {
idkontak = cursorKontak2.getString(cursorKontak2.getColumnIndex("idkontak"));
}
data.inputPesanMasuk(idkontak, pesan);
}else {
data.inputPesanMasuk(idkontak, pesan);
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", s);
c.sendBroadcast(broadcastIntent);
my android manifest
Update
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.sms" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:label="#string/app_name"
android:name="EnkripsiSMS" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".SMSReceiver" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<activity android:name=".KotakMasuk"/>
<activity android:name=".TulisPesan"/>
<activity android:name=".KotakKeluar"/>
<activity android:name=".HasilEnkripsi"/>
<activity android:name=".KirimPesan"/>
<activity android:name=".IsiPesanKeluar"/>
<activity android:name=".DekripsiPesanKeluar"/>
<activity android:name=".HasilDekripsiPesanKeluar"/>
<activity android:name=".TeruskanPesanKeluar"/>
<activity android:name=".KirimPesanKeluar"/>
<activity android:name=".IsiPesanMasuk"/>
<activity android:name=".DekripsiPesanMasuk"/>
<activity android:name=".TeruskanPesanMasuk"/>
<activity android:name=".HasilDekripsiPesanMasuk"/>
<activity android:name=".Balas"/>
<activity android:name=".HasilBalas"/>
<activity android:name=".KirimPesanMasuk"/>
</application>
</manifest>
can somebody help me?
i really need the solution.
thanks..
Instead of p use pdus
Object[] p = (Object[]) b.get("pdus");

sms receiver from broadcast receiver

I am building a simple app to receive SMS using broadcast receiver on ICS platform , but it is not receiving SMS at all ... it do not show a Toast ?
I have 3 class:
MessageActivity: which is main class
SimpleSmsRecever: to receive sms
Reply: to reply
my mainfest file is :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.message"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MessageActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name=".SmsReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</activity>
<activity android:name=".Reply" >
</activity>
</application>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
My code for Receive Sms is:
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
private static String sender, body;
private static int flag = 0;
#Override
public void onReceive(Context context, Intent intent) {
MessageActivity mvt = null;
Log.i(TAG, "Intent recieved: " + intent.getAction());
if (intent.getAction() == 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) {
flag = 1;
sender = messages[0].getOriginatingAddress();
body = messages[0].getMessageBody();
Log.i(TAG,
"Message recieved: " + messages[0].getMessageBody());
Toast.makeText(context, messages[0].getMessageBody(),
Toast.LENGTH_SHORT).show();
}
}
}
}
}
Please help, I kind of have the feeling that it has something to do with ICS. I read it somewhere while googling... Thanks.
i'm not sure about this solution but override onPause and onResume methods.

Categories

Resources