欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android如何集成极光短信验证

时间:2021-02-07 14:47:50|栏目:Android代码|点击:

先借助Android studio工具新建一个新的空项目。

步骤一(获取appkey)

1.在极光官网平台上新建短信应用(根据要求包名和应用名称填写好)


2.获取得到Jpush_AppKey


步骤二 (导入第三方库和so文件)

1.用Android studio打开应用

2.切换应用状态Android为Project状态


3.在libs文件夹下把对应的jar文件拷入  然后as libraries


步骤三(初始化以及调用)

1.新建Application初始化

public class MyApp extends Application { 
  @Override 
  public void onCreate() { 
    super.onCreate(); 
    SMSSDK.getInstance().setDebugMode(true); 
    SMSSDK.getInstance().initSdk(this); 
  } 
} 

2.在activity中调用

public class MainActivity extends BaseActivity { 
  EditText edit_name; 
  EditText edit_pwd; 
  EditText sms; 
  String name; 
  String pwd; 
  Button get_sms; 
  Button btn_login; 
  private Handler handler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
      super.handleMessage(msg); 
      switch (msg.what) { 
        case 1: 
          Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show(); 
          break; 
        case 2: 
          Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show(); 
          break; 
        case 3: 
          Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show(); 
          break; 
        case 4: 
          Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show(); 
          break; 
      } 
    } 
  }; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    init(); 
  } 
 
  private void init() { 
    edit_name = (EditText) findViewById(R.id.edit_name); 
    edit_pwd = (EditText) findViewById(R.id.edit_pwd); 
    sms = (EditText) findViewById(R.id.sms); 
    get_sms = (Button) findViewById(R.id.get_sms); 
    btn_login = (Button) findViewById(R.id.btn_login); 
    get_sms.setOnClickListener(new GetSmsClickListener()); 
    btn_login.setOnClickListener(new LoginClickListener()); 
  } 
 
 
  public class GetSmsClickListener implements View.OnClickListener { 
 
    @Override 
    public void onClick(View view) { 
      name = edit_name.getText().toString().trim(); 
      pwd = edit_pwd.getText().toString().trim(); 
      if (name.equals("")) { 
        edit_name.requestFocus(); 
      } else if (pwd.equals("")) { 
        edit_pwd.requestFocus(); 
      } else { 
        new Thread(new Runnable() { 
          @Override 
          public void run() { 
            try { 
              GetSmsCode(name, pwd); 
            } catch (Exception ex) { 
              ex.printStackTrace(); 
            } 
          } 
        }).start(); 
      } 
    } 
  } 
 
  private void GetSmsCode(String name, String pwd) { 
 
      GetCode(this.name, this.pwd, new GetCodeListener() { 
        @Override 
        public void getCodeSuccess(String s) { 
          Message msg = Message.obtain(); 
          msg.obj = s; 
          msg.what = 1; 
          handler.sendMessage(msg); 
        } 
 
        @Override 
        public void getCodeFail(String s) { 
          Message msg = Message.obtain(); 
          msg.obj = s; 
          msg.what = 2; 
          handler.sendMessage(msg); 
        } 
      }); 
  } 
 
  public class LoginClickListener implements View.OnClickListener { 
 
    @Override 
    public void onClick(View view) { 
      final String code = sms.getText().toString().trim(); 
      name = edit_name.getText().toString().trim(); 
      pwd = edit_pwd.getText().toString().trim(); 
      if (name.equals("")) { 
        edit_name.requestFocus(); 
      } else if (pwd.equals("")) { 
        edit_pwd.requestFocus(); 
      } else if (code.equals("")) { 
        sms.requestFocus(); 
      } else { 
        new Thread(new Runnable() { 
          @Override 
          public void run() { 
            try { 
              CheckSmsCode(name, pwd, code); 
            } catch (Exception ex) { 
              ex.printStackTrace(); 
            } 
          } 
        }).start(); 
      } 
    } 
  } 
  private void CheckSmsCode(String name, String pwd, String code) { 
      CheckCode(this.name, this.pwd, code, new CheckCodeListener() { 
        @Override 
        public void checkCodeSuccess(String s) { 
          Message msg = Message.obtain(); 
          msg.obj = s; 
          msg.what = 3; 
          handler.sendMessage(msg); 
        } 
 
        @Override 
        public void checkCodeFail(String s) { 
          Message msg = Message.obtain(); 
          msg.obj = s; 
          msg.what = 4; 
          handler.sendMessage(msg); 
        } 
      }); 
 
  } 
 
} 

步骤四(填写配置文件)

1.配置应用权限

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

2.配置meta (appkey 从平台上新建的对应应用中来)

<meta-data android:name="JPUSH_APPKEY" android:value="You JPUSH_APPKEY"/> 

步骤五(调试)

1.点击获取密码按钮获取短信信息


上一篇:Android手势密码--设置和校验功能的实现代码

栏    目:Android代码

下一篇:Android学习笔记之AndroidManifest.xml文件解析(详解)

本文标题:Android如何集成极光短信验证

本文地址:http://www.codeinn.net/misctech/58503.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有