gpt4 book ai didi

android - SMS 发送在 2.2 Froyo 中有效,但在 ICS 4.0 中无效

转载 作者:行者123 更新时间:2023-11-30 03:39:38 28 4
gpt4 key购买 nike

我正在开发一个发送短信并接收一些响应的应用程序。

现在的问题是 SMS 发送在 2.2 froyo 中工作正常,但在 ICS 4.0 中无法正常工作。

我在我的代码中完成了以下操作:

  • 在我的 Manifest tp 中添加了接收和发送 SMS 的权限。
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
  • 这是我在按钮中用来发送短信的代码。
    public void sendSms(String phoneNumber,String sms) {      
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null ,sms, null, null);
    }
  • 最佳答案

    @rciovati 感谢您的建议,我使用 SMS 方法添加了两个待处理的 Intent ,现在一切正常
    这是代码

    SmsManager smsManager=SmsManager.getDefault();
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    public void sendSms(String phoneNumber,String sms){
    PendingIntent pi = PendingIntent.getActivity(this, 0,
    new Intent(this, myService.class), 0);
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
    new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
    new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent arg1) {
    switch (getResultCode())
    {
    case Activity.RESULT_OK:
    Toast.makeText(getBaseContext(), "SMS sent",
    Toast.LENGTH_SHORT).show();
    break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
    Toast.makeText(getBaseContext(), "Generic failure",
    Toast.LENGTH_SHORT).show();
    break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
    Toast.makeText(getBaseContext(), "No service",
    Toast.LENGTH_SHORT).show();
    break;
    case SmsManager.RESULT_ERROR_NULL_PDU:
    Toast.makeText(getBaseContext(), "Null PDU",
    Toast.LENGTH_SHORT).show();
    break;
    case SmsManager.RESULT_ERROR_RADIO_OFF:
    Toast.makeText(getBaseContext(), "Radio off",
    Toast.LENGTH_SHORT).show();
    break;
    }
    }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent arg1) {
    switch (getResultCode())
    {
    case Activity.RESULT_OK:
    Toast.makeText(getBaseContext(), "SMS delivered",
    Toast.LENGTH_SHORT).show();
    break;
    case Activity.RESULT_CANCELED:
    Toast.makeText(getBaseContext(), "SMS not delivered",
    Toast.LENGTH_SHORT).show();
    break;
    }
    }
    }, new IntentFilter(DELIVERED));


    smsManager.sendTextMessage(phoneNumber, null, sms, sentPI, deliveredPI);

    }

    关于android - SMS 发送在 2.2 Froyo 中有效,但在 ICS 4.0 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15998189/

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