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.
Related
My app needs to be able to receive SMS messages. It all works, but I get this lint warning:
BroadcastReceivers that declare an intent-filter for SMS_DELIVER or
SMS_RECEIVED must ensure that the caller has the BROADCAST_SMS
permission, otherwise it is possible for malicious actors to spoof
intents.
How do I "ensure that the caller has the BROADCAST_SMS permission"?
In my manifest I have:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application ...>
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
My code:
public class SmsReceiver extends BroadcastReceiver {
public SmsReceiver() {}
#Override
public void onReceive(final Context context, final Intent intent) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
final SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
// use currentMessage
}
}
}
}
Add android:permission="android.permission.BROADCAST_SMS" to the opening <receiver> tag. For example:
<receiver
android:name=".SmsReceiver"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
This android:permission attribute on a <receiver> specifies which permission the sender of the broadcast must hold in order for it to be able to broadcast to your <receiver>. It's a security measure; in this case, so you can be relatively certain that it is the system sending the SMS_RECEIVED broadcast. It's not strictly required, but lint will complain if it's not there, obviously.
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.
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
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.
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!