要获取Android手机上短信发送的状态,可以通过以下步骤:
- 添加短信发送权限到AndroidManifest.xml文件中:
<uses-permission android:name="android.permission.SEND_SMS"/>
- 在发送短信的代码中,注册一个BroadcastReceiver来接收短信发送状态的广播:
// 创建BroadcastReceiver
private BroadcastReceiver messageSentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
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, "短信内容为空", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
// 手机无法发送短信,可能是飞行模式
Toast.makeText(context, "手机无法发送短信", Toast.LENGTH_SHORT).show();
break;
}
}
};
// 注册BroadcastReceiver
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("SMS_SENT");
registerReceiver(messageSentReceiver, intentFilter);
}
// 发送短信
private void sendSMS(String phoneNumber, String message) {
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
smsManager.sendTextMessage(phoneNumber, null, message, sentPI, null);
}
// 在Activity销毁时注销BroadcastReceiver
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(messageSentReceiver);
}
- 调用sendSMS()方法发送短信,当短信发送状态改变时,BroadcastReceiver会收到相应的广播并处理。
请注意,手机短信发送状态的检测并不是实时的,可能会有延迟。因此,我们在发送短信时只能获取到短信发送的状态,但无法获取到具体的短信接收状态。
要获取 Android 设备上短信的发送状态,您可以使用 BroadcastReceiver
来监听短信发送状态的变化。
首先,在您的 AndroidManifest.xml 文件中添加以下权限和广播接收器声明:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".SmsBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_SENT" />
</intent-filter>
</receiver>
然后,创建一个名为 SmsBroadcastReceiver
的广播接收器类,并实现 onReceive()
方法:
public class SmsBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null) {
switch (intent.getAction()) {
case "android.provider.Telephony.SMS_DELIVER":
// 收到短信
break;
case "android.provider.Telephony.SMS_SENT":
// 短信发送状态
int resultCode = getResultCode();
if (resultCode == Activity.RESULT_OK) {
// 短信发送成功
} else {
// 短信发送失败
}
break;
}
}
}
}
接下来,您需要在您的代码中注册 SmsBroadcastReceiver
广播接收器。可以在 onCreate()
方法中注册,在 onDestroy()
方法中取消注册:
private SmsBroadcastReceiver smsBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smsBroadcastReceiver = new SmsBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_DELIVER");
intentFilter.addAction("android.provider.Telephony.SMS_SENT");
registerReceiver(smsBroadcastReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(smsBroadcastReceiver);
}
这样,您的应用就可以接收到关于短信发送状态的广播通知了。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/142705.html