android实现发送短信demo

main.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" /> <EditText android: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设置出短信内容最小三行 --> <EditText android:id="@+id/sms" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" /> <Button android: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. */ @Override public 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 { @Override public void onClick(View v) { // TODO Auto-generated method stub String 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); } } } 

你可能感兴趣的:(android,layout,null,button,sms,encoding)