Call activity method from broadcast receiver android?

后端 未结 5 1796
猫巷女王i
猫巷女王i 2021-01-07 15:19

In my application I am sending a port SMS to the mobile. And when the message is recieved I need to perform some task in my activity and update the UI.

Manifest Decl

5条回答
  •  借酒劲吻你
    2021-01-07 16:05

    Your BroadcastReceiver doesn't know anything about any activity by default. You will have to give the context to it, for example in your constructor when you create the broadcastReceiver.

    private Activity activity;
    public BroadcastReceiver(Activity activity) {
        super();
        this.activity = activity;
    }
    

    In you onReceive method you will now be able to use your activity in whatever way you like.

提交回复
热议问题