Hello everyone, welcome to the first title of my series “Making Apps That AUTOMATE THINGS”.
To make this app we have to take three inputs from users message, mobile number and count of how many times want to send SMS. After that, we need to pass this data to android’s SMS manager. SMS manager will send a message and return acknowledgement if the message sent or not. Then we need to repeat this in the loop until our count is complete.
Step 1: Create new empty activity using android studio and give it name MainActivity.java. Don’t forget to add that created activity in the AndroidManifest.xml.
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Step 2: Adding SMS permission in the manifest.
<uses-permission android:name="android.permission.SEND_SMS" />
So finally AndroidManifest will look like this.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.geniobits.autosmssender"> <uses-permission android:name="android.permission.SEND_SMS" /> <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" tools:ignore="GoogleAppIndexingWarning"> <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>
Step 3: Creating a view for activity. Open activity_main.xml file from res/layout and add these code in the file.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="vertical"> <EditText android:id="@+id/txt_message" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginStart="20dp" android:layout_marginTop="10dp" android:layout_marginEnd="20dp" android:layout_marginBottom="10dp" android:gravity="center" android:hint="Enter Message" android:textSize="18sp" /> <EditText android:id="@+id/txt_mobile" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:layout_marginBottom="10dp" android:gravity="center" android:hint="Enter Mobile Number" android:textSize="18sp" /> <EditText android:id="@+id/txt_count" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:layout_marginBottom="10dp" android:gravity="center" android:hint="Enter count" android:textSize="18sp" /> <Button android:id="@+id/btn_manual" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@android:color/holo_blue_dark" android:text="Send Sms Manually" android:textColor="@android:color/white" android:textSize="18sp" /> <Button android:id="@+id/btn_sms" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:background="@android:color/holo_blue_dark" android:text="Send Sms" android:textColor="@android:color/white" android:textSize="18sp" /> <Button android:id="@+id/btn_whatsapp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@android:color/holo_blue_dark" android:text="Send Whatsapp Sms" android:textColor="@android:color/white" android:textSize="18sp" /> </LinearLayout> </LinearLayout>
Step 4: In these, we need to write code to ask user permission to send SMS. For this, we will use the library called “Dexter”. It’s very easy to use this library. Here is the link for details.
Dexter.withActivity( .withPermission(Manifest.permission.SEND_SMS) .withListener(new PermissionListener() { @Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */} @Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */} @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */} }).check();
Step 5: Finally we use SMS Manager to send messages like these-
SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(txt_number.getText().toString(), null, txt_message.getText().toString(), null, null);
So finally our MainActivity.java will look like these.
package com.geniobits.autosmssender; import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.karumi.dexter.Dexter; import com.karumi.dexter.PermissionToken; import com.karumi.dexter.listener.PermissionDeniedResponse; import com.karumi.dexter.listener.PermissionGrantedResponse; import com.karumi.dexter.listener.PermissionRequest; import com.karumi.dexter.listener.single.PermissionListener; public class MainActivity extends AppCompatActivity { private EditText txt_message; private EditText txt_number; private EditText txt_count; private Button btn_manual; private Button btn_sms; private Button btn_whatsapp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt_message = findViewById(R.id.txt_message); txt_number = findViewById(R.id.txt_mobile); txt_count = findViewById(R.id.txt_count); btn_manual = findViewById(R.id.btn_manual); btn_sms = findViewById(R.id.btn_sms); btn_whatsapp = findViewById(R.id.btn_whatsapp); Dexter.withActivity(this) .withPermission(Manifest.permission.SEND_SMS) .withListener(new PermissionListener() { @Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */} @Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */} @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */} }).check(); btn_sms.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { for (int i=0; i < Integer.parseInt(txt_count.getText().toString()); i++) { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(txt_number.getText().toString(), null, txt_message.getText().toString(), null, null); Toast.makeText(MainActivity.this, "SMS Sent : count"+(i+1), Toast.LENGTH_SHORT).show(); } }catch(Exception e){ Toast.makeText(MainActivity.this, "SMS sending failed!", Toast.LENGTH_SHORT).show(); } } }); btn_manual.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent sendSms = new Intent(Intent.ACTION_SEND); sendSms.setType("text/plain"); sendSms.putExtra(Intent.EXTRA_TEXT,txt_message.getText().toString()); startActivity(sendSms); } }); btn_whatsapp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } }
Here’s the output-
I hope you found this article very useful. In the next article of these series. I’ll add an article about choosing multiple contacts from android’s contact chooser and sending them multiple messages. So stay tuned and please give me feedback.