From: snickersun Date: Sat, 18 Jul 2020 07:12:55 +0000 (+0800) Subject: 1、把短信接收的action注册 改为静态注册,减少信息丢失的可能性。 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.js?a=commitdiff_plain;h=abb9c25d45f91de74eb8fcfdd3d10dabd9fea1aa;p=protocol_srv.git 1、把短信接收的action注册 改为静态注册,减少信息丢失的可能性。 2、修改NettyConnection 提前记录日志,减少由于发送错误造成的日志无法记录的问题。 --- diff --git a/app/app.iml b/app/app.iml index 586ced2e..cda70e38 100644 --- a/app/app.iml +++ b/app/app.iml @@ -130,6 +130,7 @@ + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a14fc125..6321e58a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -126,6 +126,15 @@ android:authorities="com.enqualcomm.support" android:exported="true" /> + + + + + + + diff --git a/app/src/main/java/com/enqualcomm/support/Constants.java b/app/src/main/java/com/enqualcomm/support/Constants.java index 22bb3472..94669802 100644 --- a/app/src/main/java/com/enqualcomm/support/Constants.java +++ b/app/src/main/java/com/enqualcomm/support/Constants.java @@ -218,7 +218,8 @@ public interface Constants { public static final int NEW_MEDIA_PUSH = 100; - + + public static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; public static final String OTHER_ALARM_END = "com.enqualcomm.support.OTHER_ALARM_END"; public static final String OTHER_ALARM_START = "com.enqualcomm.support.OTHER_ALARM_START"; @@ -240,7 +241,13 @@ public interface Constants { public static final String RESTORE_EXTRA_WIPE_EXTERNAL_STORAGE = "android.intent.extra.WIPE_EXTERNAL_STORAGE"; public static final String RESTORE_REASON = "android.intent.extra.REASON"; - + + + public static final String ACTION_START_FOTA_UPDATE="com.ic.action.startfotaUpdate"; + public static final String ACTION_SMSCMD="com.enqualcomm.support.SMSCMD"; + public static final String ACTION_SMSCMD_Reply="com.ic.action.fotaUpdateReply"; + public static final String KeyReplyData="reply"; + public static final int RESULT_ERROR = 500; public static final int RESULT_OK = 200; diff --git a/app/src/main/java/com/enqualcomm/support/CoreService.java b/app/src/main/java/com/enqualcomm/support/CoreService.java index 70795ce5..10e64054 100644 --- a/app/src/main/java/com/enqualcomm/support/CoreService.java +++ b/app/src/main/java/com/enqualcomm/support/CoreService.java @@ -1110,7 +1110,6 @@ public class CoreService extends Service { unregisterReceiver(this.screenOnReceiver); unregisterReceiver(this.AtCallbackBroadcastReceiver); batteryUtil.unregisterBroadReceiver(); - smsManager.unregisterBroadReceiver(); } diff --git a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java index b71917b4..654f5c23 100644 --- a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java +++ b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java @@ -44,7 +44,12 @@ import rx.Observable; import rx.functions.Action1; import static com.enqualcomm.support.Config.upload_logs_via_ftp; +import static com.enqualcomm.support.Constants.ACTION_SMSCMD; +import static com.enqualcomm.support.Constants.ACTION_SMSCMD_Reply; +import static com.enqualcomm.support.Constants.ACTION_SMS_RECEIVED; +import static com.enqualcomm.support.Constants.ACTION_START_FOTA_UPDATE; import static com.enqualcomm.support.Constants.DIALOG_ACTION; +import static com.enqualcomm.support.Constants.KeyReplyData; import static com.enqualcomm.support.Constants.contact; import static com.enqualcomm.support.utils.DeviceInfoUtil.getDeviceVersion; import static com.enqualcomm.support.utils.SimSignalUtil.send_at_cmd; @@ -52,18 +57,62 @@ import static com.enqualcomm.support.utils.SimSignalUtil.send_at_setImei0; import static com.enqualcomm.support.utils.SimSignalUtil.send_at_setImei1; -public class ICSmsManager { - private static String ACTION_SMSCMD="com.enqualcomm.support.SMSCMD"; - private static String ACTION_SMSCMD_Reply="com.ic.action.fotaUpdateReply"; - private static String KeyReplyData="reply"; +public class ICSmsManager extends BroadcastReceiver{ + private static String cell_number_to_reply=""; //this number is use for ACTION_SMSCMD_Reply - private static String ACTION_START_FOTA_UPDATE="com.ic.action.startfotaUpdate"; + private static long preTimeMillis; private Context mContext = null; Runtime runtime; - ICSmsManager.SmsReceiver smsReceiver; ReplayLocation replayLocation=new ReplayLocation(); + + @Override + public void onReceive(Context context, Intent intent) { + + + String action = intent.getAction(); + if (ACTION_SMSCMD.equals(action)) { + String cmd = intent.getStringExtra("smsCmd"); + handleSMS_CMD("", cmd); + }else if (ACTION_SMSCMD_Reply.equals(action)){ + String replydata=intent.getStringExtra(KeyReplyData); + sendSMS(cell_number_to_reply,replydata); + cell_number_to_reply=""; + }else if (ACTION_SMS_RECEIVED.equals(action)) { + long currenttime = System.currentTimeMillis(); + if (currenttime - ICSmsManager.preTimeMillis > 30000L) { + ICSmsManager.preTimeMillis = currenttime; + (new Thread(new Runnable() { + public void run() { + SystemClock.sleep(3000L); + if (intent == null || intent.getAction() == null) return; + + //长短信会被分拆送来,因此可能会有多个PDU。Protocol Description Unit是短信的工业标准。  + Object[] pduArray = (Object[]) intent.getExtras().get("pdus"); + ICLogger.i("pdu number=" + pduArray.length); + String message = ""; + String from = null; + for (int i = 0; i < pduArray.length; i++) { + SmsMessage sms = SmsMessage.createFromPdu((byte[]) pduArray[i]);//将PDU转换为SmsMessage格式 + ICLogger.i("From : " + sms.getDisplayOriginatingAddress()); + ICLogger.i("SMS : " + sms.getDisplayMessageBody()); + from = sms.getDisplayOriginatingAddress(); + message += sms.getDisplayMessageBody(); + } + ICLogger.i("收到" + from + "短信:" + message); +// Toast.makeText(context, "收到" + from + "短信:" + message, Toast.LENGTH_LONG).show(); + if (handleSMS_CMD(from, message) == false) { + //说明不是sms命令,提示短信接收。 + ContextProxy.sendMsgBroadcast(context, message, 1); + } + } + })).start(); + } + } + + } + public class ReplayLocation implements LocationClient.LocationResponseUrl { @Override @@ -82,9 +131,6 @@ public class ICSmsManager { public ICSmsManager( Runtime _runtime) { this.runtime = _runtime; this.mContext = runtime.getContextProxy().context; - this.smsReceiver = new ICSmsManager.SmsReceiver(); - - registerBroadReceiver(); } public void sendSMS(String phonenumber, String msg) { @@ -112,38 +158,8 @@ public class ICSmsManager { context.sendBroadcast(intent); } - public void registerBroadReceiver() { - - IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); - intentFilter.setPriority(2147483647); - this.mContext.registerReceiver(this.smsReceiver, intentFilter); - - intentFilter = new IntentFilter(); - intentFilter.addAction(ACTION_SMSCMD); - intentFilter.addAction(ACTION_SMSCMD_Reply); - (this.runtime.getContextProxy()).context.registerReceiver(this.mSMSCMD_BroadcastReceiver, intentFilter); - } - public void unregisterBroadReceiver() { - (this.runtime.getContextProxy()).context.unregisterReceiver(this.mSMSCMD_BroadcastReceiver); - (this.runtime.getContextProxy()).context.unregisterReceiver(this.smsReceiver); - } - private final BroadcastReceiver mSMSCMD_BroadcastReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - if (ACTION_SMSCMD.equals(action)) { - String cmd = intent.getStringExtra("smsCmd"); - handleSMS_CMD("", cmd); - }else if (ACTION_SMSCMD_Reply.equals(action)){ - String replydata=intent.getStringExtra(KeyReplyData); - sendSMS(cell_number_to_reply,replydata); - cell_number_to_reply=""; - } - } - }; //1:短信设置服务器 //#ip#=198.11.183.28,5088# //回复 ip set ok @@ -887,35 +903,6 @@ public class ICSmsManager { } public void onReceive(Context context, Intent intent) { - long currenttime = System.currentTimeMillis(); - if (currenttime - ICSmsManager.preTimeMillis > 30000L) { - ICSmsManager.preTimeMillis = currenttime; - (new Thread(new Runnable() { - public void run() { - SystemClock.sleep(3000L); - if(intent == null || intent.getAction() == null ) return; - - //长短信会被分拆送来,因此可能会有多个PDU。Protocol Description Unit是短信的工业标准。  - Object[] pduArray = (Object[])intent.getExtras().get("pdus"); - ICLogger.i("pdu number=" + pduArray.length); - String message = ""; - String from = null; - for(int i = 0 ; i < pduArray.length; i ++){ - SmsMessage sms = SmsMessage.createFromPdu((byte[])pduArray[i]);//将PDU转换为SmsMessage格式 - ICLogger.i("From : " + sms.getDisplayOriginatingAddress()); - ICLogger.i("SMS : " + sms.getDisplayMessageBody()); - from = sms.getDisplayOriginatingAddress(); - message += sms.getDisplayMessageBody(); - } - ICLogger.i("收到" + from + "短信:" + message); -// Toast.makeText(context, "收到" + from + "短信:" + message, Toast.LENGTH_LONG).show(); - if (handleSMS_CMD(from,message)==false){ - //说明不是sms命令,提示短信接收。 - ContextProxy.sendMsgBroadcast(context,message,1); - } - } - })).start(); - } } diff --git a/app/src/main/java/com/enqualcomm/support/utils/NettyConnection.java b/app/src/main/java/com/enqualcomm/support/utils/NettyConnection.java index 34c49690..cfae0923 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/NettyConnection.java +++ b/app/src/main/java/com/enqualcomm/support/utils/NettyConnection.java @@ -125,11 +125,6 @@ public class NettyConnection implements Connection { ICLogger.log(TAG,msg); } - private void log(String funcName, String msg) { - ICLogger.log(TAG,funcName,msg); - } - - public boolean isServerConnected(){ return (RxSocket.getInstance().getSocketStatus()== CONNECTED)?true:false; @@ -150,6 +145,7 @@ public class NettyConnection implements Connection { return false; }else{ log("send "+cmd); + if (Config.LOG_TOFILE) ICLogger.logToFile("APILog"," sent " + cmd); //向RxSocket写数据 //示例数据:IWAP00867155030001063# byte[] test = cmd.getBytes(); @@ -167,7 +163,6 @@ public class NettyConnection implements Connection { } ); - if (Config.LOG_TOFILE) ICLogger.logToFile("APILog"," sent " + cmd); } @@ -188,6 +183,7 @@ public class NettyConnection implements Connection { return false; }else{ log("sendBytes "+new String(cmdArray)); + if (Config.LOG_TOFILE) ICLogger.logToFile("APILog"," sentBytes " + new String(cmdArray)); //向RxSocket写数据 //示例数据:IWAP00867155030001063# ByteBuffer byteBuffer = ByteBuffer.wrap(cmdArray); @@ -204,7 +200,6 @@ public class NettyConnection implements Connection { } ); - if (Config.LOG_TOFILE) ICLogger.logToFile("APILog"," sentBytes " + new String(cmdArray)); }