我已经浏览了 Stackoverflow.com 中提供的几种解决方案,用于以编程方式在 Android 设备上获取 SMS 的发送报告。我已经实现(从 Stackoverflow.com 上的一位贡献者那里借来)2 个广播接收器,一个用于 PendingIntent = "SMS_SENT",另一个用于 PendingIntent="SMS_DELIVERED"。运行应用程序后,会发送文本消息,我从 sendReceiver 收到“SMS SENT”,但没有从 DeliverReceiver 收到任何响应。同样的程序显然适用于 Stackoverflow.com 上的贡献者。我的设备是三星 Galaxy S5,6.0.1 操作系统。我已经尝试了几篇帖子的代码,但它们的行为都相似。在设备上也没有打开/关闭交付报告的选项。感谢一些指导。这是代码:
public class DeliverReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
System.out.println("+++ DeliverReceiver resultcode = "+getResultCode());
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS Delivered", LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
makeText(context, "SMS Not Delivered",
LENGTH_SHORT).show();
break;
}
}
}
public class SentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
System.out.println("+++ SentReceiver resultcode = "+getResultCode());
switch (getResultCode()) {
case Activity.RESULT_OK:
makeText(context, "+++sms_sent+++", LENGTH_SHORT).show();
//startActivity(new Intent(SendSMS.this, ChooseOption.class));
//overridePendingTransition(R.anim.animation, R.anim.animation2);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
makeText(context, "Generic failure", LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
makeText(context, "No service", LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
makeText(context, "Null PDU", LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
makeText(context, "Radio off", LENGTH_SHORT).show();
break;
}
}
}
public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 101;
private BroadcastReceiver sendBroadcastReceiver = new SentReceiver();
private BroadcastReceiver deliveryBroadcastReceiver = new DeliverReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission
.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
sendSMS("6785459898","+++ Testing SMS +++");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
try {
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
try {
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendSMS(String ph, String msg){
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), PendingIntent.FLAG_UPDATE_CURRENT);
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(ph, null, msg, sentPI, deliveredPI);
}
}
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.BROADCAST_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="com.android.internal.telephony.sendsms5.DeliverReceiver">
<intent-filter>
<action android:name="SMS_DELIVERED" />
</intent-filter>
</receiver>
<receiver android:name="com.android.internal.telephony.sendsms5.SentReceiver">
<intent-filter>
<action android:name="SMS_SENT" />
</intent-filter>
</receiver>
</application>