Android極光推送集成以及極光驗證碼Demo

極光推送以及極光驗證碼

極光推送

配置

主要特點

產品功能說明

客戶端維持連接占用資源少、耗電低
SDK豐富的接口,可定制通知欄提示樣式
服務器大容量、穩定
jpush-android-release-3.x.y.zip 集成壓縮包內容

AndroidManifest.xml
客戶端嵌入SDK參考的配置文件
libs/jcore-android.v1.x.y.jar
極光開發者服務的核心包。
libs/jpush-android_v3.x.y.jar
JPush SDK 開發包。
libs/(cpu-type)/libjcore1xy.so
各種CPU類型的native開發包。
res
集成SDK必須添加的資源文件
example
是一個完整的 Android 項目,通過這個演示了 JPush SDK 的基本用法,可以用來做參考。
Android SDK 版本

目前SDK只支持Android 2.3或以上版本的手機系統。富媒體信息流功能則需Android3.0或以上版本的系統。

在這里我用到的是手動集成步驟

首先要在極光官網的后臺創建一個應用
在這里要設置一下推送信息,我們點擊完成推送設置
將自己創建的應用的包名添加到推送設置的應用包名當中

解壓縮 jpush-android-release-3.x.y.zip 集成壓縮包。
復制 libs/jcore-android_v1.x.y.jar 到工程 libs/ 目錄下。
復制 libs/jpush-android_v3.x.y.jar 到工程 libs/ 目錄下。
復制 libs/(cpu-type)/libjcore1xy.so 到你的工程中存放對應cpu類型的目錄下。
復制 res/ 中drawable-hdpi, layout, values文件夾中的資源文件到你的工程中 res/ 對應同名的目錄下。
說明 1:若沒有res/drawable-xxxx/jpush_notification_icon這個資源默認使用應用圖標作為通知icon,在5.0以上系統將應用圖標作為statusbar icon可能顯示不正常,用戶可定義沒有陰影和漸變色的icon替換這個文件,文件名不要變。
說明 2:使用android studio的開發者,如果使用jniLibs文件夾導入so文件,則僅需將所有cpu類型的文件夾拷進去;如果將so文件添加在module的libs文件夾下,注意在module的gradle配置中添加一下配置:



在這里要提一下一些小bug,我們創建的應用可能沒有libs文件夾,在這里我們要手動創建
點擊Project在app目錄下創建libs包,最后在gradle下配置一下下面的代碼,即可以生成。而且在Android視圖下,也可以看到有個jniLibs的文件夾,這樣我們運行應用時就不會包找不到so文件的異常了
android {
        ......
        sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
                ......
            }
            ......
        }
        ......
    }

配置 AndroidManifest.xml

根據 SDK 壓縮包里的 AndroidManifest.xml 樣例文件,來配置應用程序項目的 AndroidManifest.xml 。
主要步驟為:
復制備注為 "Required" 的部分
將標注為“您應用的包名”的部分,替換為當前應用程序的包名
將標注為“您應用的Appkey”的部分,替換為在Portal上注冊該應用的的Key,例如:9fed5bcb7b9b87413678c407

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="您應用的包名"
    android:versionCode="303"
    android:versionName="3.0.3"
    >
    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />

    <!-- Required -->
    <permission
        android:name="您應用的包名.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />

    <!-- Required -->
    <uses-permission android:name="您應用的包名.permission.JPUSH_MESSAGE" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <!-- Optional. Required for location feature -->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于開啟 debug 版本的應用在6.0 系統上 層疊窗口權限 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:name="Your Application Name">

        <!-- Required SDK 核心功能-->
        <!-- 可配置android:process參數將PushService放在其他進程中 -->
        <service
            android:name="cn.jpush.android.service.PushService"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTER" />
                <action android:name="cn.jpush.android.intent.REPORT" />
                <action android:name="cn.jpush.android.intent.PushService" />
                <action android:name="cn.jpush.android.intent.PUSH_TIME" />
            </intent-filter>
        </service>

        <!-- since 1.8.0 option 可選項。用于同一設備中不同應用的JPush服務相互拉起的功能。 -->
        <!-- 若不啟用該功能可刪除該組件,將不拉起其他應用也不能被其他應用拉起 -->
         <service
             android:name="cn.jpush.android.service.DaemonService"
             android:enabled="true"
             android:exported="true">
             <intent-filter >
                 <action android:name="cn.jpush.android.intent.DaemonService" />
                 <category android:name="您應用的包名"/>
             </intent-filter>
         </service>

        <!-- Required SDK核心功能-->
        <receiver
            android:name="cn.jpush.android.service.PushReceiver"
            android:enabled="true" >
          <intent-filter android:priority="1000">
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
                <category android:name="您應用的包名"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <!-- Optional -->
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>

        <!-- Required SDK核心功能-->
        <activity
            android:name="cn.jpush.android.ui.PushActivity"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.NoTitleBar"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.ui.PushActivity" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="您應用的包名" />
            </intent-filter>
        </activity>
        <!-- SDK核心功能-->
        <activity
            android:name="cn.jpush.android.ui.PopWinActivity"
            android:configChanges="orientation|keyboardHidden"
            android:exported="false"
            android:theme="@style/MyDialogStyle">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="您應用的包名" />
            </intent-filter>
        </activity>

        <!-- Required SDK核心功能-->
        <service
            android:name="cn.jpush.android.service.DownloadService"
            android:enabled="true"
            android:exported="false" >
        </service>

        <!-- Required SDK核心功能-->
        <receiver android:name="cn.jpush.android.service.AlarmReceiver" />

        <!-- Required since 3.0.7 -->
        <!-- 新的tag/alias接口結果返回需要開發者配置一個自定的廣播 -->
        <!-- 該廣播需要繼承JPush提供的JPushMessageReceiver類, 并如下新增一個 Intent-Filter -->
        <receiver
        android:name="自定義 Receiver"
        android:enabled="true" >
        <intent-filter>
        <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
        <category android:name="您應用的包名" />
        </intent-filter>
        </receiver>

        <!-- User defined. 用戶自定義的廣播接收器-->
         <receiver
             android:name="您自己定義的Receiver"
             android:enabled="true">
             <intent-filter>
                 <!--Required 用戶注冊SDK的intent-->
                 <action android:name="cn.jpush.android.intent.REGISTRATION" />
                 <!--Required 用戶接收SDK消息的intent-->
                 <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
                 <!--Required 用戶接收SDK通知欄信息的intent-->
                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
                 <!--Required 用戶打開自定義通知欄的intent-->
                 <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
                 <!-- 接收網絡變化 連接/斷開 since 1.6.3 -->
                 <action android:name="cn.jpush.android.intent.CONNECTION" />
                 <category android:name="您應用的包名" />
             </intent-filter>
         </receiver>

        <!-- Required. For publish channel feature -->
        <!-- JPUSH_CHANNEL 是為了方便開發者統計APK分發渠道。-->
        <!-- 例如: -->
        <!-- 發到 Google Play 的APK可以設置為 google-play; -->
        <!-- 發到其他市場的 APK 可以設置為 xxx-market。 -->
        <!-- 目前這個渠道統計功能的報表還未開放。-->
        <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
        <!-- Required. AppKey copied from Portal -->
        <meta-data android:name="JPUSH_APPKEY" android:value="您應用的Appkey"/>
    </application>
</manifest>

我們目前沒有做到兩個自定義receiver的配置信息,目前階段我們可以給注釋掉,這樣manifest最后配置完成,就不錯報錯了

將所有信息配置完成后,在自定義類繼承自Application下聲明方法
在此之前,我們manifest中要聲明一下application的name值

public class ExampleApplication extends Application {
@Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }
}

就算是配置完成了,這樣我們在相對應的極光官網上面的控制臺,即可以發送推送。
比如:

我們點開控制臺


點擊廣播所有人,平臺是安卓平臺,編輯一下通知內容,點擊發送通知后,如果發送成功,會彈出一個Dialog提示成功
我們來看一下模擬器


極光短信驗證碼Demo

因為我們已經配置過了Appkey,權限等信息,所以當前我們只需要下載極光短信的jar即可
https://docs.jiguang.cn/jsms/resources/

極光短信的API不多,只需要記住幾個就行,下面我將通過一個Demo展示一下注冊模塊加上驗證碼的邏輯\

與極光推送一樣,我們要在全局Application初始化

 @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        handler = new Handler();
        //獲取主線程的id
        mainThreadId = Process.myTid();

        //初始化JPush
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        //初始化極光短信
        SMSSDK.getInstance().initSdk(this);
        SMSSDK.getInstance().setDebugMode(true);
        SMSSDK.getInstance().setIntervalTime(60000);
    }

上面的三行代碼就是初始化極光短信。
setIntervalTime就是設置短信驗證碼的間隔時間。

<?xml version="1.0" encoding="utf-8"?>
<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">

    <include layout="@layout/title" />

    <LinearLayout
        android:id="@+id/ll_et_login"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:layout_marginTop="130dp"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/ic_phone" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="3dp"
                android:text="手機號"
                android:textColor="#666" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/et_tel_register"
                style="@style/et_lab"
                android:hint="請輸入手機號"
                android:inputType="number" />

        </LinearLayout>

        <View style="@style/ViewLine" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginRight="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/ic_psd_locked" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="3dp"
                android:text="驗證碼"
                android:textColor="#666" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/et_verification_register"
                style="@style/et_lab"
                android:layout_weight="1"
                android:hint="請輸入驗證碼"
                android:inputType="number" />

            <Button
                android:id="@+id/btn_verification_register"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_gravity="center_vertical"
                android:background="@drawable/selector_btn_login"
                android:padding="10dp"
                android:text="請輸入驗證碼"
                android:textColor="#fff" />
        </LinearLayout>

        <View style="@style/ViewLine" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/ic_psd_locked" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="3dp"
                android:text="密    碼"
                android:textColor="#666" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/et_psd_register"
                style="@style/et_lab"
                android:hint="密碼為6至12個數字或字母" />

        </LinearLayout>

        <View style="@style/ViewLine" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/ic_psd_locked" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="3dp"
                android:text="密    碼"
                android:textColor="#666" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/et_psd_confirm_register"
                style="@style/et_lab"
                android:hint="請再次確認密碼" />

        </LinearLayout>

        <View style="@style/ViewLine" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rl_login_text"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_below="@id/ll_et_login">

        <RelativeLayout
            android:id="@+id/rl_isCheck"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="20dp">

            <CheckBox
                android:id="@+id/cb_consent"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:checked="true"
                android:shadowColor="#4581c7" />

            <TextView
                android:id="@+id/tv_confirm_register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/cb_consent"
                android:text="我已閱讀并同意"
                android:textColor="#999999"
                android:textSize="14sp" />

        </RelativeLayout>

        <TextView
            android:id="@+id/tv_protocol_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/rl_isCheck"
            android:text="@string/register"
            android:textColor="#ff0000"
            android:textSize="14sp" />

    </RelativeLayout>

    <Button
        android:id="@+id/btn_register"
        style="@style/ButtonLoginStyle"
        android:layout_below="@id/rl_login_text"
        android:layout_marginTop="50dp"
        android:text="注冊" />

</RelativeLayout>

這是注冊模塊的界面


這是RegisterActivity注冊界面的邏輯,在這里,點擊按鈕我用了安卓的一個CountDownTimer類方法實現

package com.qryl.qryl.activity.login;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qryl.qryl.R;
import com.qryl.qryl.activity.MainActivity;
import com.qryl.qryl.util.VerificationCountDownTimer;

import java.io.IOException;

import cn.jpush.sms.SMSSDK;
import cn.jpush.sms.listener.SmscheckListener;
import cn.jpush.sms.listener.SmscodeListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    private static final String TAG = "RegisterActivity";

    private Button btnVerification;
    private AppCompatEditText etVerification;
    private AppCompatEditText etPsd;
    private AppCompatEditText etPsdComfirm;
    private RelativeLayout rlIsCheck;
    private CheckBox checkBox;
    private TextView tvProtocol;
    private Button btnRegisiter;
    private AppCompatEditText etTel;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

    }

    private void initView() {
        progressDialog = new ProgressDialog(this);
        btnVerification = (Button) findViewById(R.id.btn_verification_register);
        etTel = (AppCompatEditText) findViewById(R.id.et_tel_register);
        etVerification = (AppCompatEditText) findViewById(R.id.et_verification_register);
        etPsd = (AppCompatEditText) findViewById(R.id.et_psd_register);
        etPsdComfirm = (AppCompatEditText) findViewById(R.id.et_psd_confirm_register);
        rlIsCheck = (RelativeLayout) findViewById(R.id.rl_isCheck);
        checkBox = (CheckBox) findViewById(R.id.cb_consent);
        tvProtocol = (TextView) findViewById(R.id.tv_protocol_register);
        btnRegisiter = (Button) findViewById(R.id.btn_register);
        //根據cb判斷是否點擊了對號
        rlIsCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = checkBox.isChecked();
                //根據checkBox的狀態修改樣式
                if (checked) {
                    checkBox.setChecked(false);
                    checkBox.setBackgroundResource(R.mipmap.ic_frame_false);
                } else {
                    checkBox.setChecked(true);
                    checkBox.setBackgroundResource(R.mipmap.ic_frame_true);
                }
            }
        });
        //實現倒計時并發送請求并獲取驗證碼的功能
        receiverSMS();
        //注冊
        btnRegisiter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //點擊注冊的時候要確認所有的注冊信息不為空
                registerIfNotNull();
            }
        });
    }

    /**
     * 點擊注冊的時候要確認所有的注冊信息不為空
     */
    private void registerIfNotNull() {
        if (TextUtils.isEmpty(etTel.getText().toString())) {
            Toast.makeText(this, "請輸入正確的手機號!", Toast.LENGTH_SHORT).show();
        } else {
            if (TextUtils.isEmpty(etPsd.getText().toString())) {
                Toast.makeText(this, "請輸入注冊密碼!", Toast.LENGTH_SHORT).show();
            } else {
                if (!etPsd.getText().toString().equals(etPsdComfirm.getText().toString())) {
                    Toast.makeText(this, "確認密碼與密碼不符!", Toast.LENGTH_SHORT).show();
                } else {
                    if (!checkBox.isChecked()) {
                        Toast.makeText(this, "請同意用戶注冊協議!", Toast.LENGTH_SHORT).show();
                    } else {
                        progressDialog.setMessage("正在注冊...");
                        progressDialog.show();
                        checkSMS();
                    }
                }
            }
        }
    }

    /**
     * 驗證碼通過正確,就注冊發送服務器
     */
    private void checkSMS() {
        SMSSDK.getInstance().checkSmsCode(etTel.getText().toString(), etVerification.getText().toString(), new SmscheckListener() {
            @Override
            public void checkCodeSuccess(String s) {
                Log.i(TAG, "checkCodeSuccess: 驗證成功:" + s);
                postData();
            }

            @Override
            public void checkCodeFail(int i, String s) {
                Log.i(TAG, "checkCodeSuccess: 驗證失敗:" + s);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (progressDialog.isShowing() && progressDialog != null) {
                            progressDialog.dismiss();
                        }
                        Toast.makeText(RegisterActivity.this, "驗證碼驗證失敗!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    /**
     * 向服務器發送注冊信息
     */
    private void postData() {
        OkHttpClient client = new OkHttpClient();
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("password", etPsdComfirm.getText().toString());
        builder.addFormDataPart("mobile", etTel.getText().toString());
        MultipartBody requestBody = builder.build();
        Request requset = new Request.Builder().url("").post(requestBody).build();
        client.newCall(requset).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i(TAG, "onFailure: 服務器連接失敗");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (progressDialog.isShowing() && progressDialog != null) {
                            progressDialog.dismiss();
                        }
                        Toast.makeText(RegisterActivity.this, "注冊失敗!", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i(TAG, "onResponse: 服務器連接成功" + response.body().string());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (progressDialog.isShowing() && progressDialog != null) {
                            progressDialog.dismiss();
                        }
                        Toast.makeText(RegisterActivity.this, "注冊成功!", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
            }
        });
    }

    /**
     * 實現倒計時并發送請求并獲取驗證碼的功能
     */
    private void receiverSMS() {
        btnVerification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //實現倒計時
                countDownTimer();
                //請求驗證碼
                String tel = etTel.getText().toString();
                SMSSDK.getInstance().getSmsCode(tel, "1", new SmscodeListener() {
                    @Override
                    public void getCodeSuccess(String s) {
                        //獲取成功時
                        Log.i(TAG, "獲取驗證碼成功! " + s);
                    }

                    @Override
                    public void getCodeFail(int i, String s) {
                        //獲取失敗時
                        Log.i(TAG, "獲取驗證碼失敗! " + s);
                    }
                });
            }
        });
    }

    /**
     * 倒計時功能
     */
    private void countDownTimer() {
        VerificationCountDownTimer timer = new VerificationCountDownTimer(60000, 1000) {
            //倒計時過程
            @Override
            public void onTick(long millisUntilFinished) {
                super.onTick(millisUntilFinished);
                btnVerification.setClickable(false);//防止重新倒計時
                btnVerification.setText(millisUntilFinished / 1000 + "s");
            }

            //計時完畢的方法
            @Override
            public void onFinish() {
                //重新給Button設置文字
                btnVerification.setText("重新獲取驗證碼");
                //設置可點擊
                btnVerification.setClickable(true);
            }
        };
        timer.start();
    }

}

CountTimer類

package com.qryl.qryl.util;

import android.os.CountDownTimer;

/**
 * Created by hp on 2017/8/28.
 */

public class VerificationCountDownTimer extends CountDownTimer {
    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public VerificationCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {

    }

    @Override
    public void onFinish() {

    }
}

這樣,當我們輸入手機號注冊發送驗證碼即會收到驗證碼

大家可以看一下驗證碼樣式,這是一分鐘過期的。可是我們平常注冊,1分鐘的時間是不是太短了呢,而且文字什么的也是極光推送也不是自己定義的

我們可以點擊極光推送的控制臺
打開新建模板,可以設置短信有效期和短信模板

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,646評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,595評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,560評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,035評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,814評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,224評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,301評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,444評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,988評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,804評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,998評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,544評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,237評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,665評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,927評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,706評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,993評論 2 374

推薦閱讀更多精彩內容