直接贴代码吧
activity代码:
package cn.com.sgcc.eshop.ui.activity.smscheck;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.view.View; import android.widget.EditText; import android.widget.Toast; import cn.com.sgcc.eshop.R; import cn.com.sgcc.eshop.model.smscheck.SmsCheck; import cn.com.sgcc.eshop.service.smscheck.SmsService; import cn.com.sgcc.eshop.service.smscheck.SmsService.LocalBinder; import cn.com.sgcc.eshop.transfer.TransferResult; import cn.com.sgcc.eshop.transfer.TransferStatus; import cn.com.sgcc.eshop.transfer.smscheck.SmsCheckExcutor; public class SmsCheckActivity extends Activity {
private SmsCheckExcutor smsCheckExcutor; private String phoneNum; private String senderNum; private String verifyCode; private String id; boolean mBound = false; private SmsService mService; private Handler smsHandler = new Handler() { @Override public void handleMessage(final Message msg) { Bundle bundle = msg.getData(); senderNum = bundle.getString("senderNum"); verifyCode = bundle.getString("verifyCode"); EditText text = (EditText)findViewById(R.id.verifyCode); text.setText(verifyCode); // EditText phoneNumText = (EditText)findViewById(R.id.phoneNum); // phoneNumText.setText(senderNum); } }; /** 定交ServiceConnection,用于绑定Service的 */ private ServiceConnection mConnection = new ServiceConnection() { @Override
public void onServiceConnected(ComponentName className, IBinder service) { // 已经绑定了SmsService,强转IBinder对象,调用方法得到SmsService对象 LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true; } @Override
public void onServiceDisconnected(ComponentName arg0) { mBound = false; } }; @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.smscheck_activity); initBean(); }
@Override
protected void onStart() { super.onStart(); // 绑定Service,绑定后就会调用mConnetion里的onServiceConnected方法 Intent intent = new Intent(this, SmsService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override
protected void onStop() { super.onStop(); // 解绑Service,这样可以节约内存 if (mBound) { unbindService(mConnection); mBound = false; } } private class smsLoadThread extends Thread{ @Override public void run() { while((verifyCode==null || verifyCode.equals(""))||(senderNum==null ||senderNum.equals(""))){ if(mBound){ senderNum = mService.getSenderNum(); verifyCode = mService.getVerifyCode(); } } Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("senderNum", senderNum); bundle.putString("verifyCode", verifyCode); msg.setData(bundle); smsHandler.sendMessage(msg); } } public void getVerifyCode(View view) {
EditText phoneNumText = (EditText) findViewById(R.id.phoneNum); // 得到手机号码 phoneNum = phoneNumText.getText() == null ? "" : phoneNumText.getText() .toString(); if (phoneNum == null || phoneNum.equals("")) { Toast.makeText(SmsCheckActivity.this, R.string.phoneNum, Toast.LENGTH_LONG).show(); return; } TransferResult result = new TransferResult(); SmsCheck smscheck = smsCheckExcutor.getVerifyCode(phoneNum, result); if (TransferStatus.TRANS_STATUS_FAILURE.equals(result.getResultCode()) || TransferStatus.BUSS_STATUS_FAILURE.equals(result .getBussCode())) { Toast.makeText(SmsCheckActivity.this, result.getBussMsg(), Toast.LENGTH_LONG).show(); } else { id = smscheck.getId(); new smsLoadThread().start(); Toast.makeText(SmsCheckActivity.this, R.string.findVerifyCodeSucc, Toast.LENGTH_LONG).show(); } } public void submitVerifycode(View view) {
EditText phoneNumText = (EditText) findViewById(R.id.phoneNum); EditText verifyCodeText = (EditText) findViewById(R.id.verifyCode); phoneNum = phoneNumText.getText() == null ? "" : phoneNumText.getText() .toString(); if (phoneNum == null || phoneNum.equals("")) { Toast.makeText(SmsCheckActivity.this, R.string.phoneNum, Toast.LENGTH_LONG).show(); return; } verifyCode = verifyCodeText.getText() == null ? "" : verifyCodeText .getText().toString(); if (verifyCode == null || verifyCode.equals("")) { Toast.makeText(SmsCheckActivity.this, R.string.verifycode, Toast.LENGTH_LONG).show(); return; } TransferResult result = new TransferResult();
smsCheckExcutor.submitVerifycode(id, phoneNum, verifyCode, result); if (TransferStatus.TRANS_STATUS_FAILURE.equals(result.getResultCode()) || TransferStatus.BUSS_STATUS_FAILURE.equals(result .getBussCode())) { Toast.makeText(SmsCheckActivity.this, result.getBussMsg(), Toast.LENGTH_LONG).show(); } else { Toast.makeText(SmsCheckActivity.this, R.string.submitVerifyCodeSucc, Toast.LENGTH_LONG).show(); } }
private void initBean() {
smsCheckExcutor = new SmsCheckExcutor(); } } Service代码:
package cn.com.sgcc.eshop.service.smscheck;
import android.app.Service;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.telephony.SmsMessage; public class SmsService extends Service implements Runnable {
private String senderNum = ""; private String verifyCode = ""; // 实例化自定义的Binder类 private final IBinder mBinder = new LocalBinder(); private SMSReceiver receiver; @Override public void run() { // TODO Auto-generated method stub }
@Override
public void onCreate() { receiver = new SMSReceiver();
IntentFilter filter = new IntentFilter( "android.provider.Telephony.SMS_RECEIVED"); this.registerReceiver(receiver, filter); new Thread(this).start(); super.onCreate(); } private class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(
"android.provider.Telephony.SMS_RECEIVED")) { // 建构一字符串 StringBuilder stringBuilder = new StringBuilder(""); // 接收由Intent传来的数据 Bundle bundle = intent.getExtras(); // 判断Intent是有数据 if (bundle != null) { // pdus为 android内置短信参数 // identifier,通过bundle.get("")返回一包含pdus的对象 Object[] smsObject = (Object[]) bundle.get("pdus"); // 构造短信对象 SmsMessage[] messages = new SmsMessage[smsObject.length]; for (int i = 0; i < smsObject.length; i++) { messages[i] = SmsMessage .createFromPdu((byte[]) smsObject[i]); } for (SmsMessage currentMessage : messages) { // 发送人的电话号码 senderNum = currentMessage .getDisplayOriginatingAddress(); // 信息内容 stringBuilder.append(currentMessage .getDisplayMessageBody()); } } if (senderNum == null || senderNum.equals("")) return; String message = stringBuilder.toString(); if (message == null || message.equals("") || !message.contains("sgcc.eshop")) return; verifyCode = message.substring(message.indexOf(":") + 1, message.indexOf(":") + 5); }
}
}
public class LocalBinder extends Binder {
public SmsService getService() { // 返回Activity所关联的Service对象,这样在Activity里,就可调用Service里的一些公用方法和公用属性 return SmsService.this; } } @Override
public IBinder onBind(Intent intent) { return mBinder; } public String getVerifyCode() { return verifyCode; } public void setVerifyCode(String verifyCode) {
this.verifyCode = verifyCode; } public String getSenderNum() {
return senderNum; } public void setSenderNum(String senderNum) {
this.senderNum = senderNum; } @Override
public void onDestroy() { this.unregisterReceiver(receiver);//取消注册的CommandReceiver super.onDestroy(); } }
layout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas./apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include
android:id="@+id/login_title_bar" layout="@layout/title_bar" /> <LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phoneNum" /> <EditText
android:id="@+id/phoneNum" android:layout_width="200dip" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/verifycode" /> <EditText
android:id="@+id/verifyCode" android:layout_width="130dip" android:layout_height="wrap_content" /> <Button
android:id="@+id/getVerifyCode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getVerifyCode" android:text="@string/getVerifyCode" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:onClick="submitVerifycode" android:text="@string/submitVerifycode" /> </LinearLayout> 上面这些代码加上服务端发短信的代码
package cn.com.sgcc.eshop.access.transfer.smscheck;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public class SendSmsHelper {
public static void sendSms(String phoneNum,String verifyCode) throws Exception {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://utf8.sms./"); post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");//在头文件中设置转码 NameValuePair[] data ={ new NameValuePair("Uid", "wmhyhn"),new NameValuePair("Key", "94c2ea524c097edde9af"),new NameValuePair("smsMob",phoneNum),new NameValuePair("smsText","您在sgcc.eshop中获得的验证码是:"+verifyCode)}; post.setRequestBody(data); client.executeMethod(post);
Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println("statusCode:"+statusCode); for(Header h : headers) { System.out.println(h.toString()); } String result = new String(post.getResponseBodyAsString().getBytes("utf8")); System.out.println(result); post.releaseConnection(); } public static void main(String[] args)throws Exception
{ SendSmsHelper.sendSms("2323","15210716951"); }
}
就是一个验证码的实例
|
|