3

I'm trying to create an external broadcast service which sends a number. A client (external application) trying to send a request to my service and the service sends back a number. I registered my service and broadcast resiever in AndroidManifest.xml:

<service android:exported="true" android:enabled="true" android:name=".MyService"/>
<receiver android:exported="true" android:enabled="true" android:name=".MyStartServiceReceiver">
    <intent-filter>
         <action android:name="android.intent.action.SEND" />
         <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

my broadcast class:

public class MyStartServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent1 = new Intent(context, MyService.class);
        context.startService(intent1);
    }
}

in MyService class I'm trying to put extra data

public void onStart(Intent intent, int startId) {
    Log.i(TAG, "service started");
    intent.setClass(getApplicationContext(), MyService.class);
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra("result", 10);
    sendBroadcast(intent);
}

and send it back, but I keep getting zero. To check my service I use adb shell:

adb shell am broadcast -a android.intent.action.SEND
Broadcasting: Intent { act=android.intent.action.SEND }
Broadcast completed: result=0

Does anybody know what's wrong in my service?

novik
  • 65
  • 1
  • 1
  • 6

2 Answers2

3

You can see here:

http://developer.android.com/reference/android/content/Intent.html

that ACTION_SEND is activity action, it cannot be used with receiver.

So you must switch from receiver to activity, you can make it a hidden activity using Theme.NoDisplay

[edit] some more explanation: BroadcastReceiver with intent-filter for them?

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • thanks for the answer! It indirectly helped me to use another solution. I use [Message](http://developer.android.com/reference/android/os/Message.html) instead. It's better solution in my case. – novik Mar 18 '15 at 07:22
0

Try something like this.

Method to send broadcast, used within the MyService

  public static void sendSomeBroadcast(Context context, String topic) {
    Intent actionIntent = new Intent();

   // I would use Constants for these Action/Extra values
    actionIntent.setAction(ConstantClass.SEND_SOME_BROADCAST);
    actionIntent.putExtra(ConstantClas.BROADCAST_RESULT, 10);
    LocalBroadcastManager.getInstance(context).sendBroadcast(actionIntent);
  }

In action

public void onStart(Intent intent, int startId) {
    sendSomeBroadcast();
}

BroadcastReceiver

public class MyStartServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // do what you want with the intent, for example intent.getExtras()..

        Intent intent1 = new Intent(context, MyService.class);
        context.startService(intent1);
    }
}

Binding the receiver and listening for specific Action

    private void bindStartServiceReceiver() {
            MyStartServiceReceiver startServiceReceiver = new MyStartServiceReceiver();

             //This may need to be changed to fit your application
            LocalBroadcastManager.getInstance(this).registerReceiver(subscribeTopicReceiver,
            new IntentFilter(ConstantClass.SEND_SOME_BROADCAST));
      }
Coova
  • 1,818
  • 5
  • 36
  • 63