Android 中发送和接收短信
2010-05-10 19:44:00 来源:WEB开发网38: android:layout_width="fill_parent"
39: android:layout_height="wrap_content"
40: android:gravity="center"
41: android:id="@+id/btnSend"
42: android:paddingTop="20sp"
43: / >
44: < /LinearLayout >
3.创建一个Java类文件,导入以下包:
1: import java.util.regex.Matcher;
2: import java.util.regex.Pattern;
3: import com.eshore.smsManagers.R;
4: import android.app.Activity;
5: import android.telephony.gsm.*;
6: import android.view.View;
7: import android.view.View.OnClickListener;
8: import android.widget.Button;
9: import android.widget.EditText;
10: import android.widget.Toast;
11: import android.os.Bundle;
4.重写onCreate方:
1: txtFrom=(EditText)this.findViewById(R.id.txt_from);
2: txtContent=(EditText)this.findViewById(R.id.txt_content);
3: btnSend=(Button)this.findViewById(R.id.btnSend);
4: btnSend.setOnClickListener(new OnClickListener() {
5: public void onClick(View v) {
6: if(!validate())
7: return;
8: SmsManager.getDefault().sendTextMessage(txtFrom.getText().toString().trim(),null,txtContent.getText().toString(),null,null);
9: txtFrom.setText("");
10: txtContent.setText("");
11: Toast toast=Toast.makeText(main.this, "短信发送成功!",Toast.LENGTH_LONG);
12: toast.show();
13: }
14: });
相关的辅助方法有手机的合法性验证和短信内容不可为空:
1: //合法性验证
2: private boolean validate(){
3: String mobile=txtFrom.getText().toString().trim();
4: String content=txtContent.getText().toString();
5: if(mobile.equals("")){
6: Toast toast=Toast.makeText(this, "手机号码不能为空!",Toast.LENGTH_LONG);
更多精彩
赞助商链接