0

I register a BroadcastReceiver within MyService,

public class MyService extends Service {

final static String ACTION_ONE = "one";
final static String ACTION_TWO = "two";

 BroadcastReceiver receiver;

and a inner class

// use this as an inner class like here or as a top-level class
public class MyReceiver2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // do something
        Log.d("tag", "received! MyReceiver2");
    }

    // constructor
    public MyReceiver2(){

    }
}

onCreate method:

@Override
public void onCreate() {
   receiver = new MyReceiver2();

    IntentFilter filter = new IntentFilter();
    filter.addAction(MyService.ACTION_ONE);
    filter.addAction(MyService.ACTION_TWO); 

    MyService.this.registerReceiver(receiver, filter);
}

And the code below is in MainActivity to send a broadcast:

    sendBroadcast.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

Intent intent = new Intent(v.getContext(), MyService.class);
            intent.setAction(MyService.ACTION_TWO);
            sendBroadcast(intent);
}
});

I can't understand why MyService(started, and is not yet destoryed) can not receive broadcast.

If I change the receiver variable to a MyReceiver class that extends BroadcastReceiver and has already been proved to work, MyService is still not receiving broadcast.

myReceiver = new MyReceiver();

Here is MyReceiver(it is proved that it works well):

public class MyReceiver extends BroadcastReceiver{

public MyReceiver(){
    Log.d("tag", "MyReceiver Constructor");
}

@Override
public void onReceive(Context context, Intent intent) {
        Log.d("tag", "received");
}
}

============EDIT================

manifest Both

    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" >
        <intent-filter >
            <action android:name="one" />
            <action android:name="two" />
        </intent-filter>
    </service>

and

    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" >
    </service>

do not work.

Jean Y.C. Yang
  • 4,382
  • 4
  • 18
  • 27
  • first of all: what do you need that receiver for? – pskink Oct 04 '15 at 09:28
  • `MyService` needs to receive broadcasts from notification action. I started from letting `MainActivity` send broadcast. – Jean Y.C. Yang Oct 04 '15 at 09:30
  • a notification can start: 1) activity 2) broadcast 3) service, so use the option #3 directly – pskink Oct 04 '15 at 09:32
  • So, there is no chance that you need a service to listen to broadcast? If chances are that you may need service to listen to broadcast, I want to solve this problem. – Jean Y.C. Yang Oct 04 '15 at 09:34
  • you are sending a broadcast from `onClick`, where is your notification? i dont see any... – pskink Oct 04 '15 at 09:35
  • Can't me start from sending a broadcast from MainActivity Button? The problem is MyService can't receive any broadcast. I send it from notification PendingIntent too, but not works. I think it is MyService's problem, not a problem of how broadcast is sent. – Jean Y.C. Yang Oct 04 '15 at 09:38
  • do not use any broadcast, start your service directly – pskink Oct 04 '15 at 09:39
  • So there is not way a service can receive broadcast? i dont think so. [link](http://stackoverflow.com/questions/18861279/how-do-i-implement-a-broadcastreceiver-in-a-service-class-in-android) Thank you, i may try another service, **BUT I REALLY WANT TO FIND OUT WHY MyService CANNOT RECEIVE BROADCAST** – Jean Y.C. Yang Oct 04 '15 at 09:43

1 Answers1

-1

This looks like a manifest problem,

Please make sure you are declaring that receiver in the manifest! for example:

<receiver android:name=".receivers.BackgroundTasksReceiver"/>
JozeRi
  • 3,219
  • 6
  • 27
  • 45