从广播接收器完成活动

发布于 2024-12-06 19:11:33 字数 173 浏览 0 评论 0原文

我有一个活动,当电话响起时(通过电话应用程序)显示为无模式。我想在发生以下任一事件时完成活动。第一个是我触摸活动之外的任何地方(这不是问题),第二个是铃声是否停止。我正在广播接收器中监听 IDLE_STATE,但我不确定当我看到它时如何调用活动的结束。接收器不是由 Activity 注册的,而是由 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三生路 2024-12-13 19:11:34

我使用这段代码来解决我的问题。 finishActivity 的方法,因此调用 Activity 和上下文如下:

if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
     Log.e(TAG, "Inside EXTRA_STATE_IDLE");
     ((Activity) arg0).finish();
}

I used this code to solve my problem. finish is the method of Activity, so call Activity and context like:

if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
     Log.e(TAG, "Inside EXTRA_STATE_IDLE");
     ((Activity) arg0).finish();
}
幸福还没到 2024-12-13 19:11:33

现在在您的接收广播中编写代码,这将发送另一个广播,其意图名为“com.hello.action”

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

现在在活动中捕获此意图,您想要完成它,然后在 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();
};
}

这将完成活动

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

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

this will finish the activity

蓝颜夕 2024-12-13 19:11:33

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

What if you register another broadcast receiver from the activity. Then, when you want to kill it, send a broadcast message from the broadcast receiver that you mentioned.

浪漫人生路 2024-12-13 19:11:33

实际上,我最终在 Activity 中添加了一个 PhoneStateListener 来监听 IDLE_STATE。

I actually ended up adding a PhoneStateListener in the Activity to listen for IDLE_STATE.

怪异←思 2024-12-13 19:11:33

经过长时间的互联网研发,我解决了我的问题。如果您从广播接收器启动一个活动并且
清除所有活动堆栈实例。

 @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            moveTaskToBack(true);
        }
    }

After a long R&D over internet i have solved my problem. The below code is helpful if you start an activity from broadcast receiver and
clear all activity stack instance.

 @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            moveTaskToBack(true);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文