Android SMS传递意图始终返回-1 [英] Android SMS Delivery Intent always return -1

查看:78
本文介绍了Android SMS传递意图始终返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm facing a weird problem on Android. I want to listen for a delivery intent when I send a text message through my app. Here is what I've done so far:

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!即使手机处于关机状态,也无法跟踪短信是否已发送!

Now the problem is that when the onReceive() methods of my broadcast receivers are called I call the getResultCode() method, and it always return -1! Even if the phone is turned off, so it makes it impossible to track whether the SMS is delivered or not!

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

I checked the number with the GoSMSPro and sent a SMS which failed. The interesting thing is that when I put my phone in Airplane Mode I get a Result Code equal to 2 which is SmsManager.RESULT_ERROR_RADIO_OFF

现在的问题是,这是怎么了?

Now the question is what's wrong in here?

推荐答案

尝试这种方法: 要发送短信,请使用SMS管理器中的sendTextMessage.

Try This approach : To send a text message use sendTextMessage from SMS Manager.

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

成功发送消息或发送失败消息时,将触发第一个Pending Intent参数(sentIntent).接收到该Intent的广播接收者的结果代码将是以下之一.

The first Pending Intent parameter (sentIntent) is fired when the message is either successfully sent or failed. The result code for the Broadcast receiever that receives this Intent will be one of the following.

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.

仅当收件人收到您的SMS消息后,才会触发第二个待处理参数(deliveryIntent).

The second Pending parameter(deliveryIntent) is fired only after the recipient receives your SMS message.

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

After sent and delivery events display appropriate messages through 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;
 }

}

希望这对您有帮助!

这篇关于Android SMS传递意图始终返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆