gpt4 book ai didi

java - 当我的应用程序设置为默认时,手机本身的短信应用程序被禁用

转载 作者:行者123 更新时间:2023-11-30 08:48:03 25 4
gpt4 key购买 nike

在 android +API19 中:我制作了一个短信应用程序,我只需要在其中接收短信,但是当我的应用程序设置为默认时,手机本身的短信应用程序被禁用并且无法发送消息。我唯一需要的是接收消息。但是因为手机的消息应用程序被禁用,无法发送消息,请指导,我该怎么办?我只需要接收短信!

<receiver
android:name=".ReceiverSms"
android:permission="android.permission.BROADCAST_SMS"
android:enabled="true"
>
<intent-filter android:priority="999999">
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />

</intent-filter>
</receiver>

代码:

public class ReceiverSms extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
HelperWork.toastShower(context, "Runned");

boolean pswDare = true;
Bundle extras = intent.getExtras();
if (extras == null) {
return;
}
Object[] smsExtras = (Object[]) extras.get(SmsConstant.PDUS);
ContentResolver contentResolver = context.getContentResolver();
Uri smsUri = Uri.parse(SmsConstant.SMS_URI);
String body = null;
String address = null;
for (Object smsExtra: smsExtras) {
byte[] smsBytes = (byte[]) smsExtra;
SmsMessage smsMessage = SmsMessage.createFromPdu(smsBytes);
HelperWork.toastShower(context, body);
body = smsMessage.getMessageBody();
address = smsMessage.getOriginatingAddress();

// do other somthing
}
ContentValues values = new ContentValues();
values.put(SmsConstant.COLUMN_ADDRESS, address);
values.put(SmsConstant.COLUMN_BODY, body);
Uri uri = contentResolver.insert(smsUri, values);

}
}

最佳答案

通过查看 android developer's blog

Other apps that only want to read new messages can instead receive the SMS_RECEIVED_ACTION broadcast intent when a new SMS arrives. However, only the app that receives the SMS_DELIVER_ACTION broadcast (the user-specified default SMS app) is able to write to the SMS Provider defined by the android.provider.Telephony class and subclasses.

因此,如果您只想接收 SMS,则只使用 SMS_RECEIVED_ACTION 并删除 SMS_DELIVER_ACTION。您不必为了仅接收短信而将您的应用设置为默认应用。

关于java - 当我的应用程序设置为默认时,手机本身的短信应用程序被禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32137226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com