Android短信发送器

<!--因为应用要使用手机的短信服务,所以要在清单文件AndroidManifest.xml中添加短信服务权限:-->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.sms"
      android:versionCode="1"
      android:versionName="1.0">
     略....
     <uses-sdk android:minSdkVersion=“4" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>

界面布局:

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical“ android:layout_width="fill_parent“ android:layout_height="fill_parent" >

   <TextViewandroid:layout_width="fill_parent" android:layout_height="wrap_content"

   android:text="@string/inputmobile"/>

   

   <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"

   android:id="@+id/mobile"/>

 

  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"

   android:text="@string/content"/>

   

   <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"

   android:minLines="3"

   android:id="@+id/content"/>

  

   <Button android:layout_width="wrap_content" android:layout_height="wrap_content"

   android:text="@string/button"

   android:id="@+id/button"/>

</LinearLayout>


Activity主要代码:

Stringmobile = mobileView.getText().toString();

  Stringcontent = contentView.getText().toString();

   SmsManager smsManager = SmsManager.getDefault();

   PendingIntent sentIntent = PendingIntent.getBroadcast(SMSSender.this, 0, new Intent(), 0);

  if(content.length()>70){//如果字数超过70,需拆分成多条短信发送

             List<String> msgs =smsManager.divideMessage(content);

            for(String msg : msgs){

      smsManager.sendTextMessage(mobile, null, msg, sentIntent, null);

   //最后二个参数为短信已发送的广播意图,最后一个参数为短信对方已收到短信的广播意图

             }

  }else{           

             smsManager.sendTextMessage(mobile, null, content, sentIntent, null);

  }

  Toast.makeText(SMSSender.this, "短信发送完成", Toast.LENGTH_LONG).show();



发布了11 篇原创文章 · 获赞 8 · 访问量 17万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章