如何在android中只调用一次广播接收器?

如何在android中只调用一次广播接收器?,android,broadcastreceiver,Android,Broadcastreceiver,您好,我遇到了同样的问题,我有一个广播接收器,我从中启动了一个服务,用于从收件箱获取消息。在那之后,我将这些信息发送到特定的号码。对于发送消息,我使用了与上面相同的代码(由John发布)。我的问题是,当我发送消息时,toast消息“SMS SENT”将持续出现。那我怎么能只收到一次呢 我被这件事缠住了,有人能告诉我哪里错了吗 谢谢 我在这里发布了我的代码:- package z.z.z; import android.content.BroadcastReceiver; import andr

您好,我遇到了同样的问题,我有一个广播接收器,我从中启动了一个服务,用于从收件箱获取消息。在那之后,我将这些信息发送到特定的号码。对于发送消息,我使用了与上面相同的代码(由John发布)。我的问题是,当我发送消息时,toast消息“SMS SENT”将持续出现。那我怎么能只收到一次呢

我被这件事缠住了,有人能告诉我哪里错了吗

谢谢

我在这里发布了我的代码:-

package z.z.z;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSReceiver extends BroadcastReceiver  
{
    private SmsMessage[] msgs;
    private String strNo,strMsgText;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        try
        {
            Bundle bundle = intent.getExtras();        
            msgs = null;
            String str = "";         

            if (bundle != null)
            {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                for (int i=0; i<msgs.length; i++)
                {
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    str += "SMS from " + msgs[i].getOriginatingAddress();                     
                    str += ":";
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";
                    strNo = msgs[i].getOriginatingAddress();    
                    strMsgText = msgs[i].getDisplayMessageBody();

                }

                context.startService(new Intent(context,IncomingSMSService.class));

            }
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

}

在我看来,你是这样安排的。您的
IncomingSMSService
调用您的
SMSService.sendSMS
函数,该函数注册您的
SMSReceiver
,该函数发送重新启动
IncomingSMSService
的意图,依此类推


尝试删除此链的一个链接,看看它是否解决了重复问题。

实际上,我必须向特定号码发送“SMS send”消息作为通知。这就是我再次从RMACeiver发送SMS的原因。问题是,您正在使用自定义类
SMSService.sendSMS
,该类为发送和传递通知注册接收者。要发送通知文本,请使用vanilla
smsmsmanager.sendTextMessage
,这样您就不会收到无限循环的文本,也不会收到针对这些文本的已发送通知的已发送通知,等等。@Craigy我也有类似的问题,我在这里提出了我的问题:…我无法定位无限循环来自何处,我已经仔细检查了意图的路径,但它似乎是单向的,没有像anddev那样的循环逻辑…但问题仍然存在,一个无限的组中发送的第一条消息列表
package z.z.z;

import java.util.ArrayList;

import z.z.z.Global;
import z.z.z.SMSService;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

public class IncomingSMSService extends Service  
{

    private String TAG = "IncomingSMSService";

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        /* start service */

        Log.d(TAG ,"****** in service onCreate  **----- ");
        super.onCreate();

        try
        {
            Log.d(TAG ,"****** in service try  **----- ");
            /* Read SMS from INBOX  */
            Uri uriSMSURI = Uri.parse("content://sms/inbox");      
            Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);      
            String sms = "";      
            while (cur.moveToNext()) 
            {          
                    sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
    //              Log.d(TAG, "in SMS Service sms in loop :: "+ sms);
            }
            Log.d(TAG, "in SMS Service sms :: "+ sms);


            Log.d(TAG ,"********** reverseNum ******  "+ Global.destNo);
            ArrayList<String> ayyMsg = new ArrayList<String>();
            ayyMsg.add(sms);
            SMSService.sendSMS(getBaseContext(), ayyMsg, Global.destNo);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }}
   package z.z.z;

    import java.util.ArrayList;

    import z.z.z.common.RMAReceiver;

    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.telephony.SmsManager;
    import android.util.Log;

    public class SMSService 
    {

        static String TAG = "SMSService";
        static String SENT = "SMS_SENT";
        static String DELIVERED = "SMS_DELIVERED";

    //  static Context mContext;
        static RMAReceiver rmaReceiver = null;
        public static void sendSMS(Context context,ArrayList<String> message, String destNumber)
        {   
            try
            {



                PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,new Intent(SENT), 0);

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

                ArrayList<PendingIntent> ayySentPI = new ArrayList<PendingIntent>();
                ayySentPI.add(sentPI);

                ArrayList<PendingIntent> ayyDeliveredPI = new ArrayList<PendingIntent>();
                ayyDeliveredPI.add(deliveredPI);

                receiver(context);

                SmsManager sms = SmsManager.getDefault();

                sms.sendMultipartTextMessage(destNumber, null, message, ayySentPI, ayyDeliveredPI);

            }
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }

**receiver**:-

    public static void receiver(Context context)
        {
            rmaReceiver = RMAReceiver.getSingleInstance();
            context.registerReceiver(rmaReceiver, new IntentFilter(SENT));

            //---when the SMS has been delivered---
            context.registerReceiver(rmaReceiver, new IntentFilter(DELIVERED));  
        }
package z.z.z;

import z.z.z.SMSService;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;

public class RMAReceiver extends BroadcastReceiver 
{
    private static RMAReceiver singleInstance = null;
    String msg;

    private RMAReceiver()
    {       
    }

    public static RMAReceiver getSingleInstance()
    {
        if(singleInstance == null) singleInstance = new RMAReceiver();
        return singleInstance;
    }

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        System.out.println("Result Code: "+getResultCode());
//      resultCode = getResultCode();
        switch (getResultCode())
        {
            case Activity.RESULT_OK:
                msg = "SMS sent/delivered";
//              Toast.makeText(context, "SMS sent/delivered", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "SMS sent/delivered", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                msg = "Generic failure";
//              Toast.makeText(context, "Generic failure", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "Generic failure", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                msg = "No service";
//              Toast.makeText(context, "No service", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                msg = "Null PDU";
//              Toast.makeText(context, "Null PDU", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                msg = "Radio off";
//              Toast.makeText(context, "Radio off", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
            case Activity.RESULT_CANCELED:
                msg = "SMS not delivered";
//              Toast.makeText(context, "SMS not delivered", 
//                      Toast.LENGTH_SHORT).show();
//              SMSService.sendSMS(context, "No service", Global.confirmNo);
                break;
        }

        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        SMSService.sendSMS(context, msg, Global.confirmNo);



    }


}