0

I'm trying to create an Android application that when you receive a text message with a particular command performs actions such as taking a picture, sending an image or recording a sound. I made the part that reacts to the command received from an authorized number and everything seems to work correctly. Now I would like to write the code to take the photo when the appropriate command is received, I tried to search but I find only examples and guides that explain how to open the camera to take the photo, instead I would like the photo to be taken when receiving the message ( //photo ) without having to open the camera and interact with it.

I share below the code I wrote:

this is my MainActivity

package uk.co.lorenzopulcinelli.smsinterpreter

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.ActivityCompat

class MainActivity : AppCompatActivity() {

    private val requestReceiveSms = 2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)
            != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
            arrayOf(Manifest.permission.RECEIVE_SMS),
                requestReceiveSms
            )
        }
    }
}

this is my class SmsInterpreter

package uk.co.lorenzopulcinelli.smsinterpreter

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.telephony.SmsMessage
import android.widget.Toast

class SmsInterpreter : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {

        val extras = intent.extras
        val mp: MediaPlayer = MediaPlayer.create(context, R.raw.jazzysound)

        if (extras != null) {
            val sms = extras.get("pdus") as Array<*>

            for (i in sms.indices) {
                val format = extras.getString("format")

                val smsMessage = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                    SmsMessage.createFromPdu(sms[i] as ByteArray, format)
                }else{
                    SmsMessage.createFromPdu(sms[i] as ByteArray)
                }

                val phoneNumber = smsMessage.originatingAddress
                val messageText =  smsMessage.messageBody.toString()

                val numberEnabled = "+393332217878"    //example of number enabled to send codes, to not allow anyone

                if (phoneNumber.equals(numberEnabled)){
                    /*
                    Toast.makeText(
                        context,
                        "phoneNumber: ($phoneNumber)\n" +
                                "messageText: $messageText",
                        Toast.LENGTH_SHORT
                    ).show()
                     */
                    when (messageText) {
                        "//photo" -> { println("Photo") }
                        "//sound" -> {
                            mp.start()
                            println("Sound")
                        }
                        "//send" -> { println("Send") }
                        "//record" -> { println("Record") }
                        "//rubrica" -> { println("rubrica") }
                        else -> {
                            println("nessun comando!!")
                        }
                    }

                }else{
                    Toast.makeText(context, "numero non abilitato!!", Toast.LENGTH_SHORT).show()
                }
            }
        }

    }
}

this is my Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SmsInterpreter"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
        <receiver android:name=".SmsInterpreter"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
  • 2
    If you search on "camera" in the Android developer documentation, you will find [quite a bit of material](https://developer.android.com/training/camera/choose-camera-library). – CommonsWare Oct 19 '22 at 22:40
  • thank you so much for the advice, I tried to search but I seem to find only guides on how to open the camera app of the device or how to see a preview of it. I am looking for a way to capture the photo without preview, the moment the message with the code is received, as I did for the //sound command to play the sound – George Jung Oct 21 '22 at 15:39
  • 1
    You are welcome to experiment with https://developer.android.com/training/camerax/take-photo. Bear in mind that IIRC you need special permission to use the camera in the background on modern versions of Android. And, you should discuss your plans for this project with your attorney, if you have not done so already. – CommonsWare Oct 21 '22 at 16:09
  • yes, I figured this wasn't a very "legal" approach but what I'm doing is just a university project – George Jung Oct 21 '22 at 16:14

0 Answers0