效果如下:
我们经常在应用中有这样的需要:发送短信邀请其他人使用。当然,你可以调用系统发送短信的界面,也可以在自己的应用程序中进行发送,并写入短信数据库(如果不写,那么发送短信后,短信数据库中是没有记录的)。今天我们就来看看如何实现。
下面直接上代码:
/**
* This demo shows how to send message in self application.
*
* 这个demo展示了如何在自己的程序中发送短信
*
* 参考:http://stackoverflow.com/questions/8447735/android-sms-type-constants
* MESSAGE_TYPE_ALL = 0;//发送(和2一个效果)
* MESSAGE_TYPE_INBOX = 1;//接收
* MESSAGE_TYPE_SENT = 2;//发送
* MESSAGE_TYPE_DRAFT = 3;//存在草稿箱中
* MESSAGE_TYPE_OUTBOX = 4;//待发箱(和发送中一个效果)
* MESSAGE_TYPE_FAILED = 5; // for failed outgoing messages发送失败
* MESSAGE_TYPE_QUEUED = 6; // for messages to send later//发送中
*
* ContentValues values = new ContentValues();
* values.put("address", "13023895555");
* values.put("body", "short message content");
* values.put("date", "1322039220502");
* values.put("type", "1");
* values.put("status", "-1");
* values.put("read", "1");
* values.put("protocol", "0");
* getContentResolver().insert(Uri.parse("content://sms"), values);
*
*
* @author MichaelYe
* @since 2012-8-30
*
* */
public class MainActivity extends Activity
{
private EditText etNumber;
private EditText etSmsContent;
private Button btnSend;
private Button btnCancel;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNumber = (EditText)findViewById(R.id.et_number);
etSmsContent = (EditText)findViewById(R.id.et_sms_content);
btnSend = (Button)findViewById(R.id.btn_send);
btnCancel = (Button)findViewById(R.id.btn_cancel);
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber = etNumber.getText().toString().trim();
if(phoneNumber.equals(""))
{
Toast.makeText(MainActivity.this, "Number can not be empty!", Toast.LENGTH_SHORT).show();
return;
}
else
{
String smsContent = etSmsContent.getText().toString().trim();
sendSms(phoneNumber, smsContent);
writeToDataBase(phoneNumber, smsContent);
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
/**
* send sms
*
* 发送短信
*
* */
private void sendSms(String phoneNumber, String smsContent)
{
//当文本超过限定字符长度的时候(中文70,英文160),在2.2中会nullpoint,4.1.1中发送无效
//SmsManager smsManager = SmsManager.getDefault();
//smsManager.sendTextMessage(phoneNumber, null, smsContent, null, null);
//改为sendMultipartTextMessage()
ArrayList<String> messageArray = smsManager.divideMessage(smsContent);
smsManager.sendMultipartTextMessage(phoneNumber, null, messageArray, null, null);
Toast.makeText(this, "Send Success", Toast.LENGTH_LONG).show();
}
/**
* write to database
*
* 写入数据库
*
* */
private void writeToDataBase(String phoneNumber, String smsContent)
{
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", smsContent);
values.put("type", "2");
values.put("read", "1");//"1"means has read ,1表示已读
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
}
}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/number_hint"
android:inputType="number"
android:paddingLeft="20dip"
android:paddingRight="20dip" />
<EditText
android:id="@+id/et_sms_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_number"
android:hint="@string/sms_hint"
android:paddingLeft="20dip"
android:paddingRight="20dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_sms_content"
android:orientation="horizontal"
android:paddingLeft="20dip"
android:paddingRight="20dip" >
<Button
android:id="@+id/btn_send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:text="@string/btn_send" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:text="@string/btn_cancel" />
</LinearLayout>
</RelativeLayout>
注意权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
注意:测试的时候可以新建两个模拟器测试下,模拟器左上角的端口号就是电话号码。比如:5554
工程下载地址:
https://github.com/michaelye/DemoSendSms