Android短信应用——发送短信
Android短信应用——发送短信
前几天写了一个关于实时获取短信的文章(见 ),后来想到以前写的一个有发短信功能的工程,想到其中的好处让我直流口水,今天就说说有关如何通过代码实现短信发送。
当时写完后测试,发现最大的好处就是短信发出去后,在发件箱中没有“迹象”;也就是说,只要用户咨询通信服务商,他是不会知道我们偷偷做了些什么……
(只是涉及权限了,于是乎,豌豆荚先生就会毫不留情的把你的程序牵扯到的权限,毫无保留的告诉用户……唉!)
下面来看看代码吧。
先展示下布局。
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout Android:id="@+id/widget40"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView android:id="@+id/tel_num_send" android:layout_width="wrap_content"
- android:layout_height="25px" android:text="Tel Number:"
- android:layout_x="10px" android:layout_y="22px">
- </TextView>
- <EditText android:id="@+id/telNumText_send"
- android:layout_width="197px" android:layout_height="35px"
- android:text="" android:textSize="18sp" android:layout_x="90px"
- android:layout_y="12px">
- </EditText>
- <EditText android:id="@+id/message_copntent_send"
- android:layout_width="319px" android:layout_height="86px"
- android:text="" android:textSize="18sp" android:layout_x="0px"
- android:layout_y="112px">
- </EditText>
- <Button android:id="@+id/send_button_send" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="Send"
- android:layout_x="120px" android:layout_y="212px">
- </Button>
- <TextView android:id="@+id/message_content_text_send"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:text="Input Message" android:layout_x="10px" android:layout_y="82px">
- </TextView>
- </AbsoluteLayout>
我大概画了一个短信发送页面,比较简陋,各位一笑而过即可……其中主要包含一个发送号码编辑窗口,一个发送内容编辑窗口以及一个发送按钮。
布局预览
接下来是功能实现部分代码
- // 短信发送
- SmsManager smsMgr = SmsManager.getDefault();
- Intent i = new Intent("cn.etzmico.smssending");
- PendingIntent dummyEvent = PendingIntent.getBroadcast(
- SMSSending.this, 0, i, 0);
- try {
- smsMgr.sendTextMessage(telNumStr, null, messageStr,
- dummyEvent, dummyEvent);
- } catch (Exception e) {
- Log.e("SmsSending", "SendException", e);
- }
评论暂时关闭