android系统发送短信息代码

来源:互联网 发布:菲律宾网络彩票合法吗 编辑:程序博客网 时间:2024/05/16 05:23

 1.首先搭建Activity界面,


<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >


    <TextView
        android:id="@+id/tv_tip_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tv_tip_phone" />


    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_tip_phone"
        android:inputType="phone"
        />
    <TextView
        android:id="@+id/tv_tip_sms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_phone"
        android:text="@string/tv_sms"
        />
      <EditText
        android:id="@+id/et_sms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_tip_sms"
        android:minLines="4"
        android:inputType="text"
        />


      <Button
          android:layout_width="wrap_content"
          android:layout_height="100dp"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/et_sms"
          android:layout_marginTop="54dp"
          android:text="@string/btn_send_sms" />


</RelativeLayout>


2.添加权限


    <!-- 拨打电话权限 -->
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <!-- 发送短息权限 -->
    <uses-permission android:name="android.permission.SEND_SMS" />



3.编写代码:



public class SmsActivity extends Activity {

private EditText tv_phone;
private EditText tv_sms;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);

tv_phone= (EditText)findViewById(R.id.tv_tip_phone);
tv_sms= (EditText)findViewById(R.id.tv_tip_sms);

}
 
private void CallSms(View v) {
// TODO Auto-generated method stub


String phone = tv_phone.getText().toString();
String sms = tv_sms.getText().toString();
if (TextUtils.isEmpty(phone)) {
Toast.makeText(this, "请输入电话号码!", Toast.LENGTH_LONG).show();
if(TextUtils.isEmpty(sms)){
Toast.makeText(this, "请输入电话号码!", Toast.LENGTH_LONG).show();
}else{
SmsManager smsManager = SmsManager.getDefault();
List<String> smsMessages = smsManager.divideMessage(sms);
for(String smsmg:smsMessages){

smsManager.sendTextMessage(phone, null, smsmg,null, null);
Toast.makeText(this, "发送成功",Toast.LENGTH_LONG).show();
}
}

}
}
}


0 0