简体   繁体   中英

Sending SMS Message in Android Eclipse

I am creating a simple application that sends sms message every time the users open the application, I am unable to send the prepair message. here is my code:

MainActivity.java:

//The contact number is save in a number.text, so i get it first
    FileInputStream fIn = null;
    InputStreamReader isr = null;
    String number = null;
    try{
        char[] inputBuffer = new char[1024];
        String data = null;
        fIn = openFileInput("number.txt");
        isr = new InputStreamReader(fIn);
        isr.read(inputBuffer);
        data = new String(inputBuffer);
        number = data;
        isr.close();
        fIn.close();

        if(number == null || number.length() < 10){
            //Do nothing
        }else{
            smsBody = "Hello World!";
            try {
                // Get the default instance of the SmsManager
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(number, null, smsBody, null, null);//Im not able to send this.
                Toast.makeText(getApplicationContext(), "Message Sent to " + number + ".",
                        Toast.LENGTH_LONG).show();
            } catch (Exception ex) {
                Toast.makeText(getApplicationContext(),"Sending message failed!",
                        Toast.LENGTH_LONG).show();
                ex.printStackTrace();
            }

        }

    }catch(IOException e){
        //Do nothing
    }

I already set the permission in manifiest file. here is my permission code.

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
//Im adding some features so i need this
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

The toast message is displaying that the message is already send, but i'm not able to recieve it. but when i change the MainActivity.java to like this:

            //The contact number is save in a number.text, so i get it first
            FileInputStream fIn = null;
            InputStreamReader isr = null;
            String number = null;
            try{
                char[] inputBuffer = new char[1024];
                String data = null;
                fIn = openFileInput("number.txt");
                isr = new InputStreamReader(fIn);
                isr.read(inputBuffer);
                data = new String(inputBuffer);
                number = data;
                isr.close();
                fIn.close();

                if(number == null || number.length() < 10){
                    //Do nothing
                }else{
                    smsBody = "Hello World!";
                    try {
                        // Get the default instance of the SmsManager
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage("48612156715", null, smsBody, null, null);//I change this from smsManager.sendTextMessage(number, null, smsBody, null, null); where the 48612115 is my example number.
                        Toast.makeText(getApplicationContext(), "Message Sent to " + number + ".",
                                Toast.LENGTH_LONG).show();
                    } catch (Exception ex) {
                        Toast.makeText(getApplicationContext(),"Sending message failed!",
                                Toast.LENGTH_LONG).show();
                        ex.printStackTrace();
                    }

                }

            }catch(IOException e){
                //Do nothing
            }

I am now able to recieve the message, I need to get first the value of number.text file and put it into variable and then use it as the number, but it is not working.

This can be:

Your hardcoded number 48612115 has 8 digits and you are checking for less than 10 and doing nothing in your if block.

if(number == null || number.length() < 10)
{
   // do nothing
}
else
{
   // send message
}

You should check for correct syntax.

Hope it helps!

Ok, thanks to everyone i found a solution for my own problem. :-) here is the code:

String phoneNumber = "";
String smsBody = "Example Message";
        try {
            InputStream instream = new FileInputStream(
                    "data/data/com.example.test/files/number.txt");
            if (instream != null) {
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);
                String line;
                if ((line = buffreader.readLine()) != null)
                    phoneNumber = line;

                try {
                    // Get the default instance of the SmsManager
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNumber, null, smsBody, null, null);

                    Toast.makeText(getApplicationContext(), "Message Sent to " + phoneNumber + ".",
                            Toast.LENGTH_LONG).show();
                } catch (Exception ex) {
                    Toast.makeText(getApplicationContext(),"Sending Message failed!",
                            Toast.LENGTH_LONG).show();
                    ex.printStackTrace();
                }

                instream.close();
            }
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(),e,
                            Toast.LENGTH_LONG).show();
        }

Thanks to everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM