使用的是第三方的Mob來實現分享和發送短信的Demojava
如圖是項目的結構,結合了分享,發送短信,讀取短信內容的功能android
代碼地址:https://github.com/1825492258/MobSharegit
1.SMS發送短信 主要是3部github
第一步:初始化SDK initSDKjson
第二步:註冊回調接口 registerEventHandler(EventHandler handler)微信
第三步:註銷回調接口 unregisterEventHandler(EventHandler handler)網絡
以下是程序的主要代碼app
實現了點擊獲取驗證碼,出現倒計時,讀取短信的,驗證驗證碼的一套登陸流程ide
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); } }