Unable to find SMS permission in android studio - android

I am unable to find android.provider.Telephony action in android studio for creating an app that can receive SMS. Almost every article including android developer says that I have to include a intent filter action android.provider.Telephony.SMS_RECEIVE in manifest file. But I figured out that this action is no more supported by android studio. Please help me

You need to give the permission to your manifest file,
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Then you should include the intent-filter to your manifest,You should have taken a class that extends BroadcastReceiver write the name of that class as receiver name.In my case it is SMSReceivcer.
<receiver android:name=".SMSReceivcer"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

pls try this
// Add this in manifest
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<receiver android:name=".SMSReciver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
//Create new class
public class SMSReciver extends BroadcastReceiver
{
private Context mContext;
#Override
public void onReceive(Context context, Intent intent)
{
mContext = context;
Bundle myBundle = intent.getExtras();
SmsMessage[] messages = null;
String strMessage = "";
String lMessageBody = "", lMessageFrom = "";
if (myBundle != null)
{
Object[] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
lMessageFrom = messages[i].getOriginatingAddress();
lMessageBody = messages[i].getMessageBody();
}
}
}
}

Related

how to get receiver phone number from sms application? [duplicate]

Is it possible in android to filter the SMS from a specific number in android.And save it in a specific database.and the SMS should not be visible in the inbox...
You have yo do this in 3 step:
read SMS from Specific number you have to look in to this link then after
you have to create Database for store all info about SMS(like id, body, number, time) for that check this link
you have to Delete SMS from INBOX check this link for delete SMS.
add below permission for do all step:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
ya is it possible.
in mainfest file add following code:
<receiver android:name="com.example.Sms_BReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
after that create a class use following code
public class Sms_BReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] messageString = null;
Object[] pdus = (Object[]) bundle.get("pdus");
messageString = new SmsMessage[pdus.length];
for (i = 0; i < messageString.length; i++) {
messageString[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
Phone_no = messageString[0].getOriginatingAddress();
Message_body = messageString[0].getMessageBody();
Time = messageString[0].getTimestampMillis();
CharSequence text = str;
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
if (Phone_no.contains(here check no to filter) {
here delete message form inbox
and save in our own db
} else {
}
}
i hope this code will use full for u

Detecting incoming and outgoing sms?

Is it Good paractice to Detect Incoming and Outgoing Sms through ContentObserver or use BroadCast Receiver for Incoming SMS and ContentObserver for Outgoing Sms??
I had an application about SMS and Call blocker but i never upload it to play store because of after Kitkat ,you cant block sms messages. If you wanna do that just dont. I am sharing the sms part only, you can modify it as you want.
<!-- Android Permissions in manifest -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<!-- Define receiver inside the application tag -->
<receiver android:name="catchPackage.SmsController" android:process=":hascode_process" android:label="#string/sms_receiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_DELIVER"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<!-- Broadcast receiver java code -->
public void onReceive(Context context, Intent intent) {
this.context = context;
try{
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from = null;
String msg_body = null;
if (bundle != null){
try{
boolean delete= false;
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int k=0; k<msgs.length; k++){
msgs[k] = SmsMessage.createFromPdu((byte[])pdus[k]);
msg_from = msgs[k].getOriginatingAddress();
System.out.println(msg_from);
String msgBody = msgs[k].getMessageBody();
msg_body = msgBody;
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}

Is it possible to read incoming sms and reply back only to specific or desire number

Is it possible to read incoming sms and reply back only to specific or desire number. i-e i want to make app that will run in back ground ... whenever i will send sms with my number it will automatically respond back to me with XYZ information
Yes, it's possible. You need to add a BroadcastReceiver in your app that will intercept incoming SMS messages and then send a message if it matches the number you're looking for.
Following source code from:
BroadcastReceiver + SMS_RECEIVED
http://shreymalhotra.me/blog/tutorial/receive-sms-using-android-broadcastreceiver-inside-an-activity/
In AndroidManifest.xml:
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
In a new file called SMSReceiver.java:
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// get SMS data, if bundle is null then there is no data so return
Bundle extras = intent.getExtras();
if (extras == null) return;
// extract SMS data from bundle
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
// if there's an SMS from this number then send
if(sender.equals("+1800555555") {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(sender, null, "sms received", null, null);
}
}
}
}

why program(send and receive sms) automatically opened for every received sms in android?

I wrote send and received sms in android... I checked received phone number with special number(a phone number that received sms just it) in onReceive method, but this program opened for every phone number that sent sms!!!! but I dont want it!!!!!! my question is that broadcast receiver class for every received sms from every phone number that opened and automatically running?
public class SmsReceiver extends BroadcastReceiver {
public String str = "";
#Override
public void onReceive(Context context, Intent intent) {
// ---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
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]);
//for get sms from special number===============================
String msg_from = msgs[i].getOriginatingAddress();
Log.v("msg_from >>",msg_from);
if(msg_from.equals("08522215"))
{
//===============================
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();
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
}
abortBroadcast();
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<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>
<activity android:name=".MainActivity"
android:label="#string/app_name"/>
<receiver
android:name="com.example.sms.SmsReceiver"
class="com.example.sms.SmsReceiver" >
<intent-filter android:priority="100" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
I think it's because you use this code:
if(msg_from.equals("08522215"))
You should set your number completely (0098913...) or perhaps you can use this code
if(msg_from.contain("08522215")

About android SMS port

I want implement an app which receives SMS on a specific port.
Manifest Code:
<receiver android:name=".BinarySMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED"/>
<data android:port="8091"/>
<data android:scheme="sms"/>
</intent-filter>
</receiver>
And receiver class code below.
public class BinarySMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if(null != bundle)
{
String info = "Binary SMS from ";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
byte[] data = null;
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
info += msgs[i].getOriginatingAddress();
info += "\n*****BINARY MESSAGE*****\n";
data = msgs[i].getUserData();
for(int index=0; index<data.length; ++index)
{
info += Character.toString((char)data[index]);
}
}
Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
}
}
}
I am getting all SMS which are on this (8091) port or not. I want to receive only those messages which are port specific.
For those who are still wondering why the app is receiving data SMS directed to other ports and not just to port 8091. The problem lies in the Manifest code. I've seen many solutions that were posted and most of them are actually wrong.
The Manifest should be :
<receiver
android:name = ".SmsReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data
android:scheme="sms"
android:host="*"
android:port="8095" />
</intent-filter>
</receiver>
The scheme, host and port attributes must be defined in only one "data" element of the intent-filter, not in separate "data" elements.
Also not that from this link, under "data test" section it states that
"The host and port together constitute the URI authority; if a host is not specified, the port is ignored."
So keep in mind that you need to specify the host if you want the app to receive the data SMS for only a specific port.
The "host" element is * (asterisk) is to specify that it accepts data SMS from all hosts / phone numbers
Hope this helps someone (:
I had similar problem, just add following check condition at begin of your 'onReceive' code:
String dataStr = intent.getDataString();
if (dataStr.indexOf(":8888" ) == -1) {
return;
}

Categories

Resources