I am writing an sms receiver android code but it is not working.
It compiles will but when I get an sms it did not displayed as supposed to be when using Toast.
This is my code:
package com.example.homecontrolingbysms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messageReceived = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
messageReceived +="From "+ msgs[i].getOriginatingAddress();
messageReceived+=" : ";
messageReceived += msgs[i].getMessageBody().toString();
messageReceived += "\n";
}
//---display the new SMS message---
Toast.makeText(context, messageReceived, Toast.LENGTH_SHORT).show();
}
}
}
I also posted manifest to make sure all important parts of code are available
manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.homecontrolingbysms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.homecontrolingbysms.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>
<activity
android:name="com.example.homecontrolingbysms.Door"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.homecontrolingbysms.Window"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.homecontrolingbysms.Light"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
Register smsreceiver by adding below code in menifest file
<receiver android:name="com.shreymalhotra.smsreceiver.SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
This links may help you.
Receiving SMS on Android App
http://shreymalhotra.me/blog/tutorial/receive-sms-using-android-broadcastreceiver-inside-an-activity/
Please check it.
if you want to receive some broadcasts , blew things must be done.
1. In the manifest.xml ,you must declare your receiver with the intent filter
exp:
2. if the broadcast that you want to receive is ordered, you must make sure your priority.
android:priority="1000"
I think your manifest looks okay; the problem is with the line:
Mistake is in this part I think can you change this
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messageReceived = "";
if (bundle != null)
to something like this
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
intent.getAction().equals(ACTION) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
(you need to do an object comparison on the action string, or just do no comparison), in your manifest file you have misspelled SMS_RECEIVED.
Related
I am trying To build my first application which will send ans SMS message.
everything looks OK:
1. SMS application opens
2. The URI is inserted
3. Text message is typed
Only the last bit of pressing the "SEND" button is not performed.
I am using the code demonstrated here on youtube:
which includes this AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.ac.ruppin.reco.www.sendsmsyoutube">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.SENR_RESPONSE_VIA_MESSAGE"/>
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and this MainActivity.java
package il.ac.ruppin.reco.www.sendsmsyoutube;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri uri = Uri.parse("smsto:+972528524520");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","Message from my new application");
startActivity(intent);
}
}
Thanks for your help
The correct URI format for SMS is sms: (and not smsto:)
String number = "+972528524520"
Uri uri = Uri.parse("sms:" + number);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Message from my new application");
startActivity(intent);
This starts the default activity for sending an SMS message.
You can try it with the SmsManager Class
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null)
where,
phoneNo = send To , sms = your message to be passed
Ok according to Hardik Vegad answer I used the SmsManager.
What I had to do is to allow the application to send sms.
Setting>apps>MyAp>permissions
Thanks for your help
code
package com.yasharkhosravi.applicationhack;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import java.util.Objects;
public class SMSReceive extends BroadcastReceiver {
public SMSReceive() {
}
#Override
public void onReceive(Context context, Intent intent) {
SmsMessage[] msgs = null ;
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Objects[] pdus = (Objects[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
String s = "";
for (int i = 0 ; i<msgs.length ; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if(i==0) {
s += msgs[i].getOriginatingAddress().toString();
s += "\n";
}
s +=msgs[i].getMessageBody().toString();
}
}
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yasharkhosravi.applicationhack" >
<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=".SMSReceive"
android:enabled="true"
android:exported="true" >
</receiver>
</application>
</manifest>
I have an error in createFormatPdu()
red line is byte
my error is
Error inconvertible types; cannot cast 'java.util.Objects' to 'byte[]'
This error is in createFromPdu()
My SDK version is 8
use object insteed of : objects
Object[] pdus = (Object[]) myBundle.get("pdus");
You could try the following:
Objects[] pdus = (Objects[]) bundle.get("pdus"); (use Object replace Objects)
I've searched a lot on web and on SO I couldn't find an answer to my case or a straightforward tutorial, I was following this my problem is that the constructor doesn't get call nor onReceive().
public class Smsreceiver extends BroadcastReceiver
{
public Smsreceiver()
{
Log.v("constructing", "constructing");
}
#Override
public void onReceive(Context context, Intent intent)
{
Log.v("received", "received");
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}
}
and manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dwaik.testreceivesms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver
android:name=".SmsReceiver"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
I'm building against api-level 15 (4.0.3) and running on xperia neo v (4.0.3), what am I missing? or shall I do anything special beside run to get my service to run?
EDIT
I can't find my service between running apps menu, though it does exist on installed apps menu
Register your receiver in your main activity:
String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
SmsReceiver smsReceiver = new SmsReceiver();
registerReceiver(smsReceiver, new IntentFilter(SMS_RECEIVED));
Remember to unregister it at the end.
It works if you put the correct name of your class in the manifest.xml.
In fact you called: "Smsreceiver" your java class and you try to register a service in the manifest that is called SmsReceiver.
Change the name of the class or into the manifest.
<receiver android:name="com.example.package_name.HERE YOUR SMS RECEIVER">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I try to receive an sms in my app and I think the problem is in my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amin_amini.smstestmobi"
android:versionCode="1"
android:versionName="1.0.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:name=".SMS"
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=".SmsReceiver"
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>
</manifest>
My SmsReceive.java is like this
package com.amin_amini.smstestmobi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
Toast.makeText(context, "Hey", Toast.LENGTH_SHORT).show();
}
}
I'm sure my problem is in manifest.xml because the
Toast.makeText(context, "Hey", Toast.LENGTH_SHORT).show();
don't show anything.
what should I do ?
edit:
As our friends said bellow I used
Log.i("Hey","Hey" );
in my function but nothing will be shown in LogCat
So I'm defenetly sure that my problem is in me AndroidManifest.xml
note: I'm using android 4.0.3 on HTC Sensation
Change Your receiver code to that, it will help you, if still its not working then may be you have installed another SMS apps which will have higher priority and will abord the broadcast services.
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="PACKAGE_NAME.android.action.broadcast"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
Yes your code is working fine here.
this is my androidmanifest
<receiver android:name=".SmsReceiver" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
and i have added these permission
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
or download this code.. i have made this . it recieve sms and shows in dialog box.
i followed a tutorial to receive SMS on my application and read it to pass the SMSbody to Toast.
that is the Receiver class.
public class SmsReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Bundle bundle= intent.getExtras();
SmsMessage[] msgs= null;
String str="";
if(bundle != null ){
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]);
str+= msgs[i].getMessageBody();
}
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}
}
the manifest file
<receiver android:name="com.msoft.masrooq.SmsReciever">
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECIEVED"></action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
the app starts fine but it doesn't response to receiving sms
it doesn't do anything.
Here is my implementation of receiving sms messages. Sms message may be broken into many, notice how it is treated. Also check the android:priority attribute.
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
// get sms objects
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus.length == 0) {
return;
}
// large message might be broken into many
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(messages[i].getMessageBody());
}
String sender = messages[0].getOriginatingAddress();
String message = sb.toString();
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
// prevent any other broadcast receivers from receiving broadcast
// abortBroadcast();
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsreceiver"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity
android:name=".SmsLoggerActivity"
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.example.smsreceiver.SmsReceiver" android:enabled="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Few notes:
If you declare your receiver in xml than system can use your receiver regardless of your application was ever launched.
Since Android 1.6 notifications about received sms messages are delivered as ordered broadcasts, you can use android:priority attribute of <intent-filter> to tell the system send the sms first to your application (you can also call abortBroadcast() so other applications won't receive the sms, e.g. the native sms app). Don't forget broadcast receiver has about 10 seconds for executing its operation, otherwise it can be prematurely terminated before finishing its job.
Note: That on some devices your code wont work without android:priority="100" in intent filter:
<application
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
...
<receiver
android:name=".SMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
...
</application>
And here Java code :
public class SMSReceiver extends BroadcastReceiver {
public static final String ACTION ="android.provider.Telephony.SMS_RECEIVED";
private static final String SMS_SENDER="123456789";
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction() != null &&
ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
// SMS Sender, example: 123456789
String sms_from = messages[0].getDisplayOriginatingAddress();
//Lets check if SMS sender is 123456789
if (sms_from.equalsIgnoreCase(SMS_SENDER)) {
StringBuilder bodyText = new StringBuilder();
// If SMS has several parts, lets combine it :)
for (int i = 0; i < messages.length; i++) {
bodyText.append(messages[i].getMessageBody());
}
//SMS Body
String body = bodyText.toString();
// Lets get SMS Code
String code = body.replaceAll("[^0-9]", "");
}
}
}
Broadcasts are case-sensitive. Use android.provider.Telephony.SMS_RECEIVED not android.provider.telephony.SMS_RECEIVED.
Also, I also have a category set, but I'm not sure it's mandatory:
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>