Android源代码在长按home键添加一键清除当前任务

本文详细介绍了如何在SystemUI中修改布局文件和Java文件,新增加一个按钮用于清理所有最近应用,同时提供了编译和推送代码到手机的命令。包括对布局的调整、Java代码的修改以及在新线程中执行清理操作的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要修改一个布局文件:/frameworks/base/packages/SystemUI/res/layout/status_bar_recent_panel.xml

一个java文件:/frameworks/base/packages/SystemUI/src/com/android/systemui/recent/RecentsPanelView.java


布局文件作如下更改,主要添加一个按钮。

<com.android.systemui.recent.RecentsPanelView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui"
    android:id="@+id/recents_root"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:foreground="@drawable/bg_protect"
    systemui:recentItemLayout="@layout/status_bar_recent_item"
    >
    <span style="color:#ff0000;"><RelativeLayout</span>
        android:id="@+id/recents_bg_protect"
        android:background="@drawable/status_bar_recents_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true">
        <com.android.systemui.recent.RecentsVerticalScrollView
            android:id="@+id/recents_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:divider="@null"
            android:stackFromBottom="true"
            android:fadingEdge="vertical"
            android:scrollbars="none"
            android:fadingEdgeLength="@dimen/status_bar_recents_scroll_fading_edge_length"
            android:layout_gravity="bottom|start"
            <span style="color:#ff0000;">android:layout_above="@+id/clean_all_recent_apps"</span>
            android:clipToPadding="false"
            android:clipChildren="false">
            <LinearLayout android:id="@+id/recents_linear_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:fitsSystemWindows="true"
                android:clipToPadding="false"
                android:clipChildren="false">
            </LinearLayout>

        </com.android.systemui.recent.RecentsVerticalScrollView>
    <span style="color:#ff0000;"><Button
        android:id="@+id/clean_all_recent_apps"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="@string/status_bar_no_recent_apps"
        android:gravity="center"
	    android:layout_alignParentBottom="true"
        /></span>
<span style="color:#ff0000;"> </RelativeLayout></span>
    <include layout="@layout/status_bar_no_recent_apps"
        android:id="@+id/recents_no_apps"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible" />

</com.android.systemui.recent.RecentsPanelView>







java文件作如下修改,做出按钮的响应和删除所有任务的功能,在新线程中执行,避免影响主线程,并延时动态效果处理。

 protected void onFinishInflate() {
        super.onFinishInflate();

        mRecentsContainer = (RecentsScrollView) findViewById(R.id.recents_container);
        mRecentsContainer.setOnScrollListener(new Runnable() {
            public void run() {
                // need to redraw the faded edges
                invalidate();
            }
        });
        mListAdapter = new TaskDescriptionAdapter(mContext);
        mRecentsContainer.setAdapter(mListAdapter);
        mRecentsContainer.setCallback(this);

        mRecentsScrim = findViewById(R.id.recents_bg_protect);
        mRecentsNoApps = findViewById(R.id.recents_no_apps);
        
        //The Clean All Button Handle
        mClearRecent = (Button)findViewById(R.id.clean_all_recent_apps);
        if(mClearRecent != null)
        {
            mCleanAllHandler = new CleanAllRecentsHandler();
            mClearRecent.setOnClickListener(new OnClickListener() {                
                @Override
                public void onClick(View v) {                    
                    new Thread(new CleanAllRecentsThread()).start();
                }
            });
        }
        if (mRecentsScrim != null) {
            mHighEndGfx = ActivityManager.isHighEndGfx();
            if (!mHighEndGfx) {
                mRecentsScrim.setBackground(null);
            } else if (mRecentsScrim.getBackground() instanceof BitmapDrawable) {
                // In order to save space, we make the background texture repeat in the Y direction
                ((BitmapDrawable) mRecentsScrim.getBackground()).setTileModeY(TileMode.REPEAT);
            }
        }
    }

    class CleanAllRecentsThread implements Runnable{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout);
            mCurRecentID = -1;
            if (mLinearLayout != null) {
                Log.i(VIEW_LOG_TAG, "ChildCount" + mLinearLayout.getChildCount());
                int count = mLinearLayout.getChildCount();
                for(int i = count - 1; i >= 0; i--){
                    mCurRecentID = i;
                    Message msg = new Message();
                    mCleanAllHandler.sendMessage(msg);
                    
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                    
                }
            }else {
                Log.i(VIEW_LOG_TAG, "LinearLayout is null");
            }
        }
        
    }
    class CleanAllRecentsHandler extends Handler{

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            
            if (mCurRecentID < 0) {
                return;
            }
            View view = mLinearLayout.getChildAt(mCurRecentID);
            ((ViewGroup) mRecentsContainer).removeViewInLayout(view);
            
            super.handleMessage(msg);
        }
        
    }



刷入手机的命令:

编译:./mk -t -o=USE_CCACHE=1,TARGET_BUILD_VARIANT=user m05 mm frameworks/base/packages/SystemUI/
./mk -t -o=USE_CCACHE=1,TARGET_BUILD_VARIANT=user m05 mm frameworks/base/


push到手机:adb remount

adb push out/target/product/m05/system/priv-app/SystemUI.odex system/priv-app/
adb push out/target/product/m05/system/priv-app/SystemUI.apk system/priv-app/

adb push out/target/product/m05/system/framework/framework.odex system/framework
adb push out/target/product/m05/system/framework/framework2.odex system/framework
adb push out/target/product/m05/system/framework/framework2.jar system/framework
adb push out/target/product/m05/system/framework/framework.jar system/framework

adb reboot







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值