完成从广播接收器的活动接收器

2023-09-12 04:24:29 作者:生活再累也要屁颠屁颠的过

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

I have an Activity that I display as modeless when the phone rings (over the phone app). I would like to finish the Activity when either of the following events occur. The first is if I touch anywhere outside the Activity (this is not a problem), the second is if the ringing stops. I am listening for IDLE_STATE in the broadcast receiver but I am not sure on how to call the finish on the activity when I see it. The receiver is not registered by the activity but by the Manifest.xml

推荐答案

写code在接收广播现在这将发送另一个广阔的投带评为意图com.hello.action

write the code in your receiving broadcast now this will send another broad cast with the intent named "com.hello.action"

Intent local = new Intent();
local.setAction("com.hello.action");
sendBroadcast(local);

现在你想完成它,然后调用super.finish()接收器上的的onReceive方法捕获该意图的活动 像这样

Now catch this intent in the activity with you want to finish it and then call the super.finish() on the onReceive method of your receiver like this

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();
};
}

这将完成的活动