0

I am a complete newbie to Kotlin and Android Studio but I have decided that I want to write an app for use at my work. I can write what I need in Excel and VB but unfortunately Macros are not supported on Android mobile devices, hence why I would like to write an app instead. I have worked out how to create pages (activities) and how to navigate back and forth between them (roughly), However, my ultimate goal is to capture the data that the user would enter into the text fields and then once they clicked the submit button it would then send the data they had entered via SMS to a predefined phone number (S) or alternatively as a text file to an email address. Any help would be greatly appreciated. Please let me know what you need me to post to enable any help Thanks in advance Code screenshot

package com.test.warehousecontrol
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.core.app.ActivityCompat
import android.telephony.SmsManager
import kotlinx.android.synthetic.main.activity_second.*

    class SecondActivity : AppCompatActivity()


    {
    override fun onCreate(savedInstanceState: Bundle?)


    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
       //  if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)!=PackageManager.PERMISSION_GRANTED)
        val backbut = findViewById<Button>(R.id.backbut)
        backbut.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }


        }
        private fun getSendSmsIntent(phoneNumber: String, content: String?): Intent? {
            val uri = Uri.parse("smsto:$phoneNumber")
            val intent: Intent = Intent(Intent.ACTION_SENDTO, uri)
            intent.putExtra("sms_body", content)
            return getIntent(Intent, true)
            //  return getIntent(intent, true)

            val sub1: Button = findViewById<Button>(R.id.sub1)
            sub1.setOnClickListener {

                val cust: String = cust.text.toString()
                val reg: String = reg.text.toString()
                val pal: String = pal.text.toString()
                val data: String =
                    "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ")
                        .plus(pal)
                startActivity(getSendSmsIntent("1234567", data))

            }
        }

        }
Glenn Arnold
  • 105
  • 2
  • 8

1 Answers1

2

To send an SMS :

val submit: Button = findViewById<Button>(R.id.sub1)
    submit.setOnClickListener {
        val cust: String = custTextField.text.toString()
        val reg: String = regTextField.text.toString()
        val pal: String = palTextField.text.toString()
        val data:String = "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ").plus(pal)
        startActivity(getSendSmsIntent("YOUR PHONE NUMBER HERE", data))
    }

To send an email :

val submit: Button = findViewById<Button>(R.id.sub1)
        submit.setOnClickListener {
            val cust: String = custTextField.text.toString()
            val reg: String = regTextField.text.toString()
            val pal: String = palTextField.text.toString()
            val data:String = "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ").plus(pal)
            startActivity(Intent.createChooser(getEmailIntent("USER@GMAIL.COM", "SUBJECT", data), "Send mail"))
        }

private fun getSendSmsIntent(phoneNumber: String, content: String?): Intent? {
        val uri = Uri.parse("smsto:$phoneNumber")
        val intent = Intent(Intent.ACTION_SENDTO, uri)
        intent.putExtra("sms_body", content)
        return getIntent(intent, true)
    }

    private fun getEmailIntent(email: String, subject: String?, content: String?): Intent? {
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "message/rfc822"
        intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
        intent.putExtra(Intent.EXTRA_SUBJECT, subject)
        intent.putExtra(Intent.EXTRA_TEXT, content)
        return getIntent(intent, true)
    }


    private fun getIntent(intent: Intent, isNewTask: Boolean): Intent? {
        return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
    }

Your final Activity would be like this :

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity


class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        //  if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)!=PackageManager.PERMISSION_GRANTED)
        val backbut = findViewById<Button>(R.id.backbut)
        backbut.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
        val sub1: Button = findViewById<Button>(R.id.sub1)
        sub1.setOnClickListener {
            val cust: String = cust.text.toString()
            val reg: String = reg.text.toString()
            val pal: String = pal.text.toString()
            val data: String =
                "CUST : ".plus(cust).plus("\n").plus("REG : ").plus(reg).plus("\n").plus("PAL : ")
                    .plus(pal)
            startActivity(getSendSmsIntent("1234567", data))
        }
    }

    fun getSendSmsIntent(phoneNumber: String, content: String?): Intent? {
        val uri = Uri.parse("smsto:$phoneNumber")
        val intent = Intent(Intent.ACTION_SENDTO, uri)
        intent.putExtra("sms_body", content)
        return getIntent(intent, true)
    }
    private fun getIntent(intent: Intent, isNewTask: Boolean): Intent? {
        return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
    }
}
  • Thank you very much for your help, I will try this. – Glenn Arnold Sep 19 '20 at 06:19
  • Thank you, sorry, but can you tell me where I add this code? If, for example I have a button that I have named Submit with an id of sub1 in an activity called SecondActiviy? The data that I want to send is in 3 plain text fields with id's of cust, reg and pal. Any advice or help greatly appreciated – Glenn Arnold Sep 19 '20 at 07:02
  • thank you so much for your help and I think i am nearly there, only I get an error message if I use Private fun (Modifier 'private' is not applicable to 'local function')and also if I add the Intent code (Too many arguments for public open fun getIntent(): Intent! defined in com.test.warehousecontrol.SecondActivity) – Glenn Arnold Sep 19 '20 at 12:58
  • I have added a link to a screen shot of my SecondActivity.KT code. Thanks again – Glenn Arnold Sep 19 '20 at 13:34
  • @GlennArnold move the `private fun getSendSmsIntent()` ... from `onCreate` to outside, otherwise, copy and paste your code, in your question, I will modify it – Mouaad Abdelghafour AITALI Sep 19 '20 at 14:04
  • Thanks again Mouaad Abdelghafour AITALI I have tried modifying it as per your suggestion but still keep getting the 'Too many arguments' error message when trying to run the app. I have pasted the code in my question as requested. Thank you again for your help, Glenn – Glenn Arnold Sep 19 '20 at 19:46
  • @GlennArnold No worries bro, I've updated my answer – Mouaad Abdelghafour AITALI Sep 19 '20 at 19:54
  • Thank you!!! Thats brilliant - Its been bugging me for over a week!! Out of interest Is there any way that it can automatically send the SMS without user intervention?? – Glenn Arnold Sep 19 '20 at 20:04
  • Thank you for your help! However, is there simple way to avoid the user having to use the messaging app? Thank you in advance... – Glenn Arnold Sep 21 '20 at 19:36
  • @GlennArnold check this answer : https://stackoverflow.com/a/35656953/7954210 – Mouaad Abdelghafour AITALI Sep 21 '20 at 19:58
  • Thank you again!, I will have a look. – Glenn Arnold Sep 21 '20 at 20:22
  • Unfortunately I couldn't get that to work...converted it to Kotlin but no luck. Thank you anyway. I'll ask a new question later separately to save bothering you again! – Glenn Arnold Sep 23 '20 at 20:32
  • Hi, sorry to hear that, yeah create a new question, I will try to handle your needs, maybe I can make it – Mouaad Abdelghafour AITALI Sep 23 '20 at 20:40