如何使用Java android代码发送许多短信? [英] How to send many SMS using Java android code?

查看:36
本文介绍了如何使用Java android代码发送许多短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序,该程序能够从PC向手机发送短信.它是使用在线网关还是使用加密狗发送SMS都没有关系(但是,您知道,只有使用加密狗时,发送者的原始号码才会存在,不是吗?).我发现互联网上的大多数示例都是在2006-2010年编写的,它们都使用JavaMail和Java Communication API,并且目前几乎没有通信API,可能是oracle放弃了.

I am creating a program which is capable of sending sms from PC to mobile phones. It doesn't matter whether it is sending SMS using an online gateway or using the dongle (but, u know, sender's original number will be there only if we use a dongle, isn't it?). Most examples in internet I found are written in 2006-2010 and they all use JavaMail and Java Communication API, and the communication api is almost missing at the moment, probably oracle gave it up.

我正在寻找一些最新的,正在运行的技术.这是我的第一次尝试.我很高兴看到教程.谢谢

I am seeking for some technology, which is upto date and up and running. This is my first attempt in such. I am very much glad to see tutorials. Thank you

推荐答案

经过几次尝试&错误,我在一年前创建了以下代码,它工作正常.它使用GSM调制解调器发送SMS.您的GSM调制解调器应连接到通讯端口.您可能需要Comm.jar库来运行此代码,可以从 http://llk.media.mit.edu/projects/picdev/software/javaxcomm.zip

After doing few Try & Error, i created following code a year ago, it is working fine. It uses GSM Modem to send SMS. Your GSM Modem should be connected to your Comm Port. You would be requiring Comm.jar library to run this code, You can download it from http://llk.media.mit.edu/projects/picdev/software/javaxcomm.zip

我的代码是触发

My code is simple programmtic version of firing AT Commands. It simply fires AT Command on GSM Modem to send SMS.

import javax.comm.*;
import java.io.*;
import java.util.*;

public class SimpleWrite implements Runnable, SerialPortEventListener
{
    public void run()
    {}

    static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "";
    static char ch = '"';
    static String dest = ch + "111111111" + ch;  // 10 Digit Mobile Number.
    static InputStream inputStream;

    static SerialPort serialPort;
    static OutputStream outputStream;

    public void serialEvent(SerialPortEvent event)
    {
        switch (event.getEventType())
        {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
        {

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            try
            {

                while ( (line = reader.readLine()) != null)
                {
                    System.out.println(line);
                }
            }
            catch (IOException e)
            {
                System.err.println("Error while reading Port " + e);
            }
            break;

        }
        } //switch
    }

    public SimpleWrite(SerialPort serial)
    {
        try
        {
            inputStream = serial.getInputStream();
            try
            {
                serial.addEventListener(this);
            }
            catch (TooManyListenersException e)
            {
                System.out.println("Exception in Adding Listener" + e);
            }
            serial.notifyOnDataAvailable(true);

        }
        catch (Exception ex)
        {
            System.out.println("Exception in getting InputStream" + ex);
        }

    }

    public static void main(String[] args)
    {
        String line1 = "AT+CMGF=1\r\n";
        String line2 = "AT+CMGS=" + dest + "\r\n";
        String line3 = messageString + "\r\n";
        //String line1 = "AT+CREG=2";
        //String line2 = "AT+CGREG?";

        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if ( portId.getName().equals("COM11"))
                {
                    System.out.println("SMS Sending....Port Found");
                    try
                    {
                        serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                        SimpleWrite wr = new SimpleWrite(serialPort);

                    }
                    catch (PortInUseException e)
                    {
                        System.out.println("Port In Use " + e);
                    }
                    try
                    {
                        outputStream = serialPort.getOutputStream();
                    }
                    catch (IOException e)
                    {
                        System.out.println("Error writing to output stream " + e);
                    }
                    try
                    {
                        serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    }
                    catch (UnsupportedCommOperationException e)
                    {
                    }
                    try
                    {
                        outputStream.write(line1.getBytes());
                        outputStream.write(line1.getBytes());
                        outputStream.write(line2.getBytes());
                        outputStream.write(line3.getBytes());
                        outputStream.write(26);
                        outputStream.flush();
                    }
                    catch (Exception e)
                    {
                        System.out.println("Error writing message " + e);
                    }
                }
            }
        }
    }

    /** show text in the text window
     * @param Text text string to show on the display
     */
    public static void showText(String Text)
    {
        System.out.println(Text);
    }
}

这篇关于如何使用Java android代码发送许多短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆