在main.xml中:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="发送短信的程序"
android:textColor="#ffffff"
tools:context=".MySMSDemo" />
</RelativeLayout>
在MySMSDemo.java中:
package com.li.smsproject;
import java.util.Iterator;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.telephony.gsm.SmsManager;
public class MySMSDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
String content = "陆逊字伯言,吴郡吴人也。"+"本名仪,世江东大族。逊少孤,随从祖庐江太守康在官。"+
"袁术与康有隙,将攻康,康遣逊及亲戚还吴。"; //超过了70个字
android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getActivity(this, 0, super
.getIntent(), PendingIntent.FLAG_CANCEL_CURRENT);
if(content.length() > 70){ //短信是否大于70字,是就拆分短信
List<String> msgs = smsManager.divideMessage(content); //拆分
Iterator<String> iter = msgs.iterator();
while(iter.hasNext()){
String msg = iter.next();
smsManager.sendTextMessage("5554", null, msg, sentIntent, null);
}
}
else{
smsManager.sendTextMessage("5554", null, content, sentIntent, null);
}
Toast.makeText(this, "短信发送完成", Toast.LENGTH_SHORT).show();
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.smsproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MySMSDemo"
android:label="@string/title_activity_my_smsdemo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>