【问题标题】:Android SMS Delivery Intent always return -1Android SMS Delivery Intent 总是返回 -1
【发布时间】:2014-03-06 04:00:13
【问题描述】:

我在 Android 上遇到了一个奇怪的问题。当我通过我的应用程序发送短信时,我想监听传递意图。这是我到目前为止所做的:

private void shootSMS(String number,long currentTime){
    SmsManager smsManager = SmsManager.getDefault();

    Intent sentIntent = new Intent(SentBroadcastReceiver.SENT_ACTION);
    Intent deliveredIntent = new Intent(DeliveredBroadcastReceiver.DELIVERED_ACTION);

    Bundle b = new Bundle();
    b.putString("num",number);
    b.putString("record_time",currentTime+"");

    sentIntent.putExtras(b);
    deliveredIntent.putExtras(b);


    PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this,(int)currentTime,sentIntent,PendingIntent.FLAG_ONE_SHOT);
    PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(this,(int)currentTime,deliveredIntent,PendingIntent.FLAG_ONE_SHOT);
    smsManager.sendTextMessage(number,null,getString(R.string.app_name),sentPendingIntent,deliveredPendingIntent);

}

现在的问题是,当我的广播接收器的onReceive() 方法被调用时,我调用getResultCode() 方法,它总是返回-1!即使手机关机,也无法追踪短信是否送达!

我用 GoSMSPro 检查了号码并发送了一条失败的短信。有趣的是,当我将手机置于飞行模式时,我得到一个等于 2 的结果代码,即 SmsManager.RESULT_ERROR_RADIO_OFF

现在的问题是这里出了什么问题?

【问题讨论】:

  • 我也想知道这个。即使我发送到固定电话或假号码,我总是得到 Result.OK。这什么时候会返回错误?我还没有见过这样的案例。
  • 你最终弄清楚发生了什么吗?
  • @n0rm9n 其实没有,好像是运营商的问题,我没找到解决办法!

标签: android-intent sms


【解决方案1】:

sendTextMessage 上的文档指出:

sentIntent:如果不为NULL,则在消息发送成功或失败时广播此PendingIntent。结果代码将为 Activity.RESULT_OK...

如果你去看看Activity.RESULT_OK,这个值是-1。

所以-1的resultCode实际上是RESULT_OK。

一般来说,最好不要查看数值,而是尝试找出它们所代表的常数。

【讨论】:

  • 我在我的代码中做的完全一样,我只是说它返回 -1 让事情变得更明亮,我已经使用了 Activity_RESULT_OK ,但我认为不会有任何区别!谢谢试图提供帮助,但没有解决我的问题
  • 考虑编辑您的问题,向我们展示您的代码的接收方。正如我上面所说,Activity.RESULT_OK 是 -1,所以说它返回 -1 就是说结果是 RESULT_OK。我认为您不了解结果的含义,但是如果没有那方面的代码,则无法分辨。
  • 是的!这就是问题出现的地方,即使文本消息没有在我的 BroadcastReceiver 中传递,我也得到了 RESULT_OK ,只是一个简单的开关
【解决方案2】:

这样做:

private void sendSms(String phonenumber, String message) {
    SmsManager manager = SmsManager.getDefault();

    PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(
            SMS_SENT), 0);
    PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0,
            new Intent(SMS_DELIVERED), 0);

        int length = message.length();

        if (length > MAX_SMS_MESSAGE_LENGTH) {
            ArrayList<String> messagelist = manager.divideMessage(message);

            ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
            ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

            int numParts = messagelist.size();
            ;
            for (int i = 0; i < numParts; i++) {
                sentIntents.add(PendingIntent.getBroadcast(this, 0,
                        new Intent(
                                SMS_SENT), 0));
                deliveryIntents.add(PendingIntent.getBroadcast(this, 0,
                        new Intent(SMS_DELIVERED), 0));
            }

            manager.sendMultipartTextMessage(phonenumber, null,
                    messagelist, sentIntents, deliveryIntents);
        } else {
            manager.sendTextMessage(phonenumber, null, message, piSend,
                    piDelivered);
        }
}  

和您的接收方:
注意使用“Activity.RESULT_OK”代替数字

private BroadcastReceiver deliveredreceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String info = "Delivery information: ";

        switch (getResultCode()) {
        case Activity.RESULT_OK:
            info += "delivered";
            break;
        case Activity.RESULT_CANCELED:
            info += "not delivered";
            break;
        }

        Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
    }
};  

权限:

 <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
 <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
 <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>  

此权限用于发送和接收短信。

【讨论】:

  • 我在我的代码中做的完全一样,我只是说它返回 -1 让事情更明亮,我不需要划分消息也不需要使用待处理意图数组,因为我的消息长度是大约5-6个字符! GoSMS 和默认短信应用程序等其他应用程序确定它并立即给出失败状态!谢谢,但这并没有解决我的问题
【解决方案3】:

试试这个方法: 要发送短信,请使用 SMS Manager 中的 sendTextMessage。

 sendTextMessage(String destinationAddress, String  scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)

第一个 Pending Intent 参数 (sentIntent) 在消息发送成功或失败时触发。接收此 Intent 的广播接收器的结果代码将是以下之一。

Activity.RESULT_OK - To indicate a successful transmission.
Sms.Manager.RESULT_ERROR_GENERIC_FAILURE - To indicate a nonspecific failure
Sms.Manager.RESULT_ERROR_RADIO_OFF - To indicate the phone radio is turned off
Sms.Manager.RESULT_ERROR_NULL_PDU - To indicate a PDU failure.
SmsManager.RESULT_ERROR_NO_SERVICE - To indicate that no cellular service is currently available.

第二个 Pending 参数(deliveryIntent)只有在收件人收到您的 SMS 消息后才会触发。

发送后和传递事件通过 Toast 显示适当的消息。

activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
     >

 <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pn"
    android:textSize="15sp"    
    android:textColor="@android:color/white"
    android:background="@android:color/black"
    />

 <EditText
     android:id="@+id/phno"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@android:color/darker_gray"
     android:ems="10" >        
 </EditText>
 <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/msg"
    android:textSize="15sp"    
    android:textColor="@android:color/white"
    android:background="@android:color/black"
    />
  <EditText
        android:id="@+id/smstxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
          android:background="@android:color/darker_gray"
        android:lines="5"
        android:gravity="top" />  
 <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/b1" /> 


   </LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
 Button btnSend;
 EditText etPhoneNo;
 EditText etMsg;

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  etPhoneNo = (EditText) findViewById(R.id.phno);
  etMsg = (EditText) findViewById(R.id.smstxt);
  btnSend = (Button) findViewById(R.id.send);

   btnSend.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    String phoneNo = etPhoneNo.getText().toString();
    String msg = etMsg.getText().toString();
    try {

      String SENT = "sent";
     String DELIVERED = "delivered";

      Intent sentIntent = new Intent(SENT);
     /*Create Pending Intents*/
     PendingIntent sentPI = PendingIntent.getBroadcast(
       getApplicationContext(), 0, sentIntent,
       PendingIntent.FLAG_UPDATE_CURRENT);

      Intent deliveryIntent = new Intent(DELIVERED);

      PendingIntent deliverPI = PendingIntent.getBroadcast(
       getApplicationContext(), 0, deliveryIntent,
       PendingIntent.FLAG_UPDATE_CURRENT);
     /* Register for SMS send action */
     registerReceiver(new BroadcastReceiver() {

       @Override
      public void onReceive(Context context, Intent intent) {
       String result = "";

        switch (getResultCode()) {

        case Activity.RESULT_OK:
        result = "Transmission successful";
        break;
       case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
        result = "Transmission failed";
        break;
       case SmsManager.RESULT_ERROR_RADIO_OFF:
        result = "Radio off";
        break;
       case SmsManager.RESULT_ERROR_NULL_PDU:
        result = "No PDU defined";
        break;
       case SmsManager.RESULT_ERROR_NO_SERVICE:
        result = "No service";
        break;
       }

        Toast.makeText(getApplicationContext(), result,
         Toast.LENGTH_LONG).show();
      }

      }, new IntentFilter(SENT));
     /* Register for Delivery event */
     registerReceiver(new BroadcastReceiver() {

       @Override
      public void onReceive(Context context, Intent intent) {
       Toast.makeText(getApplicationContext(), "Deliverd",
         Toast.LENGTH_LONG).show();
      }

      }, new IntentFilter(DELIVERED));

      /*Send SMS*/
     SmsManager smsManager = SmsManager.getDefault();
     smsManager.sendTextMessage(phoneNo, null, msg, sentPI,
       deliverPI);
    } catch (Exception ex) {
     Toast.makeText(getApplicationContext(),
       ex.getMessage().toString(), Toast.LENGTH_LONG)
       .show();
     ex.printStackTrace();
    }
   }
  });

  }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

希望这有帮助!

【讨论】:

  • 在交付时进行细微的更正 - 这并不意味着收件人已收到它。这意味着收件人的电话提供商已收到它。它仍然可能无法发送到用户的手机(如果说它已关闭、不在覆盖范围内或网络繁忙),即使它是,也不意味着用户已经看到它。
  • 我已经知道了!发送的意图被正确触发,但传递的待处理意图表现得很奇怪,顺便感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2012-09-10
  • 2021-09-05
  • 1970-01-01
  • 2015-03-30
  • 2016-11-21
  • 2019-10-10
  • 2014-04-13
  • 2017-03-20
相关资源
最近更新 更多