发送短信是手机上最基础的功能,那么他是如何实现 的呢
首先建立一个android的项目,打开Layout下的main.xml文件,拖控件,得到的效果是
然后编辑activity下的.java文件,代码如下
<p>package com.ixmoe.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.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SmsPhoneActivity extendsActivity {
/** Called when the activity is first created. */
privateButton Btn;
privateEditText editphone,editsms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Btn= (Button)findViewById(R.id.smsBtn);
editphone=(EditText) findViewById(R.id.callphone);
editsms=(EditText) findViewById(R.id.editsms);
Btn.setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
Stringphone=editphone.getText().toString();
Stringsms=editsms.getText().toString();
SmsManagersmsManager=SmsManager.getDefault();
PendingIntentpendingIntent=PendingIntent.getBroadcast(SmsPhoneActivity.this, 0, newIntent(), 0);
if(sms.length()>70){
List<String>texts=smsManager.divideMessage(sms);
for(Stringstr:texts){
smsManager.sendTextMessage(phone,null, str, pendingIntent, null);
}
}else{
smsManager.sendTextMessage(phone,null, sms, pendingIntent, null);
}
Toast.makeText(SmsPhoneActivity.this,"发送成功",Toast.LENGTH_LONG).show();
}
});
}
}</p>
Activity动作写完之后,就是授权,打开anroidMainfest.xml文件进入建立用户界面如下
授权之后运行项目,试试效果吧