简体   繁体   English

我想通过在当前应用程序中按一个按钮来终止/关闭特定应用程序。 我怎样才能做到这一点?

[英]I want to kill/ close a particular app by pressing a button in my current app. How can I do this?

Here's what i want: My app is having a button for closing another independent application say - Google map. 这就是我想要的:我的应用程序有一个用于关闭另一个独立应用程序的按钮-Google地图。 I want to know if the process has started and if it does then i want to close it by pressing that button. 我想知道该过程是否已经开始,如果已经开始,那么我想通过按该按钮将其关闭。

You can use the BroadcastReceiver Class. 您可以使用BroadcastReceiver类。 Try this way. 尝试这种方式。

public class MyBroadcastReceiver extends BroadcastReceiver {

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

        if (isAppForground(context)) {
            // App is in Foreground
        } else {
            // App is in Background
        }
    }

    public boolean isAppForground(Context mContext) {

        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
                return false;
            }
        }

        return true;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要按返回关闭当前意图并转到我的应用程序 - I need to close current Intent by Pressing Back and turn to my app 我想从我的Java Web应用程序访问他们自己的保管箱的用户。 我怎样才能做到这一点? - I want to give access to users of their own dropbox from my java web app. How can I do that? 我想在我的应用程序上进行 m3u8 直播。 那么任何人都可以帮助我如何做到这一点? 我尝试使用 exoplayer 但我失败了 - i want to do m3u8 live streaming on my app. So can anyone help me how can i do this ? i tried with exoplayer but i failed 我想将聊天窗口/选项集成到我的 web 应用程序中。 我从哪说起呢? - I want to integrate a chat window/option into my web app. Where do I start? 我正在尝试制作社交媒体应用。 如何将我的应用程序连接到mySql和Xampp? - I am trying to make a social media app. How do I connect my app to a mySql and, Xampp? 如果Android杀了我的应用程序并且我从设置中执行强制关闭,我会收到GCM消息吗? - Will I receive GCM messages if Android kill my app and if I do a Force Close from the settings? 如何为我的应用终止远程进程? - How can I kill a remote process for my app? 我正在努力使我的应用程序具有“保持登录状态”状态。 我该怎么办? - I'm working on having a “Keep me on Logged in” state on my app. How should i do it? 当我按下按钮调用 function 时,我的应用程序崩溃了 - My app is crashing when I call my function by pressing a button 我想通过按一个按钮在Textview上产生波纹效果。 可能吗? 我该怎么做? - I want to make Ripple effect on Textview by pressing a button. Is it possible? How can I do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM