我有一个奇怪的问题。我开发了发送和接收短信的android应用程序 通过一个特定的端口。 问题是收到的消息有时是空的,有时它没有传递(在某些情况下它被正确传递)。 我认为这取决于接收方的电话类型。
以下是代码:
这是发送代码:
private void sendSMS(String phoneNO, String smsMsg){
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
Intent intent=new Intent(SENT);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
intent, 0);
Intent intent2=new Intent(DELIVERED);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
intent2, 0);
SmsManager smsMgr=SmsManager.getDefault();
smsMgr.sendDataMessage(phoneNO, null, (short) 5001, smsMsg.getBytes(), sentPI,deliveredPI);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
}
这是接收代码:
public void onReceive(Context context, Intent intent) {
Bundle extras=intent.getExtras();
if(extras!=null){
Object[] smsExtra=(Object[]) extras.get(SMS_EXTRA_NAME);
SmsDbHelper dbHelper=new SmsDbHelper(context);
SQLiteDatabase db=dbHelper.getWritableDatabase();
int i=0;
for(i=0;i<smsExtra.length;++i){
SmsMessage sms=SmsMessage.createFromPdu((byte[])smsExtra[i]);
String message=sms.getMessageBody().toString();
String phoneNO=sms.getOriginatingAddress();
ContentValues values=new ContentValues();
values.put(SmsMsgTable.COLUMN_NAME_SENDER, phoneNO);
values.put(SmsMsgTable.COLUMN_NAME_CONTENTS, message);
values.put(SmsMsgTable.COLUMN_NAME_STATUS, "NEW");
values.put(SmsMsgTable.COLUMN_NAME_MSGTYPE, 0);
Calendar c=Calendar.getInstance();
java.util.Date d=c.getTime();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
values.put(SmsMsgTable.COLUMN_NAME_DATE, df.format(d));
db.insert(SmsMsgTable.TABLE_NAME, null, values);
}
db.close();
dbHelper.close();
}
}
以下是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sabafon.securesms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sabafon.securesms.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sabafon.securesms.DisplayInbox"
android:label="@string/title_activity_display_inbox"
android:parentActivityName="com.sabafon.securesms.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.sabafon.securesms.MainActivity" />
</activity>
<activity
android:name="com.sabafon.securesms.SecurityPreferenceActivity"
android:label="@string/title_activity_security_preference"
android:parentActivityName="com.sabafon.securesms.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.sabafon.securesms.MainActivity" />
</activity>
<receiver android:name="utilityclasses.SMSReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="5001"/>
</intent-filter>
</receiver>
<activity
android:name="com.sabafon.securesms.DisplayMessageDetails"
android:label="@string/title_activity_display_message_details"
android:parentActivityName="com.sabafon.securesms.DisplayInbox" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.sabafon.securesms.DisplayInbox" />
</activity>
<activity
android:name="com.sabafon.securesms.DisplayOutbox"
android:label="@string/title_activity_display_outbox"
android:parentActivityName="com.sabafon.securesms.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.sabafon.securesms.MainActivity" />
</activity>
</application>
</manifest>