Android的,如何清除最近的任务列表,它可以在大多数手机得到Home键?反思是一种可能的方式是什么? [英] Android, how to clear the recent task list which could get from Home button in most phone? Reflection is a possible way?

查看:2102
本文介绍了Android的,如何清除最近的任务列表,它可以在大多数手机得到Home键?反思是一种可能的方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个发射器,它需要明确的从系统最近的应用程序/任务列表,而不是没有表现出我的应用程序在最近的任务清单,但我不知道这件事了。 我已搜查了计算器,只有<一href="http://stackoverflow.com/questions/6723132/android-clear-the-recent-app-list-shown-after-long-home-$p$pss">this问题是匹配的,但答案没有任何帮助。 其他一些人也问同样的问题,他提到的这来自安卓4.0 RemoveTask。 是的,我检查Android 2.3.7和Android 4.0的源$ C ​​$ C,在估计一个回合,我觉得我几乎可以到达终点,如果我可以擦除这ActivityMangerService.Java定义mRecentTasks的名单:

I'm writing a Launcher, it need clear the recent app/task list from system, not "didn't show my apps in recent tasks list", but I have no idea about it now. I have searched in the stackoverflow and only this issue is matched but the answer has no any help. Some other guy has asked the same questions, he metioned the RemoveTask which comes from Android 4.0. Yes, I have checked the source code of Android 2.3.7 and Android 4.0, at a round estimated, I think I could almost reach the end point if I can erase the list of mRecentTasks which defined in ActivityMangerService.Java :

final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>();

和其它可能有用的定义:

And another maybe useful definition:

static ActivityManagerService mSelf;
public static ActivityManagerService self() {
    return mSelf;
}

由于我不熟悉Java和放大器; refelction,我需要哪些清楚这个列表的方式帮助,下面是其中code我:

Because I'm not familiar with Java&refelction, I need help about the way which clear this list, below is the code of my:

    public static <T> void clearRecentTaskList(Launcher launcher){
    ActivityManager am = (ActivityManager) launcher.getSystemService(Context.ACTIVITY_SERVICE);
    Object systemRecentTask = new ArrayList<T>();

    Object receiver = null;
    Field recentTaskList = null;
    Class<?> service = null;
    Field self = null;        


    try {
        service = Class.forName("com.android.server.am.ActivityManagerService");
        Log.d(LOG_TAG, "clearRecentTaskList, service gotton"+service.getName());
    } catch (ClassNotFoundException e2) {
        Log.d(LOG_TAG, "clearRecentTaskList, class service not found");
    }

    try {
        self = service.getDeclaredField("mSelf");
    } catch (SecurityException e2) {
        Log.d(LOG_TAG, "clearRecentTaskList, SecurityException during get mSelf");

    } catch (NoSuchFieldException e2) {
        Log.d(LOG_TAG, "clearRecentTaskList, NoSuchFieldException during get mSelf");
    }
    Log.d(LOG_TAG, "clearRecentTaskList, self  gotton " + self.toGenericString());

    try {
        self.setAccessible(true);
        receiver = self.get(null);
    } catch (IllegalArgumentException e2) {
        Log.d(LOG_TAG, "clearRecentTaskList, IllegalArgumentException during use self to get the receiver");
    } catch (IllegalAccessException e2) {
        Log.d(LOG_TAG, "clearRecentTaskList, IllegalAccessException during use self to get the receiver");
    }

    if ( receiver != null){
        Log.d(LOG_TAG, "clearRecentTaskList, receiver is : "+receiver.toString());
    } else {
        Log.d(LOG_TAG, "clearRecentTaskList, receiver is NULL");
    }


    try {
        recentTaskList = service.getDeclaredField("mRecentTasks");
        recentTaskList.setAccessible(true);
        Log.d(LOG_TAG, "clearRecentTaskList, recentTaskList gotton"+recentTaskList.toGenericString());

        try {
            systemRecentTask = recentTaskList.get(receiver);
        } catch (IllegalArgumentException e1) {
            Log.d(LOG_TAG, "IllegalArgumentException during try to clearRecentTask");
        } catch (IllegalAccessException e1) {
            Log.d(LOG_TAG, "IllegalAccessException during try to clearRecentTask");
        }

        Log.d(LOG_TAG, "Try to print the size of recent task: "+((ArrayList<T>) systemRecentTask).size());

    } catch (SecurityException e) {
        Log.d(LOG_TAG, "SecurityException during try to clearRecentTask");
    } catch (NoSuchFieldException e) {
        Log.d(LOG_TAG, "NoSuchFieldException during try to clearRecentTask");
    }   
}

通过这个功能,我总是符合NullPointerException异常,因为接收器为空而得到自我。我曾尝试另一种方式是这样的(如果我删除了所有的try / catch):

With this function, I always meet the "NullPointerException" because receiver is null which got by self. And I have tried another way like this(If I remove the all try/catch):

self = service.getDeclaredMethod("mSelf", null);
receiver = self.invoke(null, null); // mSelf is a static method which in ActivityMangerService class

同样的结果,我不能让ActivityManagerService的实例,然后我不能得到mRecentTasks。 任何意见是AP preciated,虽然我不知道如何删除所有项目在此列表中,但它可能是另外一个问题。

The same result, I can't get the instance of ActivityManagerService and then I can't get the mRecentTasks. Any comments is appreciated and although I don't know "how to remove all items in this list", but it could be another questions.

感谢。

推荐答案

我发现最好的办法就是做到这一点:

The best way I have found is to do this:

public class BaseActivity extends Activity {
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

            Log.d("Focus debug", "Focus changed !");

        if(!hasFocus) {
            Log.d("Focus debug", "Lost focus !");

            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }
}// all credit goes here: http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/

这是不是我自己的code,但是这只是隐藏在最近的应用程序列表中的显示。

This is not my own code, but this just hides the recent apps list from showing.

这篇关于Android的,如何清除最近的任务列表,它可以在大多数手机得到Home键?反思是一种可能的方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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