先欣赏下界面:
页面布局xml文件:
<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="wrap_content" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <!-- 相对布局 ,显示收件人及号码输入框--> <RelativeLayout android:id="@+id/relativeLayoutIN" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/activity_vertical_margin" android:layout_marginBottom="@dimen/activity_vertical_margin" > <TextView android:id="@+id/shoujianrenText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/shoujianren" /> <EditText android:id="@+id/shoujianren" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/shoujianrenText" android:hint="@null" /> </RelativeLayout> <!-- 线性布局,显示信息内容--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/neirong" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/test" android:minLines="3" android:id="@+id/neirong"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/button" /> </LinearLayout>
编写业务处理代码:
package com.yangzi.sms; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.telephony.gsm.SmsManager; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; @SuppressWarnings("deprecation") public class MainActivity extends Activity { private Button button ; private EditText numtext; private EditText nrtext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到各个组件 button = (Button) this.findViewById(R.id.button); numtext = (EditText) this.findViewById(R.id.shoujianren); nrtext = (EditText) this.findViewById(R.id.neirong); //为按钮设置监听对象 button.setOnClickListener(new ButtonClickListener()); } @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; } private final class ButtonClickListener implements OnClickListener{ @SuppressLint("ShowToast") @Override public void onClick(View arg0) { String number = numtext.getText().toString(); String content = nrtext.getText().toString(); //得到消息管理对象 SmsManager manager = SmsManager.getDefault(); //若短信内容太长,需要拆分 ArrayList<String> texts = manager.divideMessage(content); for(String text:texts){ manager.sendTextMessage(number, null, text, null, null); } Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); } } }
添加权限:
<uses-permission android:name="android.permission.SEND_SMS"/>