-3

I want to make an app to send some information to the company via email if I have internet connection as a user and I use email intent

String priceMassage = creatOrderSummery(price, hasWippedCream, hasChocolate, name);

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto: ")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + name);
intent.putExtra(Intent.EXTRA_TEXT, priceMassage);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

displayMessage(priceMassage); 

but if I don't internet connection I want the information sent to company mobile number via SMS, how can I do it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
zainab
  • 13
  • 4

5 Answers5

0

You have two options to send SMS.

  1. Using SmsManager API

    SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);

  2. Using Built-in SMS application

    Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", "default content"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent);

Both need SEND_SMS permission.

<uses-permission android:name="android.permission.SEND_SMS" />

More details can be found here

Sudhin Philip
  • 644
  • 8
  • 15
0

You need to check for internet connectivity first if present use email otherwise open the intent to send sms message.

for connectivity check see this helpful answer : https://stackoverflow.com/a/8548926/4428159

For sending sms I'm assuming you need to just open the sms app and send the message so have a look at this code: https://stackoverflow.com/a/9798870/4428159

For this you don't need sms permission in your application.

public void sendSMS(View v) 
      { 
     Uri uri = Uri.parse("smsto:12346556"); 
         Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
         it.putExtra("sms_body", "Here you can set the SMS text to be sent"); 
         startActivity(it); 
      } 
Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
0

you need to check if the device is connected to the internet

you can check internet connection via ConnectionManager class.

ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

 cm.getActiveNetworkInfo().isConnected(); // return true if internet or otherwise flase

if (internet){
 // Email send code here
} else {
 // SMS send code here
}

this is the sample solution

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
0

In order to send sms

In AndroidManifest.xml add permission

<uses-permission android:name="android.permission.SEND_SMS" />

public void sendSMS(String phoneNo, String msg) {
    try {      
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, msg, null, null);    
        Toast.makeText(getApplicationContext(), "Message Sent",
              Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
              Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    } 
}
Milan Hirpara
  • 534
  • 4
  • 18
-1

use this code to check intenet connection..in main

TestInternet testInternet = new TestInternet();
        testInternet.execute();

out of main

class TestInternet extends AsyncTask<Void, Void, Boolean>
        {
            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    URL url = new URL("http://www.google.com");
                    HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                    urlc.setConnectTimeout(4000);
                    urlc.connect();
                    if (urlc.getResponseCode() == 200) {
                        return true;
                    }
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    return false;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return false;
                }
                return false;
            }

            @Override
            protected void onPostExecute(Boolean result)
            {
                if (!result)
                {
               // send sms
                else
                    {
                     //send mail     
                    }
            }
        }