我在Android上发送和接收二进制短信时遇到问题。我和一个小组在这个项目上工作,我团队中的其他人没有这个问题,但我是。我们不确定原因,但如果重要的话,我们会使用不同的手机运营商。无论如何,我想要做的是至少能够将二进制短信发送到我自己的手机然后接收它,以便测试发送和接收功能是否正常工作。
这是我的主要活动的代码,其中的消息是通过按钮点击事件发送的:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendBinarySMS(View view) {
// Get the default instance of SmsManager
SmsManager smsManager = SmsManager.getDefault();
String phoneNumber = "5558675309";
byte[] smsBody = "Testing testing 123".getBytes();
short port = 1234;
// Send a text based SMS
smsManager.sendDataMessage(phoneNumber, null, port, smsBody, null, null);
}
}
这是我的接收器注册的代码:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter android:priority="10" >
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data
android:scheme="sms"
android:host="*"
android:port="1234" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是我的接收者:
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] messages = null;
String message = "";
String sender = "";
if (bundle == null) {
return;
}
Object[] pdus = (Object[]) bundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
byte[] data = null;
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sender = messages[i].getOriginatingAddress();
data = messages[i].getUserData();
for (int index = 0; index < data.length; index++) {
message += Character.toString((char) data[index]);
}
}
processSMS(sender, message);
}
private void processSMS(String sender, String message) {
System.out.println("Sender: " + sender + ", message = " + message);
}
}
另外,如果这有帮助,我注意到我的手机与队友之间存在一种行为差异。当我第一次点击按钮时(仅第一次),我在日志中得到一个大而奇怪的文本blob,其中涉及电话配置。我怀疑这些可能与问题有关,但我不确定如何处理它们。这是一个样本:
V / TelephonyAutoProfiling:[loadFeatureFromXml] ***从xml加载启动功能 D / TelephonyAutoProfiling:[parse]加载xml V / TelephonyAutoProfiling:[getMatchedProfile]选择文件:/etc/featureset.xml D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_urgent_vmwi,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:legacy_vvm_not_save,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_network_name,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_sms_nbpcd,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:copy_submit_to_uicc,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_sms_pctest,value:false D / TelephonyAutoProfiling:[profileToMap] add - key:ecc_number_in_hidden_menu,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_cause_code_display,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:MAUNAL_TIMEZONE_SETTING_POPUP,value:false D / TelephonyAutoProfiling:[profileToMap] add - key:support_send_burst_dtmf,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:KREncodingScheme,value:false D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_priority_indicator,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:MANAGED_TIME_LOCAL_AREAS,值:450 D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_gfit,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:MANAGED_TIME_SETTING,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:network_duplicate_message,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:sms_signature,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:handle8bit,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_sms_cdg2,value:false D / TelephonyAutoProfiling:[profileToMap] add - key:CHANGE_CDMA_SUBSCRIPTION_TO_RUIM,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_eri,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:gsm_dcs_over_cdma_for_kddi,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:SUPPORT_E911_FOR_VOLTE,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:support_network_change_auto_retry,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:set_sms_character_encoding,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:support_vzw_Los_upgrade,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_privacy_indicator,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:do_not_save_sms_to_isis_short_code,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:sms_dan_send,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:support_emoji_in_concat_message,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:email_over_sms,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vvm3_wap_push,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:SUPPORT_ENHANCED_VOICECALL,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:support_svlte,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_kr_testbed_mms_receive,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:should_write_messages_for_vzw,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:NITZ_WAITING_TIMEOUT,值:10000 D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_snc_email_sms,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:app_directed_sms,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:MANUAL_SELECTION_WITH_RAT,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_sms_fromvtext,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:not_support_sms_nbpcd,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:uicc_csim,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cmas,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:revise_invalid_cdma_wappush_pdu,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:ota_for_vzw,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:store_on_usim_storage,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:SUPPORT_INFO_FOR_IMS,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:support_emergency_callback_mode_for_gsm,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_modem_data_logging,value:false D / TelephonyAutoProfiling:[profileToMap] add - key:sms_tracker_multipart_check,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:SUPPORT_UPDATE_DEVICE_INFO,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:control_uicc_storage,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:increase_mo_msg_queue_limit_vzw,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_sms_retry_scheme,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:vzw_set_nitz,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:voicemail_urgent,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:seperate_processing_sms_uicc,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:sms_gcf_config,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_sms_display_mode,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:cdma_sms_callback,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:use_update_for_copy2sim,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:insert_boottime_for_sim_mosms,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:KSC5601Decoding,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:sms_over_lgims,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:ems_segment_timer,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:parse_email_on_uicc,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:increase_user_permit_sent_sms_max_count,value:true D / TelephonyAutoProfiling:[profileToMap] add - key:support_assisted_dialing,value:true V / TelephonyAutoProfiling:来自xml的[loadFeatureFromXml]加载功能完成:{cdma_urgent_vmwi = true,vzw_network_name = true,legacy_vvm_not_save = true,cdma_sms_nbpcd = true,vzw_sms_pctest = false,copy_submit_to_uicc = true,ecc_number_in_hidden_menu = true,cdma_cause_code_display = true,support_send_burst_dtmf = true, MAUNAL_TIMEZONE_SETTING_POPUP =假,KREncodingScheme =假,vzw_gfit =真,MANAGED_TIME_LOCAL_AREAS = 450,cdma_priority_indicator =真,MANAGED_TIME_SETTING =真,network_duplicate_message =真,sms_signature =真,cdma_sms_cdg2 =假,handle8bit =真,vzw_eri =真,CHANGE_CDMA_SUBSCRIPTION_TO_RUIM =真,gsm_dcs_over_cdma_for_kddi =真,support_network_change_auto_retry =真,SUPPORT_E911_FOR_VOLTE =真,set_sms_character_encoding =真,support_vzw_Los_upgrade =真,cdma_privacy_indicator =真,support_emoji_in_concat_message =真,sms_dan_send =真,do_not_save_sms_to_isis_short_code =真,email_over_sms =真,SUPPORT_ENHANCED_VOICECALL =真,vvm3_wap_push =真,support_svlte =真,cdma_kr_testbed_mms_receive =真,should_write_messages_for_vzw =真,NITZ_WAITING_TIMEOUT = 10000,vzw_snc_email_sms =真,app_directed_sms =真,MANUAL_SELECTION_WITH_RAT =真,vzw_sms_fromvtext =真,not_support_sms_nbpcd =真,uicc_csim =真,CMAS =真,ota_for_vzw =真,revise_invalid_cdma_wappush_pdu =真, store_on_usim_storage =真,SUPPORT_INFO_FOR_IMS =真,support_emergency_callback_mode_for_gsm =真,vzw_modem_data_logging =假,sms_tracker_multipart_check =真,SUPPORT_UPDATE_DEVICE_INFO =真,control_uicc_storage =真,increase_mo_msg_queue_limit_vzw =真,vzw_sms_retry_scheme =真,vzw_set_nitz =真,voicemail_urgent =真,seperate_processing_sms_uicc =真,sms_gcf_config = true,cdma_sms_display_mode = true,cdma_sms_callback = true,use_update_for_copy2sim = true,sms_over_lgims = true,KSC5601Decoding = true,insert_boottime_for_sim_mosms = true,ems_segment_timer = true,parse_email_on_uicc = true,support_assisted_dialing = true,increase_user_permit_sent_sms_max_count = true}