3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
package
=
"com.example.android_sms"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
uses-sdk
android:minSdkVersion
=
"8"
android:targetSdkVersion
=
"18"
/>
<
uses-permission
android:name
=
"android.permission.READ_CONTACTS"
/>
<
uses-permission
android:name
=
"android.permission.READ_PHONE_STATE"
/>
<
uses-permission
android:name
=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<
uses-permission
android:name
=
"android.permission.ACCESS_NETWORK_STATE"
/>
<
uses-permission
android:name
=
"android.permission.ACCESS_WIFI_STATE"
/>
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
<
uses-permission
android:name
=
"android.permission.RECEIVE_SMS"
/>
<
uses-permission
android:name
=
"android.permission.GET_TASKS"
/>
<
uses-permission
android:name
=
"android.permission.ACCESS_FINE_LOCATION"
/>
<
application
android:allowBackup
=
"true"
android:icon
=
"@drawable/ic_launcher"
android:label
=
"@string/app_name"
android:theme
=
"@style/AppTheme"
>
<
activity
android:name
=
"com.example.android_sms.MainActivity"
android:label
=
"@string/app_name"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
intent-filter
>
activity
>
<
activity
android:name
=
"cn.smssdk.SMSSDKUIShell"
android:configChanges
=
"keyboardHidden|orientation|screenSize"
android:theme
=
"@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode
=
"stateHidden|adjustResize"
/>
application
>
manifest
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
TextView
android:id
=
"@+id/textView2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_centerHorizontal
=
"true"
android:layout_marginTop
=
"20dp"
android:text
=
"短信验证"
android:textColor
=
"#00ffaa"
android:textSize
=
"20dp"
/>
<
TextView
android:id
=
"@+id/textView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentLeft
=
"true"
android:layout_below
=
"@+id/textView2"
android:layout_marginLeft
=
"20dp"
android:layout_marginTop
=
"20dp"
android:text
=
"手机号:"
/>
<
EditText
android:id
=
"@+id/phone"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignBaseline
=
"@+id/textView1"
android:layout_alignBottom
=
"@+id/textView1"
android:layout_toRightOf
=
"@+id/textView1"
android:maxLength
=
"11"
android:ems
=
"11"
android:inputType
=
"phone"
>
<
requestFocus
/>
EditText
>
<
TextView
android:id
=
"@+id/textView3"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/textView1"
android:layout_marginTop
=
"40dp"
android:layout_below
=
"@+id/phone"
android:text
=
"验证码:"
/>
<
EditText
android:id
=
"@+id/cord"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginTop
=
"20dp"
android:layout_alignBaseline
=
"@+id/textView3"
android:layout_alignBottom
=
"@+id/textView3"
android:layout_alignLeft
=
"@+id/phone"
android:ems
=
"4"
android:maxLength
=
"4"
android:inputType
=
"phone"
/>
<
Button
android:id
=
"@+id/getcord"
style
=
"?android:attr/buttonStyleSmall"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignTop
=
"@+id/cord"
android:layout_marginLeft
=
"20dp"
android:layout_marginTop
=
"10dp"
android:layout_toRightOf
=
"@+id/cord"
android:visibility
=
"visible"
android:text
=
"获取验证码"
/>
<
Button
android:id
=
"@+id/savecord"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:layout_below
=
"@+id/cord"
android:layout_margin
=
"20dp"
android:text
=
"验证"
/>
<
TextView
android:id
=
"@+id/now"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:layout_above
=
"@+id/savecord"
android:layout_toRightOf
=
"@+id/cord"
android:gravity
=
"center_horizontal"
android:visibility
=
"gone"
android:text
=
"提示信息"
android:textColor
=
"#aaaaaa"
/>
RelativeLayout
>
|
public
class
MainActivity extends Activity implements OnClickListener{
private
EditText phone;
private
EditText cord;
private
TextView now;
private
Button getCord;
private
Button saveCord;
private
String iPhone;
private
String iCord;
private
int
time = 60;
private
boolean flag =
true
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
SMSSDK.initSDK(
this
,
"<您的appkey>"
,
"<您的appsecret>"
);
EventHandler eh=
new
EventHandler(){
@Override
public
void
afterEvent(
int
event
,
int
result, Object data) {
Message msg =
new
Message();
msg.arg1 =
event
;
msg.arg2 = result;
msg.obj = data;
handler.sendMessage(msg);
}
};
SMSSDK.registerEventHandler(eh);
}
private
void
init() {
phone = (EditText) findViewById(R.id.phone);
cord = (EditText) findViewById(R.id.cord);
now = (TextView) findViewById(R.id.now);
getCord = (Button) findViewById(R.id.getcord);
saveCord = (Button) findViewById(R.id.savecord);
getCord.setOnClickListener(
this
);
saveCord.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.getcord:
if
(!TextUtils.isEmpty(phone.getText().toString().trim())){
if
(phone.getText().toString().trim().length()==11){
iPhone = phone.getText().toString().trim();
SMSSDK.getVerificationCode(
"86"
,iPhone);
cord.requestFocus();
getCord.setVisibility(View.GONE);
}
else
{
Toast.makeText(MainActivity.
this
,
"请输入完整电话号码"
, Toast.LENGTH_LONG).show();
phone.requestFocus();
}
}
else
{
Toast.makeText(MainActivity.
this
,
"请输入您的电话号码"
, Toast.LENGTH_LONG).show();
phone.requestFocus();
}
break
;
case
R.id.savecord:
if
(!TextUtils.isEmpty(cord.getText().toString().trim())){
if
(cord.getText().toString().trim().length()==4){
iCord = cord.getText().toString().trim();
SMSSDK.submitVerificationCode(
"86"
, iPhone, iCord);
flag =
false
;
}
else
{
Toast.makeText(MainActivity.
this
,
"请输入完整验证码"
, Toast.LENGTH_LONG).show();
cord.requestFocus();
}
}
else
{
Toast.makeText(MainActivity.
this
,
"请输入验证码"
, Toast.LENGTH_LONG).show();
cord.requestFocus();
}
break
;
default
:
break
;
}
}
//验证码送成功后提示文字
private
void
reminderText() {
now.setVisibility(View.VISIBLE);
handlerText.sendEmptyMessageDelayed(1, 1000);
}
Handler handlerText =
new
Handler(){
public
void
handleMessage(Message msg) {
if
(msg.what==1){
if
(time>0){
now.setText(
"验证码已发送"
+time+
"秒"
);
time--;
handlerText.sendEmptyMessageDelayed(1, 1000);
}
else
{
now.setText(
"提示信息"
);
time = 60;
now.setVisibility(View.GONE);
getCord.setVisibility(View.VISIBLE);
}
}
else
{
cord.setText(
""
);
now.setText(
"提示信息"
);
time = 60;
now.setVisibility(View.GONE);
getCord.setVisibility(View.VISIBLE);
}
};
};
Handler handler=
new
Handler(){
@Override
public
void
handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int
event
= msg.arg1;
int
result = msg.arg2;
Object data = msg.obj;
Log.e(
"event"
,
"event="
+
event
);
if
(result == SMSSDK.RESULT_COMPLETE) {
//短信注册成功后,返回MainActivity,然后提示新好友
if
(
event
== SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交验证码成功,验证通过
Toast.makeText(getApplicationContext(),
"验证码校验成功"
, Toast.LENGTH_SHORT).show();
handlerText.sendEmptyMessage(2);
}
else
if
(
event
== SMSSDK.EVENT_GET_VERIFICATION_CODE){
//服务器验证码发送成功
reminderText();
Toast.makeText(getApplicationContext(),
"验证码已经发送"
, Toast.LENGTH_SHORT).show();
}
else
if
(
event
==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
//返回支持发送验证码的国家列表
Toast.makeText(getApplicationContext(),
"获取国家列表成功"
, Toast.LENGTH_SHORT).show();
}
}
else
{
if
(flag){
getCord.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.
this
,
"验证码获取失败,请重新获取"
, Toast.LENGTH_SHORT).show();
phone.requestFocus();
}
else
{
((Throwable) data).printStackTrace();
int
resId = getStringRes(MainActivity.
this
,
"smssdk_network_error"
);
Toast.makeText(MainActivity.
this
,
"验证码错误"
, Toast.LENGTH_SHORT).show();
cord.selectAll();
if
(resId > 0) {
Toast.makeText(MainActivity.
this
, resId, Toast.LENGTH_SHORT).show();
}
}
}
}
};
@Override
protected
void
onDestroy() {
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
}
}