简体   繁体   English

活动中的广播接收器

[英]Broadcast receiver in an activity

Hi I am developing an android SMS app where I am using a broadcast receiver inside an activity refering the below link, 嗨,我正在开发一个android SMS应用程序,其中我在活动中使用广播接收器,并参考以下链接,

SMS Delivery Report in Android Android中的短信发送报告

I have tried this code 我已经尝试过此代码

public class Myapp extends Activity 
{
  BroadcastReceiver sendBroadcastReceiver= new sentReceiver();

    protected void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activtiy_main);


    }

   public void onClick(View v)
  {
    if(v.getId()==sendmsg.getId())
    {

        sendSMS(phoneNumber,message);

    }

 @Override
  protected void onDestroy() 
  {
    super.onDestroy();
    try
    {
        unregisterReceiver(sendBroadcastReceiver);
    }
    catch (Exception e)
    {
         e.printStackTrace();
    }
  }
     //unregistering Receiver even in onPause()

  private void sendSMS(String phoneNumber, String message)
  {
    String SENT = "SMS_SENT";
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, null);

  }
  class sentReceiver extends BroadcastReceiver 
  {
    @Override
    public void onReceive(Context context, Intent arg1)

    {
        switch (getResultCode()) 

        {
        case Activity.RESULT_OK:
            Toast.makeText(getBaseContext(), "sms_sent", Toast.LENGTH_SHORT).show();
             startActivity(new Intent(Myapp.this, Sm.class));
             break;
        case SmsManager.RESULT_ERROR_NO_SERVICE:
            Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();
            Log.e("No service","");
            break;
        }
     }                  

   }

}

But I am getting warnings as 但是我收到警告

java.lang.IllegalArgumentException: Receiver not registered: com.example.Myapp$Receiver@40593ab8

Not sure How I need to register the receiver in manifest.Please suggest.Thanks! 不确定我需要如何在清单中注册接收者。请提出建议。谢谢!

1. You are registering your receiver programmatically with 1.您正在以编程方式注册接收器

registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

So there is no need to add it to the Manifest aswell. 因此,也无需将其添加到清单中。

2. You have a custom BroadcastReceiver-class called "sentReceiver", so you should use this objecttype when you instantiate it. 2.您有一个名为“ sentReceiver”的自定义BroadcastReceiver类,因此在实例化该对象类型时应使用它。

So you need to change 所以你需要改变

BroadcastReceiver sendBroadcastReceiver= new sentReceiver();

to

SentReceiver sendBroadcastReceiver = new sentReceiver();

3. I think you are calling your stuff in the wrong order. 3.我认为您打错电话的顺序是正确的。 So that the receiver is not ready listening when it should be. 因此,接收器在应该收听的时候还没有准备好收听。

You need to instantiate sendBroadcastReceiver in the oncreate method of your activity. 您需要在活动的oncreate方法中实例化sendBroadcastReceiver。 This will allow for "re-registration" after the ondestroy method is called and the receiver is unregistered by your code. 在调用ondestroy方法并且您的代码未注册接收者之后,这将允许“重新注册”。 And as Bofredo noted, use your custom calss to instantiate. 就像Bofredo指出的那样,使用您的自定义校准实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM