如何在Blackberry中发送短信? [英] How to send Text Messages in Blackberry?
本文介绍了如何在Blackberry中发送短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Blackberry中发送短信?
我正在尝试使用此代码,但无法发送短信:
How to send Text Messages in Blackberry?
i am trying this code but unable to send text messages:
<pre lang="xml">Dialog.inform("SEND SMS BLOCK: \n phno:"+ph_no+"MESSAGE :"+ message );
String addr = "sms://"+ph_no;
Dialog.inform("<<<<<<<<<ADDR>>>>>>>>> : "+addr);
MessageConnection conn = null;
Dialog.inform("<<<<<<<<<CONN>>>>>>>>> : "+conn);
try
{
conn = (MessageConnection) Connector.open("sms://");
Dialog.inform("<<<<<<<<<CONN in try catch>>>>>>>>> : "+conn);
//generate a new text message
TextMessage msgOut = (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
Dialog.inform("<<<<<<<<<MSG OUT>>>>>>>>> : "+msgOut);
// TextMessage txtMsg = (TextMessage) message;
msgOut.setPayloadText(message);
msgOut.setAddress("sms://"+ph_no);
Dialog.inform("<<<<<<<<< Address ======== : "+msgOut.getAddress());
Dialog.inform("======== MESSAGE >>>>>>>>> : "+msgOut.getPayloadText());
Dialog.inform("<<<<<<<<<Message>>>>>>>>> : "+message);
try
{
conn.send(msgOut);
}
catch (Exception e)
{
Dialog.inform("><><>><>><>><><>><><><>><><>< : " + e.toString());
}
}
catch(IOException e)
{
Dialog.inform("+++++++++++++++++++++"+e.toString());
System.out.println("+++++++++++++++++++++"+e.toString());
}
finally
{
try
{
conn.close();
conn = null;
}
catch(IOException e)
{
Dialog.inform("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"+e.toString());
System.out.println("<<<<<<<<<<<<<<>>>>>>>>>>>>>>"+e.toString());
}
}
推荐答案
尝试以下发送文本消息的代码:
try this below code for sending text messages :
<small>public final class HelloBlackBerryScreen extends MainScreen
{
private BasicEditField basicEditField1, basicEditField2;
private String addr = "9888919119";
private String message = "This is a test message.";
public HelloBlackBerryScreen()
{
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
setTitle("Send SMS :");
basicEditField1 = new BasicEditField("To : ", addr, 11, BasicEditField.EDITABLE);
basicEditField2 = new BasicEditField("Message : ", message, 500, BasicEditField.EDITABLE);
ButtonField buttonField_1 = new ButtonField("Send", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
buttonField_1.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field arg0, int arg1)
{
try
{
addr = basicEditField1.getText();
message = basicEditField2.getText();
sendSMS(addr, message);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error send msg : " + e.toString());
}
}
});
this.add(basicEditField1);
this.add(basicEditField2);
this.add(buttonField_1);
}
private void sendSMS(final String no, final String msg)
{
try
{
new Thread()
{
public void run()
{
if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA)
{
DatagramConnection dc = null;
try
{
dc = (DatagramConnection) Connector.open("sms://" + no);
byte[] data = msg.getBytes();
Datagram dg = dc.newDatagram(dc.getMaximumLength());
dg.setData(data, 0, data.length);
dc.send(dg);
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
try
{
System.out.println("Message Sent Successfully : Datagram");
Dialog.alert("Message Sent Successfully : Datagram");
}
catch (Exception e)
{
System.out.println("Exception **1 : " + e.toString());
e.printStackTrace();
}
}
});
}
catch (Exception e)
{
System.out.println("Exception 1 : " + e.toString());
e.printStackTrace();
}
finally
{
try
{
dc.close();
dc = null;
}
catch (IOException e)
{
System.out.println("Exception 2 : " + e.toString());
e.printStackTrace();
}
}
}
else
{
MessageConnection conn = null;
try
{
conn = (MessageConnection) Connector.open("sms://" + no);
//generate a new text message
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set the message text and the address
tmsg.setAddress("sms://" + no);
tmsg.setPayloadText(msg);
//finally send our message
conn.send(tmsg);
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
try
{
System.out.println("Message Sent Successfully : TextMessage");
Dialog.alert("Message Sent Successfully : TextMessage");
}
catch (Exception e)
{
System.out.println("Exception **1 : " + e.toString());
e.printStackTrace();
}
}
});
}
catch (Exception e)
{
System.out.println("Exception 3 : " + e.toString());
e.printStackTrace();
}
finally
{
try
{
conn.close();
conn = null;
}
catch (IOException e)
{
System.out.println("Exception 4 : " + e.toString());
e.printStackTrace();
}
}
}
}
}.start();
}
catch (Exception e)
{
System.out.println("Exception 5 : " + e.toString());
e.printStackTrace();
}
}
protected boolean keyDown(int keycode, int status)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
close();
return true;
}
return super.keyDown(keycode, status);
}
}</small>
这篇关于如何在Blackberry中发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文