The course is part of this learning path
In this course, we'll cover how to access the features of Android devices. You'll learn how to send an SMS, send an email, make a call, and convert speech to text on Android.
This course is intended for anyone who wants to learn how to start building their own apps on Android.
To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.
Well, hello everyone? All right. So, in this video, we're going to start a new chapter. In this particular section, we're going to learn how to access features of Android devices. So, the first subject of this section will be sending SMS. So, we're going to have to dive into the SMS section of whatever the device is via Android app and send a text message from there. So, the important part here is that we're not going to be sending a message from the application, we send it via the text message feature of the device, and that way we can send it to any phone number that we want, right?
So, let's just get into Android Studio and I'll show you how to do it. So, I want to show you first up, of course, the finished version of this particular application. So, you can see where we're going, see how there two edit texts in this application. One of them is to write the user's message, the other one is to write down the phone number to which the message will be sent. There's also a button to send the message, and then this way when the user presses the send SMS button, the message will be sent from the device's SMS section. Why ? You don't believe me, you think it's all theory? Well, here I'm going to write hello in the message section. And almost right there, phone number. In the phone number section, I'll press the 'Send SMS', and look at that after pressing the 'Send SMS' button, it asked for permission for the user.
So, in older versions of Android, it was enough to just get the permission from the manifest file. However, due to increased security policy, we're going to need to both request permission in the manifest file and do some coding for a variety of operations, such as accessing the camera, accessing the files on the device, sending SMS, accessing location, all these things. So, in this lesson, we're going to learn these details. So, now I'll just choose the allow option, and look at that as you can see when you give the permission, a toast message appears saying an SMS message was sent.
Now, how do we know that the message was actually sent? Well, for this, I'm going to have to quit the application, switch to the messages section of the device. And look at that, you can see here the message has been sent. Now, you can take this and apply this system to many different systems however you like. But the important thing to remember here is that we can reach the messages' section of the device via the Android application that we build and send a message from the number we want.
So, we can send messages as well as read incoming messages, all right? So, why don't we start developing? First of all, I'm just going to close up this project and I'm going to click 'File', select the closed project option. But, while I'm here I want to click the 'New Project' button and from here I can choose 'Empty Activity' again and click the 'Next' button. So, it's from this page we'll configure our project, name of the project can be sendSMS. I'm not going to change the package name in the file path where the project is going to be saved and of course the programming language will still be Kotlin, minimum SDK level 21, because we're going to be using different methods for before and after API 23, okay? So, it's better the minimum SDK level is below 23.
Okay, so after that announcement finally I'll click the 'Finish' button. Cool. So, now our new project is being created and of course don't do anything else while the project is being created otherwise you could encounter all just unpredictable stuff that you might not know how to work yourself out of. All right, so there's our project created. Let's open up the activity_main.xml file. So, close the project directory. I want to zoom in on the design area a bit. And I'll click on the eye icon, and I'll choose 'Show System UI'. Also, I want to delete the text view component in the design area. But now I'm going to have to add two edit texts. First edit text will be multiline edit text. That way, the user can write a really long message. And I want to make the width of this edit text 300dp, make the height 100dp, and I'm going to add a hint to this edit text I'll just make the hint write your message. Likewise, I will add another added text, this will be the phone edit text and this is where the phone number is going to go.
All right. So, I'll make the width of that 300dp. And of course, I want to add a hint to the edit text, the hint can be enter phone number and also an add button, right? So, I'll change the text of the button to be sendSMS. Now, since the layout is constraint layout here, I will have to give these components constraint values from the edges otherwise, all the components are going to move to the 0.0 position when the application is open and well it'll be confusing. So, first, I want to center these three components on the horizontal plane. I'll select the components, right click, and select center horizontally.
All right, so I want to determine the vertical constraint layouts now. Top constraint value of the multiline edit text can be 100dp, top constraint value of the phone edit text can be 20dp. Top constraint value of the button can be 50 dp. All right. So, let's make some IDs. So, I'll write in the edit text message to the ID of the first component. For the second edit text, I'll write editTextNumber, and for the button I'll just write buttonSend. Now we can go to the MainActivity.kt file. So, first up, I want to define the components here. Lateinit var message : EditText. Lateinit var number : EditText. Lateinit var send : Button, okay?
So, now I can match the IDs in the on-create method, message = findViewById( R.id.editTextMessage). Number = findViewById( R.id.editTextNumber). Send = findViewById( R.id.buttonSend). Yes, I do want to add a click listener to the button. Okay, so where are we now? I have to take the user input data and assign it to string containers. So, for all of that, I want to create two string containers in the global area, because I'm going to use these data in different places in this particular project. So, if I create the containers inside the click listener method, I'm not going to be able to access containers outside the scope of the click listener, right? So, var userMessage : String = "", and I will set the initial value of the variable as an empty value. Of course, if you want to, you can just put a question mark after the string expression and assign the null values to these variables. And that'll work, so let's define the second container. Var userNumber : String = "".
All right, so now let's get that data from the user. First, I want to get the message, so userMessage = message.text.toString, then I can get the phone number. UserNumber = number.text.toString. Then, I'm going to use a separate method for sending messages. So, outside the on-create function, I'm going to write fun sendSMS. And this method will take two arguments. The first argument will be user message, so I'll write userMessage : String. And the other will be user number, so I write userNumber : String.
Now, I'm going to call this method inside the click listener of the button. And I'll write userMessage as the first parameter and userNumber as the second parameter. Great. So, now we can write the necessary codes to the sendSMS function. So, what we need to do first is get permission from the user before sending the message here. Now, of course, you remember when the user presses the button, the permission window appears. So, after the user gives permission, you're then able to access the message part of the device, otherwise we won't be able to use the creative messaging feature. So, that means our path is laid out in front. We need to get user permissions from the manifest file. The user sees these permissions when downloading the application from Google Play and then downloads it accordingly, alright. So, I'll just open up the manifest file, and when I write uses-permission in here, the permissions I can get are immediately shown. So, here are their names like send SMS, receive SMS, read SMS. So, that means I've got to get permission for whatever I want to do.
So, right now I'm getting the SEND_SMS permission, and I'll close the tag just by pressing the slash key on the keyboard. Alright, so now let's keep writing code. Now remember I mentioned in the beginning it is not enough to just specify the permission in the manifest file to access the devices messages, right? We also need to write some code, so that's why I want you to follow this very carefully. Okay, so first we need to create an if else statement.
So, what we'll do is we'll check to see if the user has given permission or not, and this is how we do it. Alright, ContextCompact.checkSelfPermission. And this method will take two parameters. The first one is context. So, here I'll write that this keyword as my context. The other will be Manifest.permission.SEND_SMS. So, you see here it is checked if the permission is obtained from the manifest file without sending the message or before you know. So, I'll add the not equal sign after the parentheses. Now write PackageManager.PERMISSION_GRANTED. So, the code after the not equal will ask the user for the second permission remember? If the user does not allow it, well, it won't take any action, won't be able to. So, here it compares the permission given by the user when sending a message to the permission in the manifest file.
Now, let me make this point clear even if you get permission in the manifest file, if the user doesn't give permission again, the condition, well, this if condition will work. So, this condition is if the message will not be sent, you follow? So, I'll write ActivityCompat.requestPermissions inside of the if condition. So, here the first parameter is the context. So, I write this keyword again. Second parameter is an array. So, it's within this array we will specify the permissions that we need to obtain from the user. So, your application may request more than one permission from the user at the same time BTW. You must specify all of these permissions right here in this array, and that's why I'm typing arrayOf here, okay? I'll write that permission that I will get inside the parenthesis Manifest.permission.SEND_SMS.
Third parameter will be request code. And here I'll write 1 in the request code, and then this section here is the standard section. So, here there's an extra security measure brought to you by Android. So, in other words, even if the user accepts their permissions while downloading the application, they're going to ask for confirmation from the user in situations requiring high security. So, sending SMS is one of the processes that require this approval, right? As it should be. So, then I write the else condition. If the user allows, the codes you write here will work. So, we will send the SMS message here.
Now there's also been a minor update to the methods that we use to send messages. There are some deprecating codes from the codes that we use, but here I just want to show you the old and the new codes. So, first I'll write the code that we've been using for a long time anyway. For this process, I'll just create an object from the SMS manager class, and I'm writing val smsManager: SmsManager= SmsManager.getDefault. Now, as you can see, the getDefault method is deprecating. Of course, if we run the application on an emulator with the most current API 31 level, the application will still work, but in the future they can cancel it in the current versions of Android. So, that's why here I want to show you this second and current method.
So, again, we're going to create an object belonging to the SMS manager class, val smsManager: SmsManager=, then after the equal sign I write this.getSystemService. Then in parentheses I write SmsManager::class.java. Now I want you to notice the get system service, this here is underlined because this method also works on API 23 and above. So, we should create a check file, we do that like this. If the SDK level of the device is above 23, it should use this code. If it is below it should use this code, and I'll create a new if else statement in the else block. Alright, so if Build.VERSION.SDK_INT is equal to 23 and greater than, I will cut and paste the code here. Else, I'll cut and paste the code here. That's why I wanted to show you both methods. Alright, that way we'll be able to send SMS at all API levels. In fact, it's going to be better if we create the SMS manager object here once outside of the if fragment.
Outside of the if else statement, I'll write val smsManager : SmsManager. So, now we can delete the val keywords here and the : SmsManager expressions, you see that? Alright, so now I can send the SMS outside the if else statement. I'll write smsManager.sendTextMessage. So, this method is going to take five parameters. The first parameter is the phone number to send. So, use your number. Second parameter will be null. Third parameter is the text of the message to be sent which means user message. Fourth and fifth will be null again, alright. So, the process of requesting permission from the user and sending messages is now complete. But there is one more thing that we've got to do.
If the user is running the application for the first time, the permission window will open when they want to send a message, okay? So, the user may or may not allow it here. So, this is where we need to follow up. If the user allows, we must define the actions that need to be taken, alright. The codes we wrote here are the codes that will happen if the user allows it to run, but like I just said, when the user runs the application for the first time, the if block here will be activated and allow the user to send a message. So, here we have to follow the result of the permission process. So, we will do this with a different method. So, outside of the onCreate method, I will call the onRequest permission result method. So, it's in this method we will create an if statement to control the user's reaction. So, with this we will use the request code and grant result parameters given as parameters. So, why don't we just get started writing the necessary code? If the request code is 1, and the size of the grant result array is greater than 0, the first element of the grant result array is PackageManager.PERMISSION_GRANTED. The user is granted permission. So, in this case I have to perform the message sending process again, right? So, I'm copying all of the code here and pasting it in here.
Now it's true, you can also create the else block here. The else block will work if the user doesn't allow permission. So, in this case you can show a toast message to the user, but I'm not going to need the else block for now, so I can skip it but you can do it if you want. However, we can also show a toast message to inform the user when the message is sent. So, I'm writing Toast.makeText. So, this method takes three parameters. The first one is application contact. The second one is the information message that we will give to the user. The message can be "Message Sent," alright? So, the third parameter will be Toast.LENGTH_LONG, and then finally I'll write .show. I'm also going to copy the toast message here and I'll paste it in here as well. Alright, so you get it right.
This will work when the user gives permission for the very first time. So, when you open this application later, this will work because it now allows it. Okay, so after that, the app is ready we can run it. So, I first want to test the application within an emulator with API level 31, and that is Operating system Android 12 because that's the most current version at the moment. So, of course, it might take a little time to open the application, but once it's opened, I'll write 'hello android' in the message section, and I can just write any phone number in the number section.
So, when I click this send message button, it asked for extra permission from the user just like we wanted it to. So, it will warn the user here that it wants to read the messages section of the application, and if the user gives permission, the message will be sent. Alright, so we've done everything in here that the user can see, but we could have been doing these transactions confidentially, did you know that? For example, while the user sees other things in the foreground, we could be sending a message in the background to a number that we wanted when the user pressed a key. Android Operating System takes extra security measures for exactly this situation. So, that way any time the application wants to send SMS or wants to reach the camera, well, it's going to tell the user. That's a good thing, right?
So, I'll just click on 'Allow.' Message was sent, a toast message appears. So, now we'll move on over to the messages section and check, and as you can see the message has been sent. So, why don't we just test it on a device with API level 21? So, the app opens, I'll write 'hello kotlin' and to the message section I'll write a number. Alright, I'll click the 'Send' and this time it didn't ask for any permissions and it sent the message right away, because in the older versions of Android there wasn't any need for asking of separate permission from the user. Also, please note that we were able to send the message in an older version as well, right? So, now let's just make sure that the message was actually delivered, and there you can see it was sent successfully. Alright, my friends, that's how you send messages through the messaging app on an Android. So, I hope it was useful I think it was, it certainly was, yeah, it was. Alright, take a short break if you want, I want to see you in the next lesson that we've got more to do.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.