完成广播接收器的活动

And*_*ndy 8 android broadcastreceiver android-activity

我有一个活动,当电话响铃时(通过电话应用程序),我显示为无模式.我想在发生以下任一事件时完成活动.第一个是如果我触摸活动之外的任何地方(这不是问题),第二个是如果振铃停止.我正在收听广播接收器中的IDLE_STATE,但我不确定如何在看到它时调用活动的结束.接收者不是由活动注册,而是由Manifest.xml注册

Vip*_*ahu 15

现在在接收广播中编写代码,这将发送另一个名为"com.hello.action"的广播

Intent local = new Intent();
local.setAction("com.hello.action");
sendBroadcast(local);
Run Code Online (Sandbox Code Playgroud)

现在在活动中抓住这个意图,你想要完成它,然后在接收器的onReceive方法上调用super.finish(),就像这样

public class fileNamefilter extends Activity {
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    IntentFilter filter = new IntentFilter();

    filter.addAction("com.hello.action");
    registerReceiver(receiver, filter);

    }
BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        finish();

    }
};

public void finish() {
    super.finish();
};
}
Run Code Online (Sandbox Code Playgroud)

这将完成活动


Cri*_*ian 0

如果您从活动中注册另一个广播接收器怎么办?然后,当您想杀死它时,从您提到的广播接收器发送广播消息。