Avoid Receive SMS in inbox ..android - android

I want to send a SMS to a SMS center and receive a result sms from it and show that result in an Edittext... my main activity is LoginActivity.. When I run this program, after click on button, The message sent to SMS center successfully. But result SMS of center didn't show in my program , But shown in my Inbox! I want to to avoid recieve mesesage in Inbox and I want to recieve this message in my app!
My codes in LoginActivity is:
private void notifyIncoming(String message)
{
messageRes.setText(message);
}
private BroadcastReceiver receiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
Object[] pdus = (Object[])intent.getExtras().get("pdus");
StringBuilder message = new StringBuilder();
SmsMessage messageSegment;
for (int i=0;i < pdus.length;i++)
{
messageSegment = SmsMessage.createFromPdu((byte[]) pdus[i]);
message.append(messageSegment.getDisplayMessageBody());
}
notifyIncoming(message.toString());
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
setContentView(R.layout.activity_login);
// Set up the login form.
messageRes = (EditText) findViewById(R.id.code3_txt);
findViewById(R.id.sign_in_button).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
#SuppressWarnings("deprecation")
String phoneNumber = "0123456789";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "Hi", null, null);
registerReceiver(receiver, mIntentFilter);
}
});
}
and my Manifest.XML is:
<manifest.................. >
<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-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
............ >
<activity
android:name="com.example.accelometer.LoginActivity"
............... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.accelometer.LoginActivity" >
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Update:
change on onCreate Func.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, mIntentFilter);
setContentView(R.layout.activity_login);
// Set up the login form.
messageRes = (EditText) findViewById(R.id.code3_txt);
findViewById(R.id.sign_in_button).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
#SuppressWarnings("deprecation")
String phoneNumber = "0123456789";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "Hi", null, null);
}
});
}
Update 2
I delete <receiver> block from manifest..and I can receive SMS in my app, But I want to avoid show this SMS in inbox or other SMS apps like hangout and other

For what you are trying to do, there are many solutions pre-KitKat. This is probably the best example:
Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4)
With KitKat+, I posted here about one solution that requires an app:
How to prevent SMS going to inbox in Android Kitkat

Related

Receive text, make a phone call and run in background indefinitely

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>

Broadcast Receiver for RECEIVE_SMS is not working when declared in manifest(statically)

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.

Android - Sending an SMS before Shutdown

I'm trying to write an app that will send an sms on phone shutdown intent.
I want it so that, the phone will shutdown only after the sms is sent. If sending fails, the app should retry sending.
I'm getting the error
java.lang.RuntimeException: Error receiving broadcast Intent {act=android.intent.action.ACTION_SHUTDOWN flg=0x10} in com.novytes.TestSOS.ShutdownReceiver#417008b0
at com.novytes.TestSOS.SmsHandler.sendSMS (SmsHandler.java:33)
at com.novytes.TestSOS.ShutdownReceiver$1.run (ShutdownReceiver.java:25)
at com.novytes.TestSOS.ShutdownReceiver.onReceive (ShutdownReceiver.java:22)
I have the following files.
MyActivity - Main activity, just starts the MainService service
MainService- A service that registers a broadcast receiver
ShutdownReceiver - BroadcastReceiver that tries to send an SMS using another class
SMSHandler - The class used to send an SMS
MainService.java
static final String TAG = "SOS_APP";
IntentFilter filter;
BroadcastReceiver mReceiver;
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service created");
filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
mReceiver = new ShutdownReceiver();
registerReceiver(mReceiver, filter);
}
ShutdownReceiver.java
SmsHandler smsHandler;
private boolean sent=false;
#Override
public void onReceive(Context context, Intent intent) {
smsHandler = new SmsHandler();
if (intent.getAction().equals(Intent.ACTION_SHUTDOWN))
{
new Thread(){
public void run(){
if(sent!=true){
smsHandler.sendSMS(ShutdownReceiver.this);
}else{
return;
}
try {
Log.v("SOS_APP","delaying shutdown");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.run();
}
if(intent.getAction().equals(Activity.RESULT_OK)){
sent = true;
}
}
SMSHandler.java
public void sendSMS(ShutdownReceiver mReceiver)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 0);
registerReceiver(mReceiver , new IntentFilter(SENT));
smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "Message", sentPI, null);
}
And finally, here is my android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.novytes.TestSOS"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="16"/>
<uses-permission android:name="android.permission.ACTION_SHUTDOWN"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".MainService">
</service>
<receiver android:name=".ShutdownReceiver"
android:permission="android.permission.ACTION_SHUTDOWN" >
<intent-filter>
<action android:name="android.permission.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
I think you are getting getApplicationContext() null in SMSHandler.java.
You have to create Constructor in SMSHandler.java and pass Context in argument.
like:
public class SMSHandler {
Context context;
public SMSHandler(Context context) {
this.context = context;
}
public void sendSMS(ShutdownReceiver mReceiver)
{
String SENT = "SMS_SENT";
/**** Change THIS LINE with passing Context variable *****/
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);
registerReceiver(mReceiver , new IntentFilter(SENT));
smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "Message", sentPI, null);
}
}
May it will be helpful to you.
Add this permission:
<uses-permission android:name="android.permission.DEVICE_POWER" />
Some developers says it is needed

Intercept SMS message

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>

Incoming Message Broadcastreceiver not working

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>

Categories

Resources