Android 部分机型有时UDP消息发送失败

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/winrainbow/article/details/53870887

现象描述:

在锤子手机M1L上,有时UDP包发送不出去(抓不到发送出的UDP报文),没有任何异常报出

问题定位结果:

由于在发送报文之前,监听了30023端口,用于接收数据。尽管再发送报之前已经关闭了该socket

在发送udp报文时,指定了源端口也是30023 导致报文无法正常发出,随机源端口,发送成功。

 

分析:尽管socket已经close,但可能30023端口还是不能用于发送报文。(非必现)

三人行,必有吾师,欢迎加入星球,一起讨论技术点滴,在知识换宇宙中,取长补短,一起飞!


 

展开阅读全文

eclipse开发android发送验证码有时成功有时失败

05-19

public class Tab1Activity extends Activity implements OnClickListener, Callback rn private EditText userNameRegis;rn private EditText userPwdRegis;rn private EditText userPhoneRegis;rn private Button userGetIdentiRegis;rn private Spinner schoolsSpinner;rn private EditText identiCode;rn public MyDB mydb;rn SQLiteDatabase db;rn private Button completeRegis;rn private String phonenumber, usernameStr, userpwdStr;rn // flag for Internet connection statusrn Boolean isInternetPresent = false;rn // Connection detector classrn ConnectionDetector cd;rn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn requestWindowFeature(Window.FEATURE_NO_TITLE);rn setContentView(R.layout.activity_tab1);rn // 验证码rn SMSSDK.initSDK(this, "1dd6496963c8c",rn "7b1420f3290f62657f5f1b067df9d48e");rn initView();rn initSDK();rnrn String[] schools = "四川文理学院", "上海交通大学", "清华大学", "北京大学" ;rn ArrayAdapter adapter = new ArrayAdapter(this,rn android.R.layout.simple_list_item_1, schools);rn schoolsSpinner.setAdapter(adapter);rn schoolsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() rn @Overridern public void onItemSelected(AdapterView arg0, View arg1,rn int position, long id) rn // TODO Auto-generated method stubrn switch (position) rn case 0:rn // 创建四川文理学院的数据库rn mydb = new MyDB(Tab1Activity.this, "scwlxy.db", null, 1);rn db = mydb.getReadableDatabase();rnrn break;rnrn default:rn break;rn rn rnrn @Overridern public void onNothingSelected(AdapterView arg0) rn // TODO Auto-generated method stubrnrn rn );rn rnrn private void initSDK() rn // TODO Auto-generated method stubrnrn try rnrn final Handler handler = new Handler(this);rn EventHandler eventHandler = new EventHandler() rn public void afterEvent(int event, int result, Object data) rn Message msg = new Message();rn msg.arg1 = event;rn msg.arg2 = result;rn msg.obj = data;rn handler.sendMessage(msg);rn rn ;rnrn SMSSDK.registerEventHandler(eventHandler); // 注册短信回调rnrn catch (Exception e) rn e.printStackTrace();rn rnrn rnrn private void initView() rn // TODO Auto-generated method stubrn userNameRegis = (EditText) findViewById(R.id.etUserRegister);rn userPwdRegis = (EditText) findViewById(R.id.etPwdRegister);rn userPhoneRegis = (EditText) findViewById(R.id.etPhoneRegister);rn userGetIdentiRegis = (Button) findViewById(R.id.btngetIdentiCode);rn identiCode = (EditText) findViewById(R.id.etIdentiCode);rn schoolsSpinner = (Spinner) findViewById(R.id.spinnerChooseSchool);rn completeRegis = (Button) findViewById(R.id.btnCompleteRegis);rn userGetIdentiRegis.setOnClickListener(this);rn completeRegis.setOnClickListener(this);rn rnrn @Overridern public void onClick(View v) rn // TODO Auto-generated method stubrnrn switch (v.getId()) rn case R.id.btngetIdentiCode:rn phonenumber = userPhoneRegis.getText().toString().trim();rn usernameStr = userNameRegis.getText().toString();rn userpwdStr = userPwdRegis.getText().toString();rn if ((!TextUtils.isEmpty(usernameStr) & (!TextUtilsrn .isEmpty(userpwdStr)))) rn if (!TextUtils.isEmpty(phonenumber)) rn cd = new ConnectionDetector(getApplicationContext());rn isInternetPresent = cd.isConnectingToInternet();rn if (isInternetPresent) rn Toast.makeText(Tab1Activity.this, "网络已连接", 1000).show();rn SMSSDK.getVerificationCode("86", phonenumber);// 获取短信rn else rn Toast.makeText(Tab1Activity.this, "网络连接失败", 1000)rn .show();rn rn else rn Toast.makeText(Tab1Activity.this, "电话号码不能为空", 1000).show();rn rnrn else rn Toast.makeText(Tab1Activity.this, "请输入完整的用户信息", 1000).show();rn rn break;rn case R.id.btnCompleteRegis:rnrn String number = identiCode.getText().toString().trim();rn if (!TextUtils.isEmpty(number)) rn SMSSDK.submitVerificationCode("86", phonenumber, number);// 验证短信rn // 查看是否已经注册rn String sql1 = "select phone from users ";rn Cursor cursor = db.rawQuery(sql1, null);rn if (cursor.moveToNext()) rnrn String columnNames = cursor.getString(cursorrn .getColumnIndex("name"));rnrn if (columnNames.equals(userNameRegis.getText().toString())) rn Toast.makeText(Tab1Activity.this, "已存在此用户,请重新注册",rn Toast.LENGTH_SHORT).show();rnrn else rnrn String sql2 = "insert into users(name,pwd,phone) values('"rn + usernameStrrn + "','"rn + userpwdStrrn + "','"rn + phonenumber + "')";rn db.execSQL(sql2);rn Toast.makeText(getApplicationContext(), "注册成功!",rn Toast.LENGTH_SHORT).show();rnrn rnrn rnrn Intent intent = new Intent();rn intent.setClass(Tab1Activity.this, LoginActivity.class);rn startActivity(intent);rn else rn Toast.makeText(Tab1Activity.this, "验证码不能为空", 1000).show();rn rn break;rn default:rn break;rn rn rnrn @Overridern protected void onDestroy() rn // TODO Auto-generated method stubrn // 销毁回调监听接口rn SMSSDK.unregisterAllEventHandler();rn super.onDestroy();rn rnrn @Overridern public boolean handleMessage(Message msg) rn // TODO Auto-generated method stubrnrn int event = msg.arg1;rn int result = msg.arg2;rn Object data = msg.obj;rn if (result == SMSSDK.RESULT_COMPLETE) rn System.out.println("--------result" + event);rn // 回调完成rnrn if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) rn // 验证码正确rn Toast.makeText(Tab1Activity.this, "验证码正确", Toast.LENGTH_SHORT)rn .show();rn else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) rn // 正在获取验证码rn Toast.makeText(Tab1Activity.this, "正在获取验证码。。。",rn Toast.LENGTH_SHORT).show();rnrn else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) rn // 返回支持发送验证码的国家列表rn rnrn else rn // ((Throwable) data).printStackTrace();rn // Toast.makeText(MainActivity.this, "验证码错误123",rn // Toast.LENGTH_SHORT).show();rnrn int status = 0;rn try rn ((Throwable) data).printStackTrace();rn Throwable throwable = (Throwable) data;rnrn JSONObject object = new JSONObject(throwable.getMessage());rn String des = object.optString("detail");rn status = object.optInt("status");rn if (!TextUtils.isEmpty(des)) rn Toast.makeText(Tab1Activity.this, des, Toast.LENGTH_SHORT)rn .show();rn return false;rn rn catch (Exception e) rn SMSLog.getInstance().w(e);rn rnrn rn return false;rnrn rnrnrn 论坛

没有更多推荐了,返回首页