display android TOAST on receive of sms - android

i am working on an app which shows a toast on sms is received.i want either sms content to be displayed or just an notification that sms has been received.
I am using android studio. and am new to it. please help..

Register a receiver which listens to new message.
Refer these link
Android - SMS Broadcast receiver
and Blog : Incomming SMS Broadcast Receiver - Android Example

Use the BroadcastReceiver to listen to the sms received. Codes are here:
public class SmsReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage msg = null;
if (null != bundle) {
Object[] smsObj = (Object[]) bundle.get("pdus");
for (Object object : smsObj) {
msg = SmsMessage.createFromPdu((byte[]) object);
Date date = new Date(msg.getTimestampMillis());//时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String receiveTime = format.format(date);
Toast.make(context, "number:" + msg.getOriginatingAddress()
+ " body:" + msg.getDisplayMessageBody() + " time:"
+ msg.getTimestampMillis(), 1000).show();
}
}
}
}
Meanwhile, add this in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
...
<receiver android:name="com.dbjtech.acbxt.waiqin.SmsReciver" >
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

If your app is running on an Android version > 3.0, you'll have to include an Activity that the user can start manually after installation to bring the app out of its stopped state, a concept introduced in Android 3.1. The system will not broadcast to stopped apps, so your BroadcastReceiver won't work until the app has been explicitly launched by the user.
Apart from that, your code appears to be correct.

Related

Android Service to receive SMS when app is in background

I'm currently working on an app that processes incoming SMS. As I was searching for a solution I came across different ways including stand-alone BroadcastReceivers and Services which work fine. The goal is to receive the SMS even when app is in background and screen is off (phone locked).
My current problem is: I receive any SMS with a BroadcastReceiver running within a Service. This Service is started in onResume() of my MainActivity and should not be stopped.
When my App is in foreground, all SMS are recognized by the Receiver.
Even when app is in background but my phone is still on, I can receive those messages.
The strange things happen here:
App is in foreground and I turn the screen off -->SMS are recognized.
App is in background and I turn the screen off -->SMS wont be recognized.
This is my Code for Service-class:
public class SmsProcessService extends Service {
SmsReceiver smsReceiver = new SmsReceiver();
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
return START_STICKY;
}
private class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String telnr = "", nachricht = "";
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[]) extras.get("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage smsMessage = getIncomingMessage(pdu, extras);
telnr = smsMessage.getDisplayOriginatingAddress();
nachricht += smsMessage.getDisplayMessageBody();
}
// Here the message content is processed within MainAct
MainAct.instance().processSMS(telnr.replace("+49", "0").replace(" ", ""), nachricht);
}
}
}
private SmsMessage getIncomingMessage(Object object, Bundle bundle) {
SmsMessage smsMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
smsMessage = SmsMessage.createFromPdu((byte[]) object, format);
} else {
smsMessage = SmsMessage.createFromPdu((byte[]) object);
}
return smsMessage;
}
}
}
Inside my MainActivity I declare an Intent:
Intent smsServiceIntent;
which is initialized in onCreate() like this:
smsServiceIntent = new Intent(MainAct.this, SmsProcessService.class);
I start the Service in Activity onResume():
MainAct.this.startService(smsServiceIntent);
My manifest-file has <service android:name=".SmsProcessService" /> to declare the Service and following permissions:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
I really hope, you can give me some support and help to figure out the apps's behavior.
I tried the app on different phones now and I think I have found the source of the problem.
On the other phone (same OS-Version) everything works just fine. I guess that several other apps also receive the SMS-Broadcast and maybe they disrupt each other. I didn't manage to figure out exactly which apps are the worst.
I will now try to localize other applications with similar use and hope that uninstallation or changing their notification settings will help. Have you any guess how those other apps (like "Auto Ring", which I want to use furthermore) could work parallel?
EDIT:
I found the problem causing app: It's the Google-App which also has SMS-permission granted. I deactivated it and everthing works just fine
Greets Steffen
Setting broadcast priority to 2147483647 may solve the problem as mentioned here.
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

Using android device as a remote control

I want to make an aaplication that has the feature of controlling my phone from remotely.Such as on/off GPS through text message from another phone.Getting the location from phone via text message.
Is it possible to on/off gps through text message from another phone?
Definitely yes.
You just need to add in you application a receiver for the Action android.provider.Telephony.SMS_RECEIVED and parse the message text if you recognize ac command in your phone, you execute some code.
Basically you need to do three things:
Update you manifest abd add permission for your application to receive SMS:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Then you have to add, again in the manifest a receiver, that receive the SMS_RECEIVED Action, in a way similar to the following:
<receiver android:name=".SMSReceiver" android:enabled="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
Where android.name is the name of your Receiver class.
And finally you have to implement that class, that extends BroadCastReceiver and has at least the onReceive Method implemented.
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
}
}
For your help, below there an example onReceive code:
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
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 messagestr = message.getMessageBody();
String sender = message.getOriginatingAddress();
Toast.makeText(context, sender + ": " + messagestr, Toast.LENGTH_SHORT).show();
}
}
That code, read the message content and show it on a Toast. You can find a full working example here: https://github.com/inuyasha82/ItalialinuxExample/tree/master/LezioniAndroid
Recieving (handling) SMS-es is possible with Android. Your software should read the SMS, then decide if it contains command and turn off the GPS like normal app would.
This link shows how http://www.apriorit.com/dev-blog/227-handle-sms-on-android

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.

How to handle Service Information in Android

Operator can send Service Information message to phone user. It appears as notification window. My operator for example sends spent and left money after every call. It uses these Service Information for that.
Is there a way to handle that message?
If they are SMS (The case I see with operators around me).
Simple add an intent filter to your manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example">
<uses-permission id="android.permission.RECEIVE_SMS" />
<application>
<receiver class="SMSApp">
<intent-filter>
<action android:value="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
and just extends android.content.IntentReceiver and implement the onIntentReceived method as follows:
public void onReceiveIntent(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
System.out.println("Received SMS from: "+message.getDisplayOriginatingAddress());
System.out.println(message.getDisplayMessageBody());
}
}
}
I think that in the similar way parsing of USSD services is working. Have a look into the class that parses USSD reply: http://codepaste.ru/7545/. I hope this will help you. Good luck!

Categories

Resources