So I have an application, which should make a phone call when receiving a specific message from a specific phone number.
The broadcast receiver works on a Samsung Galaxy Tab2, on Android 4.0.3, but the messages are not received any more by Go SMS Pro, if no phone call has to be made.
When running the application on Samsung Galaxy S2 (Android 4.0.3), the broadcast receiver seems to not intercept at all the messages and Go SMS Pro works correctly.
I have tried to use "abortBroadcast" and "clearAbortBroadcast", but they had no effect.
Here is the code for my broadcast receiver:
public class SMSReceiver extends BroadcastReceiver{
/** The Action fired by the Android-System when a SMS was received.
* We are using the Default Package-Visibility */
private static final String SMS_EXTRA_NAME = "pdus";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Message received", Toast.LENGTH_LONG).show();
StringBuilder sb = new StringBuilder();
sb.append("");
if(intent.getAction().equals(Consts.ACTION)) {
Bundle bundle = intent.getExtras();
if(bundle != null)
{
//Get received SMS array
Object[] smsExtra = (Object[]) bundle.get(SMS_EXTRA_NAME);
for ( int i=0; i < smsExtra.length; i++)
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
if(address.equals(Consts.COPCI_NUMBER)) {
sb.append(body);
}
}
}
if(sb.toString().equals(Consts.TRIGGER_MESSAGE))
{
Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel: " + Consts.POLI_NUMBER));
dial.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(dial);
}
else {
}
}
}
}
And this is the AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Parcare"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="com.example.Parcare2.SMSReceiver" android:priority="100">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
Related
Situation: My apartment building has a closed parking lot with a gate that opens upon calling a specific number (only some numbers can open the gate, including mine).
My request: I am trying to make an application (for my own phone), which would read an incoming SMS with a specific keyword, and then call the number, which opens the gate. Furthermore, this app should work while it is not in foreground (either by running in background or maybe intent-filters?).
Is it at all possible, and how do I achieve this?
My current "solution" is provided below, but it does not work (keep in mind I'm a complete beginner).
Main activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmsListener listener = new SmsListener();
String msgBody = listener.getMsgBody();
if (msgBody.contains("keyword")) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("number"));
startActivity(callIntent);
}
}
}
SmsListener class:
public class SmsListener extends BroadcastReceiver{
private SharedPreferences preferences;
String msgBody = "";
public String getMsgBody(){
return msgBody;
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
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, msgBody, Toast.LENGTH_LONG).show();
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
}
I have no idea if the above code is at least partially correct, any help would be appreciated.
When I send a text from another phone, the toast I wrote does not appear, meaning that the code doesn't even run probably.
My manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BROADCAST_SMS" /> // Error saying that only system apps can use this permission
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".SmsListener">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I am trying to detect SMS received and read it via texttospeech.
when i declare the Broadcast Receiver in manifest it doesn't work. But it works when done dynamically in an activity.
I am aware that some Broadcast actions cant be caught in a receiver when declared in manifest and requires an activity(as mentioned here), but have seen people using RECEIVE_SMS in manifest as in here.
I don't know what I am doing wrong. Any help would be greatly appreciated!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bulsy.smstalk1">
<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.READ_CONTACTS" />
<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="com.bulsy.smstalk1.SmsListener"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS"
android:exported="true">
<intent-filter android:priority="2147483647">//this doesnt work
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
SmsListener.java
public class SmsListener extends BroadcastReceiver{
private SharedPreferences preferences;
TextToSpeech textToSpeech;
String msg_from;
public SmsListener()
{
}
#Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
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();
final String msgBody = msgs[i].getMessageBody();
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
String fromName = getContactName(context,msg_from);
fromName = fromName==null? msg_from:fromName;
textToSpeech.speak("You have a text message from " + fromName + ". Content: " + msgBody , TextToSpeech.QUEUE_FLUSH, null);
}
}
}
);
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmsListener smsListener = new SmsListener();//Dynamically setting the receiver. this works.
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(smsListener,filter);
}
}
The root of the problem here is the lifetime of a manifest-registered Receiver instance. An instance of such a Receiver will only be alive until the onReceive() method completes. The TextToSpeech object will not be ready before the Receiver dies, and without any other indication of the Receiver working, it appears as though the Receiver has just failed.
The solution is to move the TextToSpeech functionality to a Service you can run from the Receiver, and pass the necessary info as extras on the Intent used to start it.
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>
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 your
IncomingSMSReceiver 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.
Make sure that the receiver starts after phone reboots.
Add <action android:name="android.intent.action.BOOT_COMPLETED" /> in intent-filter
I used this in my project, find this from Android SMS Receive Listener
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import java.util.ArrayList;
/**
* ##author Chathura Wijesinghe <cdanasiri#gmail.com>
*
* <receiver android:name=".SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
*/
public class SMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static ArrayList<SMSReceivedListner> smsListner = new ArrayList<SMSReceivedListner>();
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
final Bundle extras = intent.getExtras();
if (action.equals(SMSReceiver.SMS_RECEIVED)) {
final boolean smsValid = extras != null;
if (smsValid) {
//Create SMSMessages from PDUs in the Bundle
final Object[] pdus = (Object[])extras.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
//Assemble
final ArrayList<Long> vibrations = new ArrayList<Long>();
for (SmsMessage message : messages) {
for (SMSReceivedListner smsReceivedListner : smsListner )
smsReceivedListner.message(message);
}
}
}
}
public static void addSMSListner(SMSReceivedListner listner){
smsListner.add(listner);
}
public static void removeSMSListner(SMSReceivedListner listner){
smsListner.remove(listner);
}
public interface SMSReceivedListner{
public void message(SmsMessage message);
}
}
You have to remove the android:permission="android.permission.BROADCAST_SMS" from the receiver declaration. No BOOT_COMPLETED or other permission are required.
explicitly set exported to true in your manifest as an element for the Receiver
android:exported="true"
and make sure both of your application element and receiver element are enabled
android:enabled="true"
Broadcast code seems to be okay and Make sure that you are using any other SMS apps in your device(May this SMS Broadcast might interrupting your apps Broadcast) . If you are not using no other SMS apps then it should work otherwise un-install that SMS app and try once.
and also check you have added the following permissions in your AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
Hope this will work.
Ideally it should have worked.Is weird that its not working.
Remove android:permission="android.permission.BROADCAST_SMS" from the broadcast receiver.It is not required.
Try increasing the priority android:priority="2147483647" to maximum.It might be some other app is consuming the event or try the below solution.
Add <action android:name="android.intent.action.BOOT_COMPLETED" /> in your intent filter and see if it works.
Something like this:
<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>
</receiver>
and add permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Hi everyone .
i send message to one device and receive this sms in another device's application.I used Broascast receiver to listen sms body or number .I have taken all permission in manifest but my reciver not call.I have used many thing in 2 days related to manifest like android:priority,android:enabled="true", android:exporte but stiil receiver not working.
/* final SmsManager sms = SmsManager.getDefault();*/
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "in receiver", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
Log.e("out", "out");
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
Toast.makeText(context, "broad cast reciver", Toast.LENGTH_SHORT).show();
if (bundle != null){
//---retrieve the SMS message received---
try{
Log.e("in", "in");
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();
String msgBody = msgs[i].getMessageBody();
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
manifest....
<
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application android:icon="#drawable/ic_launcher">
<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=".SmsListener" android:enabled="true" android:exported="true" android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
Have your SmsListener extend BroadcastReceiver:
public class SmsListener extends BroadcastReceiver {
public SmsListener() {
}
#Override
public void onReceive(Context context, Intent intent) {
//you code here
}
}
And now you can register your receiver in your main activity:
String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
SmsListener smsListener = new SmsListener();
registerReceiver(smsListener, new IntentFilter(SMS_RECEIVED));
Remember to unregister it at the end.
When i uninstalled other application in my device it have also same functionality like my application for example hello application (check out at play store).Then i installed my application it work great.I think another application set priority or other thing in manifest for new incoming sms.So due to this reason our application broadcast receiver not called
Changing the Priority to 1 made it work for me. It defaulted to 1000.
I also had to change the name from .SmsListener to .MainActivity$SmsListener but that would probably depend on where you defined the class (in my case it was within the MainActivity).
<receiver android:name=".MainActivity$SmsListener"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Sorry, but I am new to android development. I am trying to create an app that will run in the background and check for incoming SMS messages and show a pop up with the content of the message. I don't know which android template to use for such an app. I'm using eclipse juno.
You can look at Services to do background stuff and issue notifications.
http://developer.android.com/reference/android/app/Service.html
I'd suggest you to use a BroadcastReceiver
public void onReceive(Context ctx, Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null)
return ;
Object[] dataArr = (Object[]) extras.get("pdus");
for (Object data: dataArr) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) data);
String body = sms.getMessageBody();
String sender = sms.getOriginatingAddress();
}
}
and register your broadcastReceiver in your manifest
<receiver android:name=".sms.SMSReceiver" android:enabled="true" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
Hope this help you.