Search code examples
androidandroid-intentservicebroadcastreceiver

service receiving Null values in an intent


I'm starting a service from the BroadcastReceiver and sending some values from BroadcastReceiver to the service. When I'm trying to retrieve those values in the service, It's giving me null value.

Please let me know what I'm doing wrong. Thanks in advance!

starting service from broadcast-

    Intent serviceIntent = new Intent(context, LatestCallService.class);
    intent.putExtra("from","Internet");
    context.startService(serviceIntent);

retrieving value in service but giving Null-

    public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStart(intent, startId);
    String a= intent.getStringExtra("from");
    Toast.makeText(this, " Name : "+a, Toast.LENGTH_SHORT).show();

Solution

  •  @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Event", Toast.LENGTH_SHORT).show();
            intent = new Intent(context, MyService.class);
            intent.putExtra("check","done");
            context.startService(intent);
        }
    

    Use the Intent from onReceive method