如图是项目的结构,结合了分享,发送短信,读取短信内容的功能
代码地址:
1.SMS发送短信 主要是3部
第一步:初始化SDK initSDK
第二步:注册回调接口 registerEventHandler(EventHandler handler)
第三步:注销回调接口 unregisterEventHandler(EventHandler handler)
如下是程序的主要代码
实现了点击获取验证码,出现倒计时,读取短信的,验证验证码的一套登录流程
1 package com.item.jiejie; 2 3 import android.Manifest; 4 import android.content.pm.PackageManager; 5 import android.net.Uri; 6 import android.os.CountDownTimer; 7 import android.os.Handler; 8 import android.os.Message; 9 import android.support.annotation.NonNull; 10 import android.support.v7.app.AppCompatActivity; 11 import android.os.Bundle; 12 import android.text.Editable; 13 import android.text.TextUtils; 14 import android.text.TextWatcher; 15 import android.view.View; 16 import android.widget.Button; 17 import android.widget.EditText; 18 import android.widget.Toast; 19 20 import org.json.JSONObject; 21 22 import java.lang.ref.SoftReference; 23 24 import cn.smssdk.EventHandler; 25 import cn.smssdk.SMSSDK; 26 27 28 public class SMSActivity extends AppCompatActivity implements TextWatcher { 29 30 private EditText edtPhone; // 输入的手机号 31 private EditText edtCode; // 输入的验证码 32 private Button btnCode; // 获取验证码 33 private Button btnLogin; // 登录 34 35 private MyHandler myHandler; 36 private SMSContentObserver smsContentObserver; // 回调接口 37 private MyCountDownTimer countDownTimer = new MyCountDownTimer(60000, 1000); // 计时器 38 39 private static class MyHandler extends Handler { 40 SoftReference<SMSActivity> mActivity; 41 42 private MyHandler(SMSActivity activity) { 43 mActivity = new SoftReference<>(activity); 44 } 45 46 @Override 47 public void handleMessage(Message msg) { 48 super.handleMessage(msg); 49 SMSActivity activity = mActivity.get(); 50 if (activity == null) { 51 return; 52 } 53 switch (msg.what) { 54 case 1001: // 短信的返回 55 activity.edtCode.setText(msg.obj.toString()); 56 break; 57 case 1002: // 这个是EventHandler发送短信的返回 58 int event = msg.arg1; 59 int result = msg.arg2; 60 Object data = msg.obj; 61 if (result == SMSSDK.RESULT_COMPLETE) { // 回调完成 62 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { 63 // 提交验证码成功 64 Toast.makeText(activity, "提交验证码成功", Toast.LENGTH_SHORT).show(); 65 } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { 66 // 获取验证码成功 67 Toast.makeText(activity, "验证码发送成功", Toast.LENGTH_SHORT).show(); 68 activity.countDownTimer.start(); // 发送成功,开启倒计时 69 activity.setSmsPermission(); // 发送成功了,要开始读取短信了 70 } 71 } else { 72 // 报错了 73 try { 74 Throwable throwable = (Throwable) data; 75 JSONObject object = new JSONObject(throwable.getMessage()); 76 String des = object.optString("detail"); 77 if (!TextUtils.isEmpty(des)) { 78 Toast.makeText(activity, des, Toast.LENGTH_SHORT).show(); 79 } 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } 83 } 84 break; 85 } 86 } 87 } 88 89 /** 90 * 这里好像要加个动态的权限 91 */ 92 private void setSmsContentObserver() { 93 if (smsContentObserver == null) { 94 smsContentObserver = new SMSContentObserver(SMSActivity.this, myHandler); 95 getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsContentObserver); 96 } 97 } 98 99 /** 100 * 动态权限 101 */ 102 private void setSmsPermission() { 103 if (ToastUtils.hasPermission(this, Manifest.permission.READ_SMS)) { 104 setSmsContentObserver(); 105 } else { 106 ToastUtils.requestPermission(this, 0x01, Manifest.permission.READ_SMS); 107 } 108 } 109 110 @Override 111 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 112 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 113 if (requestCode == 0x01) { 114 if ((grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { 115 setSmsContentObserver(); 116 } 117 } 118 } 119 120 @Override 121 protected void onCreate(Bundle savedInstanceState) { 122 super.onCreate(savedInstanceState); 123 setContentView(R.layout.activity_sms); 124 edtPhone = (EditText) findViewById(R.id.edt_login_phone); 125 edtCode = (EditText) findViewById(R.id.edt_login_code); 126 btnCode = (Button) findViewById(R.id.btn_login_code); 127 btnLogin = (Button) findViewById(R.id.btn_login); 128 btnCode.setEnabled(false); 129 btnLogin.setEnabled(false); 130 edtPhone.addTextChangedListener(this); 131 myHandler = new MyHandler(this); 132 btnCode.setOnClickListener(new View.OnClickListener() { 133 @Override 134 public void onClick(View view) { 135 // 点击获取验证码,获取短信目前支持国家列表,在监听中返回 136 //getContentResolver().registerContentObserver(); 137 SMSSDK.getVerificationCode("86", edtPhone.getText().toString()); 138 } 139 }); 140 btnLogin.setOnClickListener(new View.OnClickListener() { 141 @Override 142 public void onClick(View view) { 143 // 提交验证码 submitVerificationCode(String country, String phone, String code) 144 SMSSDK.submitVerificationCode("86", edtPhone.getText().toString().trim(), edtCode.getText().toString().trim()); 145 } 146 }); 147 EventHandler mEventHandler = new EventHandler() { 148 @Override 149 public void afterEvent(int event, int result, Object data) { 150 Message msg = new Message(); 151 msg.what = 1002; 152 msg.arg1 = event; 153 msg.arg2 = result; 154 msg.obj = data; 155 myHandler.sendMessage(msg); 156 } 157 }; 158 SMSSDK.registerEventHandler(mEventHandler); 159 edtCode.addTextChangedListener(new TextWatcher() { 160 @Override 161 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 162 163 } 164 165 @Override 166 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 167 168 } 169 170 @Override 171 public void afterTextChanged(Editable editable) { 172 if (editable.length() >= 2 && edtPhone.getText().toString().length() >= 11) { 173 btnLogin.setEnabled(true); 174 } else { 175 btnLogin.setEnabled(false); 176 } 177 } 178 }); 179 } 180 181 @Override 182 protected void onDestroy() { 183 super.onDestroy(); 184 SMSSDK.unregisterAllEventHandler(); 185 countDownTimer.cancel(); // 取消定时器 186 if (smsContentObserver != null) { 187 getContentResolver().unregisterContentObserver(smsContentObserver); 188 } 189 } 190 191 @Override 192 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 193 194 } 195 196 @Override 197 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 198 199 } 200 201 @Override 202 public void afterTextChanged(Editable editable) { 203 if (editable.length() == 11) { 204 btnCode.setEnabled(true); 205 } else { 206 btnCode.setEnabled(false); 207 } 208 } 209 210 /** 211 * 定时器 212 */ 213 private class MyCountDownTimer extends CountDownTimer { 214 215 private MyCountDownTimer(long millisInFuture, long countDownInterval) { 216 super(millisInFuture, countDownInterval); 217 } 218 219 @Override 220 public void onTick(long l) { 221 btnCode.setEnabled(false); 222 btnCode.setText(l / 1000 + "s"); 223 } 224 225 @Override 226 public void onFinish() { 227 btnCode.setEnabled(true); 228 btnCode.setText("获取验证码"); 229 cancel(); 230 } 231 } 232 }
2.使用ShareSDK来实现分享功能
分享要注意ShareSDK.xml 的位置,还有些清单文件微信 qq等的回调要配置
参考官方的http://wiki.mob.com/%e5%88%86%e4%ba%ab%e5%88%b0%e6%8c%87%e5%ae%9a%e5%b9%b3%e5%8f%b0/
分享到指定平台的功能
ShareParams sp = new ShareParams(); sp.setTitle("测试分享的标题"); sp.setTitleUrl("http://sharesdk.cn"); // 标题的超链接 sp.setText("测试分享的文本"); sp.setImageUrl("http://www.someserver.com/测试图片网络地址.jpg"); sp.setSite("发布分享的网站名称"); sp.setSiteUrl("发布分享网站的地址"); Platform qzone = ShareSDK.getPlatform (QZone.NAME); // 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作) qzone.setPlatformActionListener (new PlatformActionListener() { public void onError(Platform arg0, int arg1, Throwable arg2) { //失败的回调,arg:平台对象,arg1:表示当前的动作,arg2:异常信息 } public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) { //分享成功的回调 } public void onCancel(Platform arg0, int arg1) { //取消分享的回调 } }); // 执行图文分享 qzone.share(sp);
如下是自定义一个Dialog然后进行分享
package com.item.jiejie.share; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import com.item.jiejie.R; import java.util.HashMap; import cn.sharesdk.framework.Platform; import cn.sharesdk.framework.PlatformActionListener; import cn.sharesdk.framework.ShareSDK; import cn.sharesdk.tencent.qq.QQ; import cn.sharesdk.tencent.qzone.QZone; import cn.sharesdk.wechat.friends.Wechat; import cn.sharesdk.wechat.moments.WechatMoments; /** * 这是分享弹窗 * Created by Administrator on 2017/11/7. */ public class ShareDialog extends Dialog implements View.OnClickListener { private Context mContext; private DisplayMetrics dm; /** * UI */ private TextView mDialogOne; private TextView mDialogTwo; private TextView mDialogThree; private TextView mDialogFour; public ShareDialog(Context context) { super(context, R.style.SheetDialogStyle); mContext = context; dm = mContext.getResources().getDisplayMetrics(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_share_layout); initView(); } private void initView() { /** * 通过获得的Dialog的Window来控制Dialog的宽高及位置 */ Window dialogWindow = getWindow(); dialogWindow.setGravity(Gravity.BOTTOM); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); lp.width = dm.widthPixels;//设置宽度 dialogWindow.setAttributes(lp); mDialogOne = (TextView) findViewById(R.id.dialog_item_one); mDialogTwo = (TextView) findViewById(R.id.dialog_item_two); mDialogThree = (TextView) findViewById(R.id.dialog_item_three); mDialogFour = (TextView) findViewById(R.id.dialog_item_four); mDialogOne.setOnClickListener(this); mDialogTwo.setOnClickListener(this); mDialogThree.setOnClickListener(this); mDialogFour.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.dialog_item_one: // 分享微信 shareData(Wechat.NAME); break; case R.id.dialog_item_two: // 微信朋友圈 shareData(WechatMoments.NAME); break; case R.id.dialog_item_three: // QQ shareData(QQ.NAME); break; case R.id.dialog_item_four: // QQ空间 shareData(QZone.NAME); break; } dismiss(); } private PlatformActionListener mListener = new PlatformActionListener() { @Override public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) { Log.d("jiejie", "onComplete"); } @Override public void onError(Platform platform, int i, Throwable throwable) { Log.d("jiejie", "onError"); } @Override public void onCancel(Platform platform, int i) { Log.d("jiejie", "onCancel"); } }; private void shareData(String name) { Platform.ShareParams params = new Platform.ShareParams(); params.setTitle("测试分享的标题"); params.setTitleUrl("https://www.baidu.com/"); // 标题的超链接 params.setText("测试的分享文本啊啊啊啊啊啊啊啊啊啊啊"); // text是分享文本 params.setUrl("http://sharesdk.cn"); // url仅在微信(包括好友和朋友圈)中使用 params.setImageUrl("https://qlogo4.store.qq.com/qzone/2524921779/2524921779/100"); //分享网络图片 params.setSite("发布分享的网站名称"); // site是分享此内容的网站名称,仅在QQ空间使用 params.setSiteUrl("https://github.com/"); // siteUrl是分享此内容的网站地址,仅在QQ空间使用 Platform platform = ShareSDK.getPlatform(name); platform.setPlatformActionListener(mListener); platform.share(params); } }