sms receiver from broadcast receiver - android

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.

Related

Displaying SMS message once received in Android

I am trying to write a simple android program that listens to SMS messages, and once a message is received, it displays it in a toast.
Below is my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zaidalmahmoud.expenseless">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<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>
</application>
</manifest>
Below is my SMS Listener:
public class SmsListener extends BroadcastReceiver {
private String msgBody;
private SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Toast.makeText(context,"message received:",Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
}
Toast.makeText(context,"message is:"+msgBody,Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.d("Exception caught",e.getMessage());
}
}
}
}
}
and below is my activity:
public class MainActivity extends AppCompatActivity{
SmsListener smsListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smsListener= new SmsListener();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsListener, intentFilter);
}
#Override
public void onDestroy()
{
super.onDestroy();
// Unregister the SMS receiver
unregisterReceiver(smsListener);
}
}
Surprisingly, I am getting no output at all (no Toast message) once the SMS is received. Can someone help? Thank you.
You are not declaring your receiver in Manifest file, because of it will not listen anything.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zaidalmahmoud.expenseless">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<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=".SmsListener"/>
</application>

SMS Broadcast Receiver onReceive() method not being called

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>

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 ???

SMS Broadcast Receiver works in debug only with emulator

I have an app that tries to catch SMS messsages from a specific sender and then starts a new activity. When running in Eclipse using Debug As and using emulator my BroadcastReceiver it works great, it hits the breakpoints as expected. When I use Run As in Eclipse to launch in the emulator the SMS messages never get caught. It appears the BroadcastReceiver is never called based on the lack of Log outputs. The SMSes also do not get caught when running on my phone. Any ideas why it works one way but not another?
I have very few apps on my phone and the only one I know of that catches text messages is the built-in messenger one.
<receiver android:name=".sms.ConfirmationResponder">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
...
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
The receiver:
public class ConfirmationResponder extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("SMS", "############################ Confirmation being read");
// More stuff after this but I don't even get the log message
}
}
Here is my program:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SMSBroadcastReceiver 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) {
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) {
Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
}
}
}
}
}
And the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Technicaljar.SMSBroadcastReceiver"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true" >
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
Hope it Helps
I think it works but eclipse is not attached to device when "Run as" mode and do not show logcat info.

Categories

Resources