在Android Eclipse中发送SMS消息

时间:2015-01-06 15:50:35

标签: java android

我正在创建一个简单的应用程序,每次用户打开应用程序时都会发送短信,我无法发送预付费消息。这是我的代码:

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
    }

我已经在最明确的文件中设置了权限。这是我的许可代码。

<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" />

toast消息显示消息已发送,但我无法接收消息。 但当我将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("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
            }

我现在能够收到消息,我需要先获取number.text文件的值并将其放入变量然后将其用作数字,但它不起作用。

2 个答案:

答案 0 :(得分:3)

这可以是:

您的硬编码 48612115 有8个数字,您检查的数字少于10且在if数据块中无效。

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

您应该检查语法是否正确。

希望它有所帮助!

答案 1 :(得分:0)

好的,谢谢大家,我为自己的问题找到了解决方案。 :-)这里是代码:

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();
        }

感谢大家!