短信发送器

来源:互联网 发布:淳化阁帖 知乎 编辑:程序博客网 时间:2024/06/05 20:46

java代码:

public class MainActivity extends ActionBarActivity implements OnClickListener{private EditText number;private EditText content;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
number=(EditText) findViewById(R.id.number);<span style="color:#33cc00;">//输入号码框</span>content=(EditText) findViewById(R.id.content);//内容输入框Button bt=(Button) findViewById(R.id.button);bt.setOnClickListener(this);//给按钮添加点击事件}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:String num=number.getText().toString().trim();//得到号码String con=content.getText().toString().trim();//得到内容if(TextUtils.isEmpty(num)||TextUtils.isEmpty(con)){Toast.makeText(this, "内容或者号码不能为空", Toast.LENGTH_SHORT).show();return;}else{SmsManager smsManager=SmsManager.getDefault();ArrayList<String> contents=smsManager.divideMessage(con);for(String str:contents){smsManager.sendTextMessage(num, null, str, null,null);<span style="white-space:pre"></span>      //发送到 从哪   内容 反馈 反馈}}break;default:break;}}}

布局文件:

 <TextView        android:id="@+id/tv_number"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="请输入号码"        android:textSize="15dp"        android:textColor="#ff8700" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/content"        android:layout_below="@+id/content"        android:layout_marginTop="13dp"        android:text="发送"        android:textColor="#ff000998" />    <EditText        android:id="@+id/number"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignLeft="@id/tv_number"        android:layout_below="@id/tv_number"        android:layout_marginTop="12dp"        android:ems="10"        android:inputType="phone"        android:singleLine="true" />    <TextView        android:id="@+id/tv_content"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/number"        android:layout_below="@+id/number"        android:layout_marginTop="10dp"        android:text="请输入内容"        android:textColor="#ff188172"        android:textSize="15dp" />    <EditText        android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/tv_content"        android:layout_below="@+id/tv_content"        android:layout_marginTop="16dp"        android:ems="10"        android:inputType="textMultiLine"        android:lines="5" />




最后加上 发短信的权限:

0 0