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>
Related
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 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();
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
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>
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>