Receiver as inner class in Android

前端 未结 6 1717
广开言路
广开言路 2020-11-27 06:14

In my code there is an inner class that extends BroadcastReceiver.

And I have added the following line to the AndroidManifest.xml:

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 06:30

    I've just met the same problem.

    I have a service to communicate with many activities, and I also have a receiver in my service as inner class. when the receiver get message ,it has to call the method of service. so it goes on like this :

    1. Service code:

    public class XXService extends Service{
    
         // I want the inner receiver call the methd of XXService,
        // so I have to write this constructor like this.
    
        private XXService instance;
        public XXService(){instance = this;}
        public XXService getInstance(){
           return instance;
        }
        public void sayHello(){
        }
        public static class XXReceiver extends BroadcastReceiver{
    
            onReceive(......){
                XXService.getInstance.sayHello();
            } 
       }
    }
    

    2. Register receiver in manifest.xml :

    
    

提交回复
热议问题