android实现发送短信demo时间:2023-01-28 08:57:14main.xml <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/inputnumber" /><EditTextandroid:id="@+id/number"android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true"/><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/inputsms" /><!--minlines设置出短信内容最小三行 --><EditTextandroid:id="@+id/sms"android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3"/><Buttonandroid:id="@+id/send"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/send"/></LinearLayout> AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wide.sms" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <!-- 设置发送短信的权限 --> <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SMSActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest> SMSActivity.java package com.wide.sms;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SMSActivity extends Activity {private EditText et_number = null;private EditText sms = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);et_number = (EditText) findViewById(R.id.number);sms = (EditText) findViewById(R.id.sms);Button btn_send = (Button) findViewById(R.id.send);btn_send.setOnClickListener(new sendListener());}class sendListener implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString number = et_number.getText().toString();String smsContent = sms.getText().toString();SmsManager smsManager = SmsManager.getDefault();if(smsContent.length()>70){List<String> contents = smsManager.divideMessage(smsContent);for(String c:contents){smsManager.sendTextMessage(number, null, c, null, null);}}else{smsManager.sendTextMessage(number, null, smsContent, null, null);}Toast.makeText(SMSActivity.this, "短信发送成功", Toast.LENGTH_LONG);}}}