如何在广播接收器中创建回调函数并从活动中获取事件

问题描述 投票:0回答:1

我有一个广播接收器,可以开始新的活动,当我按下活动中的按钮时,我想回电。

广播接收器:

public class CallsReceiver extends BroadcastReceiver implements DialogStopService.Listener {
    private boolean isAllowed = false;

    @Override
    public void onReceive(final Context context, Intent intent) {
        Intent i = new Intent(CallsReceiver.this, DialigStopService.class);
        context.startActivity(i);


    }

    @Override
    public void onAllowPressed() {
        isAllowed = true;
    }

活动代码为:

ublic class DialogStopService extends Activity {
    private allowListener listener;

    private void initListener() {
        //how to init the listener
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initListener();
     }
    public interface allowListener{
        void onAllowPressed();
    }

我的问题是如何从广播中获取上下文以调用回调函数。

java android callback broadcastreceiver listener
1个回答
0
投票

一种可能的解决方案是使用startActivityForResult并在按下按钮时放入Extra。

© www.soinside.com 2019 - 2024. All rights reserved.