How to pass data to BroadcastReceiver?

后端 未结 5 986
难免孤独
难免孤独 2020-12-08 21:43

What I am trying to do is that the numbers to which my application sends messages to, are passed to the BraodcastReceiver...but so far either I am getting null or BroadcastR

5条回答
  •  萌比男神i
    2020-12-08 22:06

    Following @Jason 's example...I did this...

    In MainActivity or any activity from where you want to send intent from

    Intent intent = new Intent("my.action.string");
    intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
    sendBroadcast(intent);
    

    and then in my SmsReceiver Class I did this

    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
    
      Log.i("Receiver", "Broadcast received: " + action);
    
      if(action.equals("my.action.string")){
         String state = intent.getExtras().getString("extra");
    
      }
    }
    

    And in manifest.xml I added "my.action.string" though it was an option..

    
        
            
            
            
        
    
    

    worked like charm!!

提交回复
热议问题