点击后台主页键和应用程序运行 [英] click on home key and application run in background

查看:122
本文介绍了点击后台主页键和应用程序运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把code表示如果家里按钮pressed然后background.how运行实施code在我application.here $ c。使用作弄在后台我的应用程序运行应用程序$ C,但它不工作时,点击主屏幕键

 公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果(键code == KeyEvent.KEY code_BACK ||
            关键code == KeyEvent.KEY code_HOME&放大器;&安培; event.getRepeatCount()== 0){
        this.moveTaskToBack(真);
        返回true;
    }
    返回super.onKeyDown(键code,事件);
}


解决方案

在这里检查时,Home键被pressed或您的应用程序将要离开了。

 公共无效的onPause(){
    super.onPause();
        上下文的背景下= getApplicationContext();
                ActivityManager AM =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
                清单< RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                如果(!taskInfo.isEmpty()){
                  组件名topActivity = taskInfo.get(0).topActivity;
                  如果(!topActivity.getPackageName()。等于(context.getPackageName())){                    Toast.makeText(xYourClassNamex.this,你离开你的应用程序。Toast.LENGTH_SHORT).show();
                  }
                }
      }

I am trying to put code for if the home button is pressed then application process running in background.how implement code for my application run in background using codding in my application.here code but its not working when click on home key

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK ||
            keyCode == KeyEvent.KEYCODE_HOME && event.getRepeatCount() == 0) {
        this.moveTaskToBack(true);               
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

解决方案

Check here, when Home button is pressed or Your app is going to left.

public void onPause(){
    super.onPause();
        Context context = getApplicationContext();
                ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                if (!taskInfo.isEmpty()) {
                  ComponentName topActivity = taskInfo.get(0).topActivity; 
                  if (!topActivity.getPackageName().equals(context.getPackageName())) {

                    Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP. ", Toast.LENGTH_SHORT).show();
                  }
                }
      }

这篇关于点击后台主页键和应用程序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆