BroadcastReceiver automatically reads SMS in Android - android

I used BroadcastReceiver to show received sms's content in Toast. It's working fine. Shows Content in Toast. But it also Shows the message in Dialog box like thing.
And also the message goes to read state. Any way to avoid this.
My BroadcastReceiver is
public class Receiver 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, "From : " +strMessageFrom+"\nBody : "+strMessageBody, Toast.LENGTH_LONG).show();
}
}
}
}
AndroidManifest.xml is
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/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="com.realtech.sms_db.Receiver"
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>

As I understood, the issue is 'why it is going in read state'?. There is nothing wrong with the implementation. The incoming message is getting marked as read is not because of the coed, it is because of device notification settings "auto preview". That is why you are able to see the window. To disable "auto preview" follow the steps :-
Step 1: Go to messages folder
Step 2: Select Menu (3 dots).
Step 3: Select "Settings"
Step 4: Scroll down to " Notification Settings"
Step 5: Select " Notification Settings"
Step 6: Uncheck the box near to "Preview message".
Hope it helps you.

show AlertDialog.Builder and use custom layout. set alert type TYPE_SYSTEM_ALERT
sample
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setView(yourCustomView);
AlertDialog dialog=builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();

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>

Android Sms Broadcast Receiver not firing despite priority

I have read many similar questions that usually results in an answer about priority. The reason I don't think this is true in my case is that other SMS readers on my phone (like automation apps) receive the broadcasts just fine. I would like to post the process of what I'm doing currently and make triple sure that I'm not doing something wrong in my code that would cause this to fail. Thanks for any tips you can give!
Note: I've tried with the highest integer priority, priority 99, 100, 0, or none set at all. They all don't work.
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hennessylabs.appname" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.hennessylabs.drivercompanion.ProcessTextMessage" android:exported="true">
<intent-filter android:priority="999" android:permission="android.permission.BROADCAST_SMS">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
BroadcastReceiver:
package com.hennessylabs.appname;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
/**
* Created by kyleC on 11/15/2015.
*/
public class ProcessTextMessage extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
final SmsManager sms = SmsManager.getDefault();
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Entered onReceive", Toast.LENGTH_LONG).show();
// 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);
}
}
}
Expected Result:
The expected result is that when an SMS arrives, the screen would first show a Toast that it entered the OnReceive method. Then it would log and Toast the contents of the SMS.
Actual Result:
Nothing from the expected result happens. In fact even while connected to USB and in debug mode it never seems to enter that class at all. So maybe I have my manifest set up wrong?
Provided everything else is correct, it looks like you're just missing the RECEIVE_SMS permission.
<uses-permission android:name="android.permission.RECEIVE_SMS" />
You are missing some things from the manifest declaration of your receiver. You must declare exported=true and you must require the BROADCAST_SMS permission. See working example:
<receiver android:name=".sms.IncomingSmsReceiver" android:exported="true">
<intent-filter android:priority="999" android:permission="android.permission.BROADCAST_SMS">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
So what is the version of the device your app is running on?
Begins in Android 4.4, only the default sms app can receive the SMS_DELIVER_ACTION broadcast.
more information: http://android-developers.blogspot.sg/2013/10/getting-your-sms-apps-ready-for-kitkat.html

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>

Android Broadcast receiver not receving income SMS

I have written a SMS receiver for catching income SMS , everything looks fine but it doesn't
works and no SMS income received by the receiver . this is the codes and manifest content.
As I remember I had same app has written in android 2.3 working fine but this code is running in android 4.x which is not functioning properly. what is the problem ? Is it depends on security issues of android 4.x ?
Manifest:
<receiver android:name="SmsReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Java Code:
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Logger.i("INCOMMING SMS...");
if (action == 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) {
String sendr = messages[0].getOriginatingAddress();
Logger.i(sendr);
}
}
}
}
}
I had the same problem and I've fixed it by adding two more actions to the manifest registered receiver so it will look like this:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.provider.telephony.SMS_RECEIVED"/>
<action android:name="com.your.package.android.action.broadcast"/>
</intent-filter>
</receiver>
also add permission :
<uses-permission android:name="android.permission.RECEIVE_SMS" />
And for comparsion of Strings do not use equals operator, but equals method instead.(Note. equalsIgnoreCase() should be better for you.)
so it will be like:
if(SMS_RECEIVED.equalsIgnoreCase(action))
{
//continue
}
Hope it helps.
I had the same problem as you and after much research on the net, I found the problem. Your application is not started and the Emulator Control doesn't send the option --include-stopped-packages as the adb command can do it. It is a feature introduced with Android 3.x.
So, your receiver never receives the broadcast because your application is not started.
To start it first, open a console from your system and type the following command:
*adb -e shell am broadcast -a android.provider.Telephony.SMS_RECEIVED --include-stopped-packages*
Your BroadcastReceveiver receive an empty SMS.
After this command, your process is displayed in the Devices view, and you can send SMS from your emulator control.

Which android template should I use to create an android background activity?

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.

Categories

Resources