Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Added sample android app

Acho Arnoldd376e17e23e1
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 1caf4d9c..7fef7783 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -32,8 +32,8 @@ android {
}
dependencies {
-
implementation 'androidx.core:core-ktx:1.8.0'
+ implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index fdb2b65a..9e711bb7 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -3,6 +3,12 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.httpsms">
+
+ <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
+ <uses-permission android:name="android.permission.READ_PHONE_STATE" />
+ <uses-permission android:name="android.permission.READ_SMS" />
+ <uses-permission android:name="android.permission.SEND_SMS" />
+
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@@ -24,4 +30,4 @@
</activity>
</application>
-</manifest>
+</manifest>
diff --git a/android/app/src/main/java/com/httpsms/MainActivity.kt b/android/app/src/main/java/com/httpsms/MainActivity.kt
index 0393391a..1338c179 100644
--- a/android/app/src/main/java/com/httpsms/MainActivity.kt
+++ b/android/app/src/main/java/com/httpsms/MainActivity.kt
@@ -1,11 +1,76 @@
package com.httpsms
-import androidx.appcompat.app.AppCompatActivity
+import android.Manifest
+import android.annotation.SuppressLint
+import android.content.Context
+import android.content.pm.PackageManager
import android.os.Bundle
+import android.telephony.PhoneNumberUtils
+import android.telephony.TelephonyManager
+import android.util.Log
+import android.view.Menu
+import android.view.MenuItem
+import android.widget.TextView
+import android.widget.Toast
+import androidx.activity.result.contract.ActivityResultContracts
+import androidx.appcompat.app.AppCompatActivity
+import androidx.core.app.ActivityCompat
+import java.util.*
+
class MainActivity : AppCompatActivity() {
+ private val logTag = MainActivity::class.simpleName ?: "MainActivity"
+
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
+
+ val phoneNumber = getPhoneNumber(this)
+
+ val titleText = findViewById<TextView>(R.id.cardPhoneNumber)
+ titleText.text = PhoneNumberUtils.formatNumber(phoneNumber, Locale.getDefault().country)
+ }
+
+ @SuppressLint("HardwareIds")
+ private fun getPhoneNumber(context: Context): String {
+ // Register the permissions callback, which handles the user's response to the
+ // system permissions dialog. Save the return value, an instance of
+ // ActivityResultLauncher. You can use either a val, as shown in this snippet,
+ // or a late init var in your onAttach() or onCreate() method.
+ val requestPermissionLauncher =
+ registerForActivityResult(
+ ActivityResultContracts.RequestPermission()
+ ) { isGranted: Boolean ->
+ if (isGranted) {
+ val toast = Toast.makeText(context, "Granted", Toast.LENGTH_SHORT)
+ toast.show()
+ } else {
+ val toast = Toast.makeText(context, "NOT Granted", Toast.LENGTH_LONG)
+ toast.show()
+ }
+ }
+
+ val telephonyManager = this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
+ if (ActivityCompat.checkSelfPermission(
+ this,
+ Manifest.permission.READ_SMS
+ ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
+ this,
+ Manifest.permission.READ_PHONE_NUMBERS
+ ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
+ this,
+ Manifest.permission.READ_PHONE_STATE
+ ) != PackageManager.PERMISSION_GRANTED
+ ) {
+ // You can directly ask for the permission.
+ // The registered ActivityResultCallback gets the result of this request.
+ requestPermissionLauncher.launch(Manifest.permission.READ_SMS)
+ requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_NUMBERS)
+ requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_STATE)
+ }
+
+ val phoneNumber = telephonyManager.line1Number ?: "NO_PHONE_NUMBER"
+ Log.d(logTag, phoneNumber)
+ return phoneNumber
}
}
diff --git a/android/app/src/main/res/layout/activity_main.xml b/android/app/src/main/res/layout/activity_main.xml
index 487692e1..05b2165a 100644
--- a/android/app/src/main/res/layout/activity_main.xml
+++ b/android/app/src/main/res/layout/activity_main.xml
@@ -6,13 +6,58 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
- <TextView
- android:layout_width="71dp"
- android:layout_height="17dp"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
+ <com.google.android.material.card.MaterialCardView
+ android:id="@+id/card"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_margin="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
+ app:layout_constraintTop_toTopOf="parent">
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical"
+ android:padding="16dp">
+
+ <!-- Title, secondary and supporting text -->
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="horizontal">
+
+ <TextView
+ android:id="@+id/cardPhoneNumber"
+ android:layout_width="wrap_content"
+ android:layout_height="match_parent"
+ android:text="@string/phone_number"
+ android:textAppearance="?attr/textAppearanceTitleMedium"
+ android:textColor="?android:attr/textColorPrimary"
+ android:textSize="28sp" />
+
+ <com.google.android.material.switchmaterial.SwitchMaterial
+ android:id="@+id/cardSwitch"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:minHeight="48dp"
+ android:text=""
+ tools:ignore="TouchTargetSizeCheck" />
+ </LinearLayout>
+
+
+ <TextView
+ android:id="@+id/cardRefreshTime"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="8dp"
+ android:text="@string/nextRefreshTime"
+ android:textAppearance="?attr/textAppearanceBodyMedium"
+ android:textColor="?android:attr/textColorSecondary"
+ android:textSize="16sp" />
+
+ </LinearLayout>
+
+ </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
diff --git a/android/app/src/main/res/values-night/themes.xml b/android/app/src/main/res/values-night/themes.xml
index ae5fad9f..9d120d1c 100644
--- a/android/app/src/main/res/values-night/themes.xml
+++ b/android/app/src/main/res/values-night/themes.xml
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
- <style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
+ <style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
@@ -10,7 +10,7 @@
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
- <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
+ <item name="android:statusBarColor" tools:targetApi="l">#121212</item>
<!-- Customize your theme here. -->
</style>
</resources>
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index dc0c1c22..4fad1216 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -1,3 +1,7 @@
<resources>
<string name="app_name">HttpSMS</string>
+ <string name="nextRefreshTime">23/06/2022 18:58:30</string>
+ <string name="phone_number">+1 800 555 0100</string>
+ <string name="menuIconDescription">Menu Icon</string>
+ <string name="menuIconName">More</string>
</resources>
diff --git a/android/app/src/main/res/values/themes.xml b/android/app/src/main/res/values/themes.xml
index 4f328f19..f8f4268d 100644
--- a/android/app/src/main/res/values/themes.xml
+++ b/android/app/src/main/res/values/themes.xml
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
- <style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
+ <style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>