0

I have two services. I need to broadcast custom intent with data from one service. and in second service I need to receive it. I have tried following:

In first service:

  public String ACTION_CHANGE_TIME_FORMAT = "com.example.agile.mywatchface.changetimeformat";

Intent timeFormatIntent = new Intent();
                    timeFormatIntent.putExtra("format", 12);
                    timeFormatIntent.setAction(ACTION_CHANGE_TIME_FORMAT);
                    LocalBroadcastManager.getInstance(this).sendBroadcast(timeFormatIntent);

In Second Service:

  public String ACTION_CHANGE_TIME_FORMAT = "com.example.agile.mywatchface.changetimeformat";

    public class TimeFormatReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(MyPreferences.LOGCAT_TAG, "Time format Broadcast received: ");
            format = intent.getExtras().getInt("format", 0);
            updateTime();
        }
    }

and I have registered and unregistered receivers properly in second service:

   IntentFilter timeFormatIntentFilter = new IntentFilter();
    timeFormatIntentFilter.addAction(ACTION_CHANGE_TIME_FORMAT);
    MyWatchfaceService.this.registerReceiver(timeFormatReceiver, timeFormatIntentFilter);

Is there anything wrong here? I can't get data(format).

Edit: onRecieve() is not calling.

Krupal Shah
  • 8,949
  • 11
  • 57
  • 93

2 Answers2

0

In Second Service:
Change format = intent.getExtras().getInt("format", 0); to format = intent.getIntExtra("format", 0);

The intent.getExtras() will return the Bundle which put by Intent.putExtras(Bundle bundle), but you didn't do it.

chartsai
  • 368
  • 3
  • 7
0

1_ Check that your onReceive() method is called. If not, your Receiver still does not registered.

2_ if (1) is OK, try int format = intent.getIntExtra("format", 0);

P/S: I dont know where do you use the "format" variable, consider use it as local variable and pass it like a parameter.

int format = intent.getIntExtra("format", 0);
updateTime(format)
NamNH
  • 1,752
  • 1
  • 15
  • 37
  • Check these conditions: 1) Your receiver is declared in manifest file? 2) The code registerReceiver() is called, I mean your second service is started and register your BroadcastReceiver. – NamNH Mar 24 '15 at 10:33
  • I registered programmatically. Do i need explicitely register in reciever? – Krupal Shah Mar 24 '15 at 10:37
  • Sorry, you don't need declare it in manifest in this case. But when and where the code register is called? Is the second service started or bound? – NamNH Mar 24 '15 at 10:56