首页 > 代码库 > android短信发送器源代码

android短信发送器源代码

Activity类:

import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class SmsActivity extends Activity {
 private EditText phoneText; 
 private EditText contentText;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        phoneText=(EditText)findViewById(R.id.phoneText);
        contentText=(EditText)findViewById(R.id.contentText);
       
        sendSms();
    }
   
    public void sendSms(){      
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new SmsOnClick());
    }
   
    private final class SmsOnClick implements OnClickListener{
  @Override
  public void onClick(View v) {
   String phonenumber=phoneText.getText().toString();
   String content=contentText.getText().toString();
   
   if(phonenumber==null||phonenumber.length()<1){
    Toast.makeText(SmsActivity.this, R.string.empty, Toast.LENGTH_SHORT).show();
   }else{
    SmsManager smsManager = SmsManager.getDefault();
    PendingIntent sentIntent = PendingIntent.getBroadcast(SmsActivity.this,0, new Intent(), 0);
    if (content.length() > 70) {// 如果字数超过70,需拆分成多条短信发送
     List<String> msgs = smsManager.divideMessage(content);
     for (String msg : msgs) {
      smsManager.sendTextMessage(phonenumber, null, msg, sentIntent, null);
      // 最后二个参数为短信已发送的广播意图,最后一个参数为短信对方已收到短信的广播意图
     }
    } else {
     smsManager.sendTextMessage(phonenumber, null, content, sentIntent, null);
    }
   }
  }     
    }
}

 

Manifest添加sms permission

<uses-permission android:name="android.permission.SEND_SMS"/>

<iframe id="google_ads_frame5" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402506242&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3764833.html&dt=1402506243936&shv=r20140603&cbv=r20140417&saldr=sb&correlator=1402506243702&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=64910759&ga_fc=1&u_tz=480&u_his=466&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=0&ady=2171&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=5&xpc=a0nD0OOCUJ&p=http%3A//www.cnblogs.com&dtd=44" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame5" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame6" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402506242&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3764833.html&dt=1402506243988&shv=r20140603&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1402506243702&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=64910759&ga_fc=1&u_tz=480&u_his=466&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=306&ady=2421&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=6&xpc=9SxhvevLIm&p=http%3A//www.cnblogs.com&dtd=58" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame6" marginWidth="0" scrolling="no" hspace="0"></iframe>