How to receive incoming SMS on Android KitKat or above? - android

I know from KitKat onward we need to make out app as default SMS app to be able to modify the SMS database.
But I don't want that. I only need to get notification of incoming SMS and display as toasts, and also send SMS.
Do I need to make my app default for that?
Edit 1:
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rtrgroup.sms_to_neta_server"
android:installLocation="preferExternal">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"/>
<activity android:name=".MainScreen" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>
<service android:name=".MainService"
android:exported="true">
</service>
<receiver android:name=".MainReceiver"
android:exported="true"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my BroadcastReceiver:
package com.rtrgroup.sms_to_neta_server;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.widget.Toast;
import android.telephony.*;
public class MainReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage;
if (Build.VERSION.SDK_INT >= 19) { //For ver. >= KitKat
SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
currentMessage = msgs[0];
} else {
Object pdus[] = (Object[]) bundle.get("pdus");
currentMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
}
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String messageB1 = currentMessage.getDisplayMessageBody();
String msg = currentMessage.getMessageBody();
String address = currentMessage.getOriginatingAddress();
Toast.makeText(context, senderNum + ": " + msg, Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
}
}

You don't need to be the default SMS app to send SMS. SmsManager.sendTextMessage() still works as long as you have the android.permission.SEND_SMS permission.
To receive SMSs, you can monitor the android.provider.Telephony.SMS_RECEIVED broadcast, with android.permission.RECEIVE_SMS permission.
Ref. http://android-developers.blogspot.fi/2013/10/getting-your-sms-apps-ready-for-kitkat.html

Related

Android SMS Broadcast Receiver not working

I' m trying to build an app that receives notifications of incoming sms. I need help as incoming SMS's are not detected on any device. I am using the following code that I got from here:
http://karanbalkar.com/2014/09/display-incoming-sms-messages-in-android/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class MySMSApp extends BroadcastReceiver {
public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(ACTION)){
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 strMessageFrom = message.getDisplayOriginatingAddress();
String strMessageBody = message.getDisplayMessageBody();
Toast.makeText(context, "SMS Message received from:" +strMessageFrom, Toast.LENGTH_LONG).show();
Toast.makeText(context, "SMS Message content" +strMessageBody, Toast.LENGTH_LONG).show();
}
}
}
}
And the manifest reads as follows. I have setup the intent receiver to trigger the class above but it seems not to do so. I'm unable to telnet onto the emulator to spoof an incoming sms and so I'm blind as to what's going on.
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<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:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name=".SMSBroadcastReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>
</activity>
</application>
In AndroidManifest.xml you use SMSBroadcastReceiver, but your BroadcastReceiver class name is MySMSApp. Also your <receiver> element is inside the <activity> element.
Try to use this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MySMSApp">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</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 ???

BroadcastReceiver onReceive event doesn't fire

I'm trying to listen for received SMS messages, but onReceive method is never called.
The program is built for 1.6 version of android, and it runs without errors with 2.3.7 version, but the event I said before doesn't fire.
Any tip?
// SMSListener CODE
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSListener extends BroadcastReceiver {
protected String smsBody = "";
public static final String SMS_EXTRA_NAME = "pdus";
protected SQLEventDataSource events;
#Override
public void onReceive(Context context, Intent intent) {
// Get SMS map from Intent
Bundle extras = intent.getExtras();
Toast.makeText( context, "O evento onReceive foi lançado", Toast.LENGTH_SHORT ).show();
String messages = "";
if ( extras != null )
{
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );
for ( int i = 0; i < smsExtra.length; ++i )
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
// check if it's a configuration message
if (body.substring(0, 5) == "SMED#") {
String[] configs = body.split("#");
events.addEventConfiguration(configs[1], configs[2], configs[3]);
}
}
// Display SMS message
Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
}
}
private void setSmsBody(String smsBody) {
this.smsBody = smsBody;
}
public String getSMSBody() {
// TODO Auto-generated method stub
return this.smsBody;
}
}
// AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pt.ipbeja.estig.smed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="1" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.provider.Telephony.SMS_RECEIVED" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".SMEDGPSActivity"
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=".SMSListener" android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Assuming you literally pasted your manifest, it contains a typo in the required permissions. It currently incorrectly lists the action filter string (android.provider.Telephony.SMS_RECEIVED) as opposed to the actual permission:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

Call function in service from broadcastReceiver

I have been struggling with the problem.
I have a Broadcast receiver that is suppose to receive SMS' and it does that, but I want to use a method/function in my Service, the intent is to make it a foreground service eventually, that never shuts down.
The Broadcastreceiver looks like this:
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
{
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
}
//want to send msgs[] to SmsService
}
}
}
In the manifest for making it receive I have added:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BROADCAST_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<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>
<service android:name="SmsService"></service>
</application>
Intent myIntent = new Intent(context, SmsService.class);
myIntent.putExtra("msg", msgs);
context.startService(myIntent);

How to block an incoming message in android?

I am trying to develop an app in android which blocks an incoming sms. I have set the priority but its not blocking the incoming sms. I have used this.abortBroadcast() also but no result.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
#SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
this.abortBroadcast();
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
}
}
and the manifest file is like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="BVB.EDU"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SMS"
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>
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
Here's what I use for blocking incoming texts.
SmsReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class BroadCastReceiver extends BroadcastReceiver
{
/** Called when the activity is first created. */
private static final String ACTION = "android.provider.Telephony.SEND_SMS";
public static int MSG_TPE=0;
public void onReceive(Context context, Intent intent)
{
String MSG_TYPE=intent.getAction();
if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED"))
{
// Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
// toast.show();
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]);
}
// show first message
Toast toast = Toast.makeText(context,"BLOCKED Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
else if(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS"))
{
Toast toast = Toast.makeText(context,"SMS SENT: "+MSG_TYPE , Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
else
{
Toast toast = Toast.makeText(context,"SIN ELSE: "+MSG_TYPE , Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for(int i=0;i<8;i++)
{
System.out.println("Blocking SMS **********************");
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="APP.PACKAGE.NAMEHERE"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true" />
<uses-feature android:name="android.hardware.telephony" />
<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" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".APPACTIVITYHERE"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden" >
<service android:name=".MyService" android:enabled="true"/>
<receiver android:name="SmsReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
<service android:name=".MyServiceSentReceived" android:enabled="true"/>
<receiver android:name="SmsReceiver">
<intent-filter android:priority="2147483645">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
The main thing to take away from the manifest is the service block, receiver block, and the permissions.
Add abortBroadcast(); in the if(bundle!=null){} block. that should stop it going to other apps. And I noticed that your Broadcast Receiver's name is SmsReceiver, but in Manifest, you gave it ".SMSReceiver" (case sensitive).
Problem is there in your manifest, you're closing <application> tag before the receiver tag and it's wrong. All components should be inside an application tag.
Your class name is SmsReceiver, and in manifest you declared as SMSReceiver,
so you won't get the broadcast at all for your receiver.
Use have to change your class name in manifest like below
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
And in your receiver, it's better to check the intent object for whether you got the message or not and then you can abort it.
But be careful it will abort all messages. If you want abort messages depending on some particular string you can do some manipulation on message what you got and then you can abort it.
If you want to abort all messages you can directly call abortBroadcast() before doing any manipulation on that message.
Bundle extras = intent.getExtras();
if ( extras != null )
{
// do you manipulation on String then if you can abort.
if(somecondition){
abortBroadcast();
}
}
Here is my manifest which working fine, once compare with your code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application android:icon="#drawable/icon" android:enabled="true" android:label="#string/app_name">
<service android:name="com.mypackage.service.MyService" android:exported="true">
</service>
<receiver android:name="com.mypackage.receiver.MyReceiver">
<intent-filter android:priority="100"><action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter>
</receiver>
</application>
</manifest>
and the java code
public void onReceive( Context context, Intent intent )
{
if(intent != null){
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if ( extras != null ){
//read sms
}
}
}
}
#sankar
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="BVB.EDU"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SMS"
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=".SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
Because of the security policy in Android 4.4 and up
to block the incoming SMS-messages it is required to give to the
application the permissions of "Default SMS-application"

Categories

Resources