简体   繁体   中英

Sending data from `BraodcastReceiver` to `Activity`

I have a MainActivity and a MyBroadcastReceiver. The BroadcastReceiver waits for incoming SMSes and reads the sms and senderId, it should send this data to MainActivity in real time. I have tried to implement the BroadcastReceiver in the Activity itself but it launches the activity again.

    Public class MainActivity extends AppCompatActivity{
 public static String BROADCAST_ACTION = "SMSCOMING";
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final Bundle bundle = intent.getExtras();
            try {
                //Getting the data d
                    triggerFunc(d);
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    };

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     IntentFilter filter = new IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        this.registerReceiver(this.broadcastReceiver, filter);
}

void triggerFunc(data d){
   //Do some stuff
}

    }

Please note that we have two kind of broadcast receivers in Android:

  • Standalone broadcast receivers (one of four main Android building blocks). This type of receivers must be registered in Android manifest file. These receivers will be run whenever their matching intents are received, no matter app's UI is running or not.

  • In-Activity broadcast receivers. This kind of receivers don't need to be registered in Manifest file, you should instead register them at runtime. These receivers are only run when their enclosing activity is active and running.

So, if you would like your app to be able to catch all SMS messages, regardless of its UI status, you would need the former option. However if you would need your app to catch SMS message while its activity is shown, you would need the latter option.

When the BroadcastReceiver receives an SMS notification, it cannot know if the Activity is running or not. If you want to launch the Activity, you need to create an appropriate Intent and call Context.startActivity() .

You should create a class which extends BroadcastReceiver rather than simply an instance of an anonymous inner class. Also, you should register your BroadcastReceiver using the <receiver> tag in AndroidManifest.xml .

whatever code you have added in question as per that your Receiver only called when you Activity is running. instead of that create broadcast separately(remove from activity & create new class in your package) and register in AndroidManifest file and call your activity from Receiver.

just like below.

create BroadcastReceiver Class in your package.

public class BinarySMSReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final Bundle bundle = intent.getExtras();
            try {
                //Getting the data d
                    Intent intent = new Intent();
                    intent.setClassName(context, "activity.class");
                    intent.putExtras(bundle);
                    context.startActivity(intent);


            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    };

in AndroidManifest

<receiver android:name="com.company.application.SMSBroadcastReceiver" >
        <intent-filter android:priority="500">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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