实现原理:调用系统短信发送功能,需要注意如果短信内容非常多需要对短信内容进行拆分,拆分后存到集合里,对短信内容多条发送
布局界面:以线性布局为主,里面有TextView,EditText,Button
具体代码:
public class MainActivity extends Activity implements OnClickListener{
private Button send;
private EditText number;
private EditText content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number=(EditText) this.findViewById(R.id.number);
content=(EditText) this.findViewById(R.id.content);
send=(Button) this.findViewById(R.id.send);
send.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.send:
String phoneNumber=number.getText().toString().trim();
String phoneContent=content.getText().toString().trim();
SmsManager manager=SmsManager.getDefault();
ArrayList<String> texts=manager.divideMessage(phoneContent);
for(String text : texts){
manager.sendTextMessage(phoneNumber, null, text, null, null);
}
Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
break;
}
}
}
布局界面:
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number" />
<EditText
android:id="@+id/number"
android:layout_width="fill_parent"
android:hint="@string/number"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content" />
<EditText
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/content"
android:minLines="3"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"/>
</LinearLayout>
下载地址:
http://download.csdn.net/detail/u010074054/8468045