zoukankan      html  css  js  c++  java
  • TabActivity 切换到后台 回到当前运行的桌面

    一般Activity中监听返回键,都是重写onKeyDown(int keyCode, KeyEvent event)方法对手机按键进行监听,

    而当要监听的Activity为TabActivity时(其实ListActivity也一样),这个方法并不起作用。

    下面是解决办法:

    正在开发的应用的首页是一个TabActivity,当用户按下返回键时会退出程序,想退出到后台看home键,还不是销毁。

    • 常规方法像上文说的重写onKeyDown(int keyCode, KeyEvent event),根本不起作用;
    • 又发现SDK2.0以后新增了一个专门针对返回键的方法onBackPressed(),还是不起作用。

    现有一个dispatchKeyEvent(KeyEvent event)果然管用

     1 public boolean dispatchKeyEvent(KeyEvent event) {
     2         if (event.getAction() == KeyEvent.ACTION_DOWN
     3                 && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
     4             ResolveInfo launcherResolve=queryCurrentLauncher();
     5             Intent intent=new Intent();
     6             intent.addCategory(Intent.ACTION_MAIN);
     7             ComponentName componentName=new ComponentName(launcherResolve.activityInfo.packageName,launcherResolve.activityInfo.name);
     8             intent.setComponent(componentName);
     9             startActivitySafely(intent);
    10         }
    11         return super.dispatchKeyEvent(event);
    12     }
    13     private void startActivitySafely(Intent intent){
    14         try{
    15             startActivity(intent);
    16             Toast.makeText(this, "切换到后台成功", Toast.LENGTH_SHORT).show();
    17         }catch (Exception e){
    18             Toast.makeText(this, "切换到后台失败", Toast.LENGTH_SHORT).show();
    19             e.printStackTrace();
    20         }
    21     }
    22 
    23     private ResolveInfo queryCurrentLauncher() {
    24         Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
    25         launcherIntent.addCategory(Intent.CATEGORY_HOME);
    26         launcherIntent.addCategory(Intent.CATEGORY_DEFAULT);
    27         List<ResolveInfo> launcherInfoList = getPackageManager().queryIntentActivities(launcherIntent, PackageManager.MATCH_DEFAULT_ONLY);
    28 
    29         ResolveInfo launcherResolveInfo = null;
    30         ActivityManager activityManager = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE);
    31         List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(30);
    32         Iterator<ActivityManager.RunningTaskInfo> itInfo = tasks.iterator();
    33         while (itInfo.hasNext()) {
    34             ActivityManager.RunningTaskInfo info = itInfo.next();
    35             for (ResolveInfo resolveInfo : launcherInfoList) {
    36                 String name1 = info.topActivity.getClassName();
    37                 String namme2 = resolveInfo.activityInfo.name;
    38                 if (name1.equals(namme2)) {
    39                     //获取当前使用的桌面
    40                     launcherResolveInfo = resolveInfo;
    41                     return launcherResolveInfo;42                 }
    43             }
    44         }
    45         return launcherResolveInfo;
    46     }


    //这段代码的功能:在TabActivity下实现切换到后台,回到当前用户使用的桌面,而不是弹出选择框让用户选择,也不是回到固定的桌面,下次启动的时候直接回到之前的界面,从而避免每次打开都会重新开始
  • 相关阅读:
    论文摘要
    memset/memcpy/strcpy
    error C2259: 'CException' : cannot instantiate abstract class解决
    IplImage 结构
    图像的深度和通道概念
    Oracle11g安装教程
    LSTM理解
    卷积神经网络CNN
    常见激活函数的介绍和总结
    TFIDF算法介绍
  • 原文地址:https://www.cnblogs.com/xushihai/p/4818387.html
Copyright © 2011-2022 走看看