我已经通过Stackoverflow.com提供的几个解决方案,以编程方式获取Android设备上的SMS发送报告。我已实现(借用Stackoverflow.com上的一个贡献者)2个广播接收器,一个用于PendingIntent =“SMS_SENT”,另一个用于PendingIntent =“SMS_DELIVERED”。运行应用程序时,会发送短信,我从sentReceiver收到“短信发送”,但我没有收到来自deliverReceiver的任何回复。同样的程序显然适用于Stackoverflow.com上的贡献者。我的设备是三星Galaxy S5,6.0.1 os。我尝试了几个帖子中的代码,但所有这些代码的行为都相似。在设备上,没有选项可以打开/关闭传送报告。感谢一些指导。这是代码:
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>
答案 0 :(得分:0)
尝试在某些操作系统级别上删除intent-filter
,在这种情况下,隐式意图不起作用