在注册期间,通过api中的消息验证手机号码,如;毒蛇“;对于android
我有我的消息API的url、用户名和密码。我是个初学者。我试过某些代码,但失败了。我需要将此api与我拥有的参数集成的工作代码,以及用于读取消息api接收的消息的接收器代码 过程如下在注册期间,通过api中的消息验证手机号码,如;毒蛇“;对于android,android,api,sms,message,verification,Android,Api,Sms,Message,Verification,我有我的消息API的url、用户名和密码。我是个初学者。我试过某些代码,但失败了。我需要将此api与我拥有的参数集成的工作代码,以及用于读取消息api接收的消息的接收器代码 过程如下 在用户注册期间,用户输入一个号码 数字被发送到API的url,用户收到一条带有随机代码的消息 应用程序验证随机码是否与 他被派去了 如果相同,则用户将获得寄存器并查看应用程序 内容 此活动仅发生一次注册 请帮帮我,我现在正处于严重的困惑之中…提前非常感谢您试试这个对我有用的代码 在这里,我要求用户在安装过程中输入一
请帮帮我,我现在正处于严重的困惑之中…提前非常感谢您试试这个对我有用的代码 在这里,我要求用户在安装过程中输入一次电话号码,然后通过手机本身发送一个随机号码作为短信。此过程仅执行一次,即仅用于注册(如果输入的代码与发送的随机代码匹配。如果不匹配,则不会激活此过程,并再次显示注册屏幕) 这是密码
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.text.InputType;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class SplashActivity extends Activity {
private int splashTime = 3000;
private Thread thread;
private ProgressBar mSpinner;
Boolean number_confirmation=false;
String randomcode;
String deviceimei;
String phonenumber;
String sent_code,activation;
boolean isactivated;
ProgressDialog progressDialog;
TextView tv_loading;
public EditText input;
SharedPreferences wmbPreference1,wmbPreference2;
SharedPreferences.Editor editor;
RandomCodeGenerator rc;
boolean isInternetPresent=false;
AlertDialogManager alert;
ConnectionDetector cd;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
try
{
alert=new AlertDialogManager();
cd=new ConnectionDetector(SplashActivity.this);
isInternetPresent = cd.isConnectingToInternet();
if(isInternetPresent)
{
//wmbPreference for Shared Prefs that lasts forever
wmbPreference1 = PreferenceManager.getDefaultSharedPreferences(this);
//installsp for Shared Prefs that lasts only just once each time program is running
wmbPreference2 = getApplicationContext().getSharedPreferences("install_code_prefs", Activity.MODE_PRIVATE);
boolean isActivated=wmbPreference1.getBoolean("ISACTIVATED", false);
boolean isFirstRun = wmbPreference1.getBoolean("FIRSTRUN", true);
boolean isPhonenumberEntered=wmbPreference1.getBoolean("PHONENUMBERENTERED", false);
number_confirmation=wmbPreference1.getBoolean("NUMBER_CONFIRMATION", false);
deviceimei=wmbPreference1.getString("IMEI", "");
if (isFirstRun||!isActivated)
{
// Code to run once
//code to get next random number
rc=new RandomCodeGenerator();
//code to set wmbPreference with FIRSTRUN flag
editor = wmbPreference1.edit();
editor.putBoolean("FIRSTRUN", false);
editor.commit();
// Showing Alert Message
//code to create alert dialog
if(isPhonenumberEntered==false)
phonenumberDialog();
else
passwordDialog();
}
super.onCreate(savedInstanceState);
if(isActivated)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mSpinner = (ProgressBar) findViewById(R.id.Splash_ProgressBar);
tv_loading=(TextView) findViewById(R.id.tv_loading);
mSpinner.setIndeterminate(true);
// runOnUiThread(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
//
// }
// });
thread = new Thread(runable);
thread.start();
}
else
{
editor = wmbPreference2.edit();
editor.putBoolean("FRESHRUN", true);
editor.commit();
}
}
else
{
// Internet Connection is not present
alert.showAlertDialog(SplashActivity.this, "xxxxxx",
"Please connect to working Internet connection", false);
}
}
catch(Exception e)
{
Toast.makeText(SplashActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
public Runnable runable = new Runnable() {
public void run() {
try {
Thread.sleep(splashTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
tv_loading.setText("Completing..");
mSpinner.setIndeterminate(true);
}
});
startActivity(new Intent(SplashActivity.this,HomeActivity.class));
finish();
} catch (Exception e) {
// TODO: handle exception
}
}
};
public void passwordDialog()
{
//code to create alert dialog
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SplashActivity.this);
alertDialog.setTitle("xxxxx");
//alertDialog.setIcon(R.drawable.logo);
// Setting Dialog Message
alertDialog.setMessage(Html.fromHtml("<b>PASSWORD</b><br/>Enter 6 digit password sent to your phone"));
input = new EditText(SplashActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
// bt_resend=new Button(HomeActivity.this);
// bt_resend.setText("Resend");
// bt_resend.setLayoutParams(lp);
alertDialog.setView(input);
// alertDialog.setView(bt_resend);
alertDialog.setCancelable(false);
alertDialog.setNeutralButton("Resend", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
phonenumberDialog();
}
});
alertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to execute after dialog
deviceimei=getIMEI();
checkPassword();
saveIMEI();
// Toast.makeText(SplashActivity.this,"IMEI:"+deviceimei, Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// code to execute after dialog
dialog.cancel();
//code to finish app
finish();
}
});
alertDialog.show();
}
public void phonenumberDialog()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SplashActivity.this);
alertDialog.setTitle("xxxxx");
alertDialog.setIcon(R.drawable.logo);
// Setting Dialog Message
alertDialog.setMessage(Html.fromHtml("<b>PHONE NUMBER</b><br/>Enter Your Phone Number for verification<br/><small>*standard SMS rates apply</small>"));
input = new EditText(SplashActivity.this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Next",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// code to execute after dialog
editor = wmbPreference1.edit();
editor.putBoolean("PHONENUMBERENTERED", true);
editor.commit();
String mobno=input.getText().toString();
//save phonenumber in shared preference
editor = wmbPreference1.edit();
editor.putString("PHONENUMBER", mobno);
editor.commit();
final String rnc=rc.nextId();
randomcode=rnc;
//code to set installsp with randomnumber code
editor= wmbPreference2.edit();
editor.putString("install_code", rnc);
editor.commit();
Log.d("rnc", rnc);
try
{
//code to send text message
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(mobno, null,"xxxxx\nInstallation Password\n#####\n"+"6 digit Password: "+rnc+" " , null, null);
}
catch(Exception e)
{
// Toast.makeText(HomeActivity.this, "Please Enter Phone Number", Toast.LENGTH_LONG);
}
//code to create alert dialog
passwordDialog();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("SKIP",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
passwordDialog();
}
});
alertDialog.show();
}
public String getIMEI()
{
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
return imei;
}
public void checkPassword()
{
activation = input.getText().toString();
SharedPreferences installsp = getApplicationContext().getSharedPreferences("install_code_prefs", Activity.MODE_PRIVATE);
sent_code=installsp.getString("install_code", null);
phonenumber=wmbPreference1.getString("PHONENUMBER", "");
new WebServiceBackgroundTask().execute(phonenumber,sent_code,deviceimei);
}
public void saveIMEI()
{
editor = wmbPreference1.edit();
editor.putString("IMEI", deviceimei);
editor.commit();
}
public class WebServiceBackgroundTask extends AsyncTask<String, Integer, String> {
int i=0;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(SplashActivity.this);
progressDialog.setTitle("YATRAMiTR");
progressDialog.setIcon(R.drawable.logo);
progressDialog.setMessage(Html.fromHtml("<b>Password</b><br/>Validating Password..."));
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String resp="";
if(activation.compareTo(sent_code)==0)
{
// Toast.makeText(SplashActivity.this,"Password Matched", Toast.LENGTH_SHORT).show();
//code to set wmbPreference with ISACTIVATED flag
editor=wmbPreference1.edit();
editor.putString("PWD", sent_code);
editor.commit();
isactivated=true;
editor = wmbPreference1.edit();
editor.putBoolean("ISACTIVATED", true);
editor.commit();
resp="success";
}
else
isactivated=false;
return resp;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
// Toast.makeText(SplashActivity.this,"Sent code:"+sent_code, Toast.LENGTH_SHORT).show();
// Toast.makeText(SplashActivity.this,"Entered code:"+activation, Toast.LENGTH_SHORT).show();
if(result!="")
Toast.makeText(SplashActivity.this, result, Toast.LENGTH_LONG).show();
if(isactivated)
{
//code to restart app
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(i);
}
else
{
Toast.makeText(SplashActivity.this,"Wrong Password!", Toast.LENGTH_SHORT).show();
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(i);
}
}
}
}
上面的代码检查应用程序是否已激活。如果未激活,则该应用程序将重定向到Splashscreen活动
现在,Randomcode类如下所示(RandomCodeGenerator.java)
ConnectionDetector.java
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
试试这个,看看这是不是你需要的。如果有任何错误,请告诉我。我想
将满足您的要求。这是一种免费验证手机号码的新方法。:) 你面临的问题是什么?这里有什么问题吗,这是要求r8.问题是我没有一个合适的代码,我对它很陌生,无法制作自己的代码…先生,我很感激你的回答,但是我可以有一个整合了sms网关的代码,我必须使用sms api的url、用户名和密码来发送消息,然后通过从sms api发送到他自己号码的随机代码来验证。这就像viber和其他一些使用消息验证的应用程序一样。感谢您的支持。此外,此处无法将alertdialogmanager和connectiondetector解析为类型。
import java.math.BigInteger;
import java.security.SecureRandom;
public class RandomCodeGenerator {
private SecureRandom random = new SecureRandom();
public String nextId() {
return new BigInteger(30, random).toString(32);
}
}
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
public class AlertDialogManager {
/**
* Function to display simple Alert Dialog
* @param context - application context
* @param title - alert dialog title
* @param message - alert message
* @param status - success/failure (used to set icon)
* - pass null if you don't want icon
* */
@SuppressWarnings("deprecation")
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Setting alert dialog icon
// alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
alertDialog.setIcon(R.drawable.logo);
alertDialog.setCancelable(false);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
}