Android短信应用——发送短信


前几天写了一个关于实时获取短信的文章(见 ),后来想到以前写的一个有发短信功能的工程,想到其中的好处让我直流口水,今天就说说有关如何通过代码实现短信发送。

当时写完后测试,发现最大的好处就是短信发出去后,在发件箱中没有“迹象”;也就是说,只要用户咨询通信服务商,他是不会知道我们偷偷做了些什么……

(只是涉及权限了,于是乎,豌豆荚先生就会毫不留情的把你的程序牵扯到的权限,毫无保留的告诉用户……唉!)

下面来看看代码吧。

先展示下布局。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <AbsoluteLayout Android:id="@+id/widget40"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <TextView android:id="@+id/tel_num_send" android:layout_width="wrap_content"  
  6.         android:layout_height="25px" android:text="Tel Number:"  
  7.         android:layout_x="10px" android:layout_y="22px">  
  8.     </TextView>  
  9.     <EditText android:id="@+id/telNumText_send"  
  10.         android:layout_width="197px" android:layout_height="35px"  
  11.         android:text="" android:textSize="18sp" android:layout_x="90px"  
  12.         android:layout_y="12px">  
  13.     </EditText>  
  14.     <EditText android:id="@+id/message_copntent_send"  
  15.         android:layout_width="319px" android:layout_height="86px"  
  16.         android:text="" android:textSize="18sp" android:layout_x="0px"  
  17.         android:layout_y="112px">  
  18.     </EditText>  
  19.     <Button android:id="@+id/send_button_send" android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content" android:text="Send"  
  21.         android:layout_x="120px" android:layout_y="212px">  
  22.     </Button>  
  23.     <TextView android:id="@+id/message_content_text_send"  
  24.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  25.         android:text="Input Message" android:layout_x="10px" android:layout_y="82px">  
  26.     </TextView>  
  27. </AbsoluteLayout>  

我大概画了一个短信发送页面,比较简陋,各位一笑而过即可……其中主要包含一个发送号码编辑窗口,一个发送内容编辑窗口以及一个发送按钮。

布局预览



接下来是功能实现部分代码

  1. // 短信发送   
  2.                     SmsManager smsMgr = SmsManager.getDefault();  
  3.                     Intent i = new Intent("cn.etzmico.smssending");  
  4.                     PendingIntent dummyEvent = PendingIntent.getBroadcast(  
  5.                             SMSSending.this0, i, 0);  
  6.                     try {  
  7.                         smsMgr.sendTextMessage(telNumStr, null, messageStr,  
  8.                                 dummyEvent, dummyEvent);  
  9.                     } catch (Exception e) {  
  10.                         Log.e("SmsSending""SendException", e);  
  11.                     }  

相关内容