Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast result is always zero

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?

like image 631
novik Avatar asked Oct 12 '25 13:10

novik


1 Answers

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?

like image 198
marcinj Avatar answered Oct 14 '25 01:10

marcinj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!