How can i get recevier instance(which is registered in AndroidManifest.xml) in activity

后端 未结 2 1536
情深已故
情深已故 2021-01-22 20:52

I\'ve register a receiver in AndroidManifest.xml like this

 
        

        
相关标签:
2条回答
  • 2021-01-22 21:12

    Receivers are meant to act on events generated by the system and sometimes the users. There are special cases where you might want to get an instance of those yourself but this is uncommon. The whole point of having receivers is to react to system events and take some action. Unless you know what you are doing, I would recommend against creating receiver instances yourself in an activity.

    If you really want to, you can do it like this

    private BroadcastReceiver myReceiver = new BroadcastReceiver()
    {
    
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // do stuff
        }
    }
    

    Also see this thread for related info: BroadcastReceiver as inner class

    0 讨论(0)
  • 2021-01-22 21:30
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()
    {
    
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // Your code here to do what ever you want
        }
    }
    
    0 讨论(0)
提交回复
热议问题