`

SmsManager发送短信

 
阅读更多

添加发送短信的权限

 

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

	EditText mobileView;
	EditText contentView;
	Button send;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_message);
        
        mobileView = (EditText)findViewById(R.id.mobile);
        contentView = (EditText)findViewById(R.id.content);
        send = (Button)findViewById(R.id.button);
        
    	send.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				
				String mobile = mobileView.getText().toString();
		    	String content = contentView.getText().toString();
				SmsManager smsManager = SmsManager.getDefault();
		    	PendingIntent sentIntent = PendingIntent.getBroadcast(SendMessage.this, 0, new Intent(), 0);
		    	if(content.length()>70){//如果字数超过70,需拆分成多条短信发送,设为150也是按照70发送
		    		List<String> msgs = smsManager.divideMessage(content);
		    		for(String msg : msgs){
				    smsManager.sendTextMessage(mobile, null, msg, sentIntent, null);
		    	 //最后二个参数为短信已发送的广播意图,最后一个参数为短信对方已收到短信的广播意图
				    }
		    	}else{
		    		smsManager.sendTextMessage(mobile, null, content, sentIntent, null);
		    	}
		    	Toast.makeText(SendMessage.this, "短信发送完成", Toast.LENGTH_LONG).show();
				
			}
    		
    	});
    	
    }
 


 
  • 大小: 69.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics