android发送短信
发送短信
首先建立一个android的项目,打开Layout下的main.xml文件,拖控件,得到的效果是
然后编辑activity下的.java文件,代码如下
package com.csdn.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();
}
});
}
}
Activity动作写完之后,就是授权,,打开anroidMainfest.xml文件进入建立用户界面如下
授权之后运行项目
出现如下界面
当输入号码输入好短信的信息后,点击发送短信,5554界面如下
5556的界面如下
打开信息;