在Android开发中,监听短信发送状态是一个常见需求,实现这一功能需要通过广播接收器(BroadcastReceiver)来监听系统发送的短信广播,以下是详细的步骤和代码示例:
一、添加权限和依赖项
需要在AndroidManifest.xml
文件中添加发送短信的权限:
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
需要在应用的build.gradle
文件中添加必要的依赖项:
dependencies { implementation 'com.android.support:support-annotations:28.0.0' }
二、创建发送短信的方法
在MainActivity
中创建一个方法,用于发送短信并设置监听器:
public class MainActivity extends AppCompatActivity { private TextView status; private EditText phone, content; private Button sendButton; private myServiceReceiver mReceiver01, mReceiver02; private String SMS_SEND_ACTION = "SMS_SEND_ACTION"; private String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); status = findViewById(R.id.textview01); phone = findViewById(R.id.phone); content = findViewById(R.id.smscontent); sendButton = findViewById(R.id.send); mReceiver01 = new myServiceReceiver(); mReceiver02 = new myServiceReceiver(); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String phoneNumber = phone.getText().toString().trim(); String message = content.getText().toString().trim(); sendSMS(phoneNumber, message); } }); } private void sendSMS(String phoneNumber, String message) { SmsManager smsManager = SmsManager.getDefault(); Intent itSend = new Intent(SMS_SEND_ACTION); Intent itDeliver = new Intent(SMS_DELIVERED_ACTION); PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, itSend, 0); PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0, itDeliver, 0); smsManager.sendTextMessage(phoneNumber, null, message, sendPI, deliverPI); status.setText("短信正在发送中..."); } }
三、注册并创建监听发送状态的BroadcastReceiver
创建一个自定义的BroadcastReceiver
类,用于监听短信发送状态:
public class myServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context, "短信发送失败,通用错误", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(context, "短信发送失败,无服务", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(context, "短信发送失败,PDU为空", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(context, "短信发送失败,无线电关闭", Toast.LENGTH_SHORT).show(); break; } } }
四、动态注册和反注册广播接收器
在MainActivity
中动态注册和反注册广播接收器:
@Override protected void onResume() { super.onResume(); registerReceiver(mReceiver01, new IntentFilter(SMS_SEND_ACTION)); registerReceiver(mReceiver02, new IntentFilter(SMS_DELIVERED_ACTION)); } @Override protected void onPause() { super.onPause(); unregisterReceiver(mReceiver01); unregisterReceiver(mReceiver02); }
五、相关问题与解答
问题1:如何确保短信发送状态能够被正确监听?
答:确保在AndroidManifest.xml
文件中声明了所需的权限,并在MainActivity
中正确注册和反注册广播接收器,检查BroadcastReceiver
中的onReceive
方法是否正确处理不同的结果码。
问题2:如何处理短信发送失败的情况?
答:在BroadcastReceiver
的onReceive
方法中,根据getResultCode()
返回的结果码进行处理,对于RESULT_ERROR_GENERIC_FAILURE
,可以提示用户“短信发送失败,通用错误”,对于其他错误码,也可以进行相应的处理。
到此,以上就是小编对于“android监听短信状态”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/638876.html